Reputation: 2568
I have some NodeJS logging done via console.log()
internally (its actually loglevel) and as I see it, Jest tags console.log output with console.log ../path/to/string/with/console.log/call:line#
for whatever reason:
I haven't found any related options in the docs. How can I disable it?
Upvotes: 19
Views: 4887
Reputation: 184
tslalamam answer code not worked for me, but this one works
import console from "console"
global.console = console
setupFilesAfterEnv: ["./config.js"]
Enjoi!
Upvotes: 1
Reputation: 218
Create a global configuration test file e.g. src/test/config.js
, add this line to that file:
jest.spyOn(console, "log").mockImplementation();
add this to jest config:
setupFilesAfterEnv: ['./src/test/config.js']
you can also use that file for global cleanup, before/after each etc
Upvotes: 4
Reputation: 2906
IMPORTANT:
I had the curiosity to take a look to the answer mentioned in the first answer, wich it says:
Looking at the source code for Jest, it doesn't seem like there is a neat way to turn those messages off.
And I noticed an update marked on the answer and resolves the problem.
Solution:
Just add this code to your test:
beforeEach(() => {
global.console = require('console');
});
Upvotes: 14
Reputation: 2568
Thanks @Anders Carstensen, I've looked at the answer you mentioned and it says:
Looking at the source code for Jest, it doesn't seem like there is a neat way to turn those messages off.
Not an option for me to write my own console, so I'll just stick with Mocha/Sinon for now.
Upvotes: 1