Currently I am testing a NodeJS app using jest. Particularly the system looks like this:
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With