W
W
Wasya UK2021-07-29 19:06:56
Software testing
Wasya UK, 2021-07-29 19:06:56

How to test transports in winston using jest?

I need to check if the logs are written to the database. I use winston-mongodb + winston, I test with jest, but I don't know how to do such a check. I thought to just write a function and check the database after a certain
time interval, but this idea seems bad to me.

I get this logger.

const loggerHttp = winston.createLogger({
  level: 'info',
  format: winston.format.combine(
    winston.format.timestamp(),
    winston.format.json(),
    winston.format.metadata()
  ),

  defaultMeta: { service: 'user-service' },
  transports: [
    new winston.transports.File({ filename: errorLogFilename, level: 'error' }),
    new winston.transports.File({ filename: logFilename }),
    new winston.transports.MongoDB(mongoLogOptions),
  ],
});

module.exports = loggerHttp;


I don't know how to check if there is a log in the database
it('Should write logs to database', () => {
    const logMock = jest
      .spyOn(loggerHttp, 'log')
      .mockImplementation(() => true);

    loggerHttp.log({ level: 'info', message: messageString });

    expect(logFromDatabase.message).toBe(messageString);
  });

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question