19

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?

10
  • 1
    Please check if this answer is helpful to you. Commented Jul 28, 2018 at 9:47
  • Does this answer your question? Remove logging the origin line in Jest
    – jonrsharpe
    Commented Dec 16, 2020 at 11:55
  • @jonrsharpe you are posting a link to a question, not an answer. Also, I've already mentioned one of the answers to the question you linked in my own answer to my question: stackoverflow.com/a/51588150/216042.
    – yentsun
    Commented Dec 17, 2020 at 9:18
  • 1
    That's exactly not what should happen, this question should be closed as a duplicate of the other one, so the next person with the same problem gets sent to the answers there (which it does have, you just don't want to use them). What you're suggesting with that dupe proposal is that people going to that question be send here, which makes absolutely no sense. I don't think you've understood how duplicates work.
    – jonrsharpe
    Commented Dec 17, 2020 at 10:00
  • 1
    @jonrsharpe "do you really think that the answer below (basically "don't use Jest") is more useful" -- absolutely! It saved me a ton of valuable time at that moment. For developing with React - you use Jest without questions. Developing server-side Node.js - questionable, unless you are okay with some annoying this and that. So yes, I claim my answer is still a better one.
    – yentsun
    Commented Dec 17, 2020 at 10:18

4 Answers 4

14

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');
  });
1
  • 1
    This seems to also work as a stand-alone command in a setup file. Thanks for the tip!
    – Max Wilder
    Commented Sep 16, 2022 at 23:48
4

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

1

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.

0

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!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.