title: How to use Jest to mock winston logger instance encapsulated in service class
body: <p>I am trying to mock a <em>winston.Logger</em> instance that is encapsulated within a service class created with NestJS. I have included my code below. </p>
  
  <p>I cannot get the mocked logger instance to be triggered from within the service class. Can anyone explain where I am going wrong?</p>
  
  <pre class="lang-js prettyprint-override"><code>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', () =&gt; (
      {
          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 --", () =&gt; {
      let loggerMock: winston.Logger;
  
      test('testing logger log function called...', () =&gt; {        
          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);
      });
  
     ...
  </code></pre>
  
  <p><strong>LoggingService debug method code</strong></p>
  
  <pre><code>public debug(message: string) {
          this.logger.log(
              {
                  level: types.LogLevel.DEBUG,
                  message: message,
                  meta: {
                      context: this.contextName
                  }
              }
          );
      }
  </code></pre>
  
  <p><strong>Update: 3/09/2019</strong></p>
  
  <p>Refactored my nestjs LoggingService to dependency inject winston logger instance in constructor to facilitate unit testing. This enables me to use <em>jest.spyOn</em> on the winston logger's log method and check that it has been called within the service instance:</p>
  
  <pre class="lang-js prettyprint-override"><code>// create winstonLoggerInstance here, e.g. in beforeEach()....
  const winstonLoggerMock = jest.spyOn(winstonLoggerInstance, 'log');
  serviceInstance.debug('debug sent from test');
  expect(winstonLoggerMock).toHaveBeenCalled();
  </code></pre>