Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to turn off logs from winston for jest

Currently I am testing a NodeJS app using jest. Particularly the system looks like this:

  • NodeJS v13.4.0
  • Jest 25.4.0
  • winston@next 3.2.1

When I run tests, I like the log to be ordered. Especially at the first test and then dig into the logs. I know, that jest has the option --silent. Funnily this option does not apply to winston. The source code of winston looks like it is trying to directly write to stdout/err.

Does somebody know, how I can get winston to be silent while tests are running with the --silent option

like image 811
TheDome Avatar asked Sep 04 '25 02:09

TheDome


1 Answers

you can utilize setupFilesAfterEnv jest option, by pointing it to a file that will mock console. for instance

add this to jest.config.js:

{
  ...
  setupFilesAfterEnv: ["./jest.setup.js"],
  ...
}

then create a file jest.setup.js

global.beforeAll(() => {
  console.error = jest.fn().mockImplementation(() => {});
  console.info = jest.fn().mockImplementation(() => {});
  console.log = jest.fn().mockImplementation(() => {});
});

The source code of winston looks like it is trying to directly write to stdout/err.

note that, since you configured winston to print to stdout\stderr, so the following advise above should do the trick. of course, you might wish to add an additional check for the --silent flag

otherwise, you can have a condition to silence winston by utilizing the silent property of winston transport

silent: If true, all logs are suppressed

like image 163
Mr. Avatar answered Sep 06 '25 16:09

Mr.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!