yentsun
yentsun

Reputation: 2568

How to disable Jest `console.log` tags

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:

enter image description here

I haven't found any related options in the docs. How can I disable it?

Upvotes: 19

Views: 4887

Answers (4)

theCrazySander
theCrazySander

Reputation: 184

tslalamam answer code not worked for me, but this one works

1: Create a file with this code (e.g. config.js)

import console from "console"
global.console = console

2: Add this line to your jest.config.js

setupFilesAfterEnv: ["./config.js"]

Before:

How to disable Jest console.log tags

After:

How to disable Jest console.log tags

Enjoi!

Upvotes: 1

tslalamam
tslalamam

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

Ahmad MOUSSA
Ahmad MOUSSA

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

yentsun
yentsun

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

Related Questions