vendredi 6 mars 2020

Jest expect issue with called fct


I'm a little titled with a problem with jest. My tests which test the call of the createLogger function from winston doesn't trigger.
As I'm exporting the logger variable which has been filled by the result of createLogger, I don't understand why my expect isn't working. Can someone tells me where's the problem?
Thanks :D.

Here's my code:

// Some code before
const logger = winston.createLogger({
  level: 'debug',
  format: winston.format.combine(
    winston.format.json(),
    winston.format.colorize(),
    winston.format.simple(),
  ),
  transports: process.env.NODE_ENV === 'production' ? [
    new winston.transports.Console(),
    loggingWinston, // Don't worry about this => coming from @google-cloud/logging-winston
  ] : [
    new winston.transports.Console(),
  ],
});
module.exports = logger;

My test on runkit

The error:


● default behavior with production env

    expect(jest.fn()).toHaveBeenCalledTimes(expected)

    Expected number of calls: 1
    Received number of calls: 0

      51 |   process.env.NODE_ENV = 'production';
      52 |
    > 53 |   expect(winston.createLogger).toHaveBeenCalledTimes(1);
         |                                ^
      54 | });
      55 |
      56 | test('default behavior with development env', () => {

      at Object.<anonymous> (src/lib/__tests__/logger.test.js:53:32)

Aucun commentaire:

Enregistrer un commentaire