test(middleware-logger): increased test coverage
ADD
packages/middleware-logger/src/spec/index.spec.ts
+import * as index from '../index';
+
+test('index exposes public API', () => {
+ expect(index.logger$).toBeDefined();
+ expect(index.loggerWithOpts$).toBeDefined();
+});

MODIFY
packages/middleware-logger/src/spec/logger.factory.spec.ts
packages/middleware-logger/src/spec/logger.factory.spec.ts
@@ -69,4 +69,25 @@ describe('Logger factory', () => {
timestamp: '2018-10-21T18:17:33.000Z',
});
});
+
+ test('#factorizeLog factorizes logger message without timestamp', () => {
+ // given
+ const res = { statusCode: 200 } as HttpResponse;
+ const req = { method: 'GET', url: '/api/v1' } as HttpRequest;
+ const stamp = { value: req, timestamp: 1539031930521 };
+
+ // when
+ loggerFactoryModule.prepareLogString = jest.fn();
+ loggerUtilModule.factorizeTime = jest.fn(() => '300ms');
+ factorizeLog(res, stamp)();
+
+ // then
+ expect(loggerFactoryModule.prepareLogString).toHaveBeenCalledWith({
+ method: 'GET',
+ url: '/api/v1',
+ statusCode: '200',
+ time: '300ms',
+ timestamp: '',
+ });
+ });
});

ADD
packages/middleware-logger/src/spec/logger.handler.spec.ts
+import { Timestamp } from 'rxjs';
+import { EventEmitter } from 'events';
+import { WriteStream } from 'fs';
+import { HttpResponse, HttpRequest } from '@marblejs/core';
+import { loggerHandler } from '../logger.handler';
+import { LoggerOptions } from '../logger.model';
+
+class HttpResponseMock extends EventEmitter {}
+
+describe('#loggerHandler', () => {
+ let loggerUtil;
+ let loggerFactory;
+
+ beforeEach(() => {
+ jest.unmock('../logger.util.ts');
+ loggerUtil = require('../logger.util.ts');
+
+ jest.unmock('../logger.factory.ts');
+ loggerFactory = require('../logger.factory.ts');
+ });
+
+ afterEach(() => {
+ jest.clearAllMocks();
+ });
+
+ test('writes log to provided stream', () => {
+ // given
+ const res = new HttpResponseMock() as HttpResponse;
+ const stream = {} as WriteStream;
+ const opts = { silent: false, stream } as LoggerOptions;
+ const stamp = {} as Timestamp<HttpRequest>;
+ const expectedLog = 'test_log';
+
+ // when
+ loggerUtil.writeToStream = jest.fn();
+ loggerFactory.factorizeLog = jest.fn(() => () => expectedLog);
+ loggerHandler(res, opts)(stamp);
+ res.emit('finish');
+
+ // then
+ expect(loggerUtil.writeToStream).toHaveBeenCalledWith(stream, expectedLog);
+ });
+
+ test('writes log to console.info', () => {
+ // given
+ const res = new HttpResponseMock() as HttpResponse;
+ const opts = { silent: false };
+ const stamp = {} as Timestamp<HttpRequest>;
+ const expectedLog = 'test_log';
+
+ // when
+ jest.spyOn(console, 'info').mockImplementation(jest.fn());
+ loggerUtil.writeToStream = jest.fn();
+ loggerFactory.factorizeLog = jest.fn(() => () => expectedLog);
+ loggerHandler(res, opts)(stamp);
+ res.emit('finish');
+
+ // then
+ expect(console.info).toHaveBeenCalledWith(expectedLog);
+ });
+});

test(middleware-logger): increased test coverage