Sometimes, we want to disable console inside unit tests with Jest.
In this article, we’ll look at how to disable console inside unit tests with Jest.
How to disable console inside unit tests with Jest?
To disable console inside unit tests with Jest, we can use the silent
option or disable console output in the config.
We run
jest --silent
to run tests without showing console output.
In jest setup file, we can add
global.console = {
log: jest.fn(),
error: console.error,
warn: console.warn,
info: console.info,
debug: console.debug,
};
to replace console.log
with a stubbed function by setting global.console.log
to jest.fn
.
The other console methods are set to the regular console methods.
Conclusion
To disable console inside unit tests with Jest, we can use the silent
option or disable console output in the config.