I am trying to mock a winston.Logger instance that is encapsulated within a service class created with NestJS. I have included my code below.
I cannot get the mocked logger instance to be triggered from within the service class. Can anyone explain where I am going wrong?
import * as winston from 'winston';
import { loggerOptions } from '../logger/logger.config';
import { LoggingService } from '../logger/logger.service';
const logger: winston.Logger = winston.createLogger(loggerOptions);
// trying to mock createLogger to return a specific logger instance
const winstonMock = jest.mock('winston', () => (
{
format: {
colorize: jest.fn(),
combine: jest.fn(),
label: jest.fn(),
timestamp: jest.fn(),
printf: jest.fn()
},
createLogger: jest.fn().mockReturnValue(logger),
transports: {
Console: jest.fn()
}
})
);
describe("-- Logging Service --", () => {
let loggerMock: winston.Logger;
test('testing logger log function called...', () => {
const mockCreateLogger = jest.spyOn(winston, 'createLogger');
const loggingService: LoggingService = LoggingService.Instance;
loggerMock = mockCreateLogger.mock.instances[0];
expect(loggingService).toBeInstanceOf(LoggingService)
expect(loggingService).toBeDefined();
expect(mockCreateLogger).toHaveBeenCalled()
// spy on the winston.Logger instance within this test and check
// that it is called - this is working from within the test method
const logDebugMock = jest.spyOn(loggerMock, 'log');
loggerMock.log('debug','test log debug');
expect(logDebugMock).toHaveBeenCalled();
// now try and invoke the logger instance indirectly through the service class
// check that loggerMock is called a second time - this fails, only called once
// from the preceding lines in this test
loggingService.debug('debug message');
expect(logDebugMock).toHaveBeenCalledTimes(2);
});
...
LoggingService debug method code
public debug(message: string) {
this.logger.log(
{
level: types.LogLevel.DEBUG,
message: message,
meta: {
context: this.contextName
}
}
);
}
Update: 3/09/2019
Refactored my nestjs LoggingService to dependency inject winston logger instance in constructor to facilitate unit testing. This enables me to use jest.spyOn on the winston logger's log method and check that it has been called within the service instance:
// create winstonLoggerInstance here, e.g. in beforeEach()....
const winstonLoggerMock = jest.spyOn(winstonLoggerInstance, 'log');
serviceInstance.debug('debug sent from test');
expect(winstonLoggerMock).toHaveBeenCalled();