Skip to content

Commit

Permalink
test: added polaris logger tests
Browse files Browse the repository at this point in the history
  • Loading branch information
osher-sade committed Feb 7, 2019
1 parent ab6600f commit 74fb689
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 15 deletions.
1 change: 1 addition & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
clearMocks : true,
moduleFileExtensions: [
"ts",
"js",
Expand Down
10 changes: 0 additions & 10 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@
"@semantic-release/changelog": "^3.0.2",
"@semantic-release/git": "^7.0.8",
"@types/jest": "^23.3.13",
"@types/rimraf": "^2.0.2",
"cz-conventional-changelog": "^2.1.0",
"husky": "^1.3.1",
"jest": "^23.6.0",
Expand Down
121 changes: 121 additions & 0 deletions test/polaris-logger.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
import { Logger } from 'winston';
import { LoggerConfiguration } from '../src/configurations/logger-configuration';
import { ApplicationLogProperties } from '../src/entities';
import { PolarisLogger } from '../src/polaris-logger';
import { createLogger } from '../src/winston-logger';

const loggerImplMock: { [T in keyof Logger]: any } = {
fatal: jest.fn(),
error: jest.fn(),
warn: jest.fn(),
info: jest.fn(),
debug: jest.fn(),
trace: jest.fn(),
} as any;
jest.mock('../src/winston-logger', () => {
return {
createLogger: jest.fn(() => {
return loggerImplMock;
}),
};
});

describe('polaris-logger tests', () => {
const appProps: ApplicationLogProperties = {
id: 'p0laris-l0gs',
name: 'polaris-logs',
repositoryVersion: 'v1',
environment: 'environment',
component: 'component',
};
const config: LoggerConfiguration = {
loggerLevel: 'info',
logstashHost: '127.0.0.1',
logstashPort: 8080,
};
const message: string = 'log message';

test('constructor', () => {
const logger = new PolarisLogger(appProps, config);

expect(createLogger).toHaveBeenCalledWith(config);
});

test('fatal_loggingMessage_messageAndApplicationPropertiesAreInTheLog', () => {
const logger = new PolarisLogger(appProps, config);
logger.fatal(message);
expect(loggerImplMock.fatal).toHaveBeenCalledWith({
message,
component: appProps.component,
environment: appProps.environment,
repositoryVersion: appProps.repositoryVersion,
eventKindDescription: { systemId: appProps.id },
system: { id: appProps.id, name: appProps.name },
});
});

test('error_loggingMessage_messageAndApplicationPropertiesAreInTheLog', () => {
const logger = new PolarisLogger(appProps, config);
logger.error(message);
expect(loggerImplMock.error).toHaveBeenCalledWith({
message,
component: appProps.component,
environment: appProps.environment,
repositoryVersion: appProps.repositoryVersion,
eventKindDescription: { systemId: appProps.id },
system: { id: appProps.id, name: appProps.name },
});
});

test('warn_loggingMessage_messageAndApplicationPropertiesAreInTheLog', () => {
const logger = new PolarisLogger(appProps, config);
logger.warn(message);
expect(loggerImplMock.warn).toHaveBeenCalledWith({
message,
component: appProps.component,
environment: appProps.environment,
repositoryVersion: appProps.repositoryVersion,
eventKindDescription: { systemId: appProps.id },
system: { id: appProps.id, name: appProps.name },
});
});

test('info_loggingMessage_messageAndApplicationPropertiesAreInTheLog', () => {
const logger = new PolarisLogger(appProps, config);
logger.info(message);
expect(loggerImplMock.info).toHaveBeenCalledWith({
message,
component: appProps.component,
environment: appProps.environment,
repositoryVersion: appProps.repositoryVersion,
eventKindDescription: { systemId: appProps.id },
system: { id: appProps.id, name: appProps.name },
});
});

test('debug_loggingMessage_messageAndApplicationPropertiesAreInTheLog', () => {
const logger = new PolarisLogger(appProps, config);
logger.debug(message);
expect(loggerImplMock.debug).toHaveBeenCalledWith({
message,
component: appProps.component,
environment: appProps.environment,
repositoryVersion: appProps.repositoryVersion,
eventKindDescription: { systemId: appProps.id },
system: { id: appProps.id, name: appProps.name },
});
});

test('trace_loggingMessage_messageAndApplicationPropertiesAreInTheLog', () => {
const logger = new PolarisLogger(appProps, config);
logger.trace(message);
expect(loggerImplMock.trace).toHaveBeenCalledWith({
message,
component: appProps.component,
environment: appProps.environment,
repositoryVersion: appProps.repositoryVersion,
eventKindDescription: { systemId: appProps.id },
system: { id: appProps.id, name: appProps.name },
});
});
});
4 changes: 0 additions & 4 deletions test/winston-logger.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,6 @@ describe('winston-logger tests', () => {
const fileExtension: string = 'txt';
const numberOfDaysToDeleteFile: number = 55;

afterEach(() => {
jest.clearAllMocks();
});

test('createLogger_basicConfiguration_createsTheLoggerWithBasicConfiguration', () => {
const config: LoggerConfiguration = {
loggerLevel,
Expand Down

0 comments on commit 74fb689

Please sign in to comment.