Console.log statements output nothing at all in Jest
Asked Answered
L

24

346

console.log statements output nothing at all in Jest. This was working for me yesterday, and all of sudden, it's not working today. I have made zero changes to my config and haven't installed any updates.

I'm not using the --forceExit option. Still seeing this issue.

Lilybel answered 8/2, 2018 at 22:19 Comment(1)
This might answer, for some use cases at least: https://mcmap.net/q/94233/-no-console-log-to-stdout-when-running-quot-npm-test-quot-jestHorace
D
227

Jest suppresses the console log message by default. In order to show the console log message, set silent option to false at the command line

set --silent=false in the command line:

npm run test -- --silent=false

Drops answered 9/4, 2020 at 0:17 Comment(2)
I vote for this one! Also search your code/command for the text --SILENT as there are so many different places with jest where this can be added, if you can't find any silent parameter then you should see logging.Meatball
even after using this flag, in watch mode, when tests are re-triggered, console logs are not emitted by jestAmboina
P
134

You can run both options together like this --watch --verbose false if you want to also be watching the files and see the output.

for one time runs just do --verbose false

Playacting answered 8/12, 2018 at 16:55 Comment(6)
Note that the order of options matter. For example, if you use -t, you need to put --verbose false before the -t: jest --verbose false -t my-testExpiable
This answer is incorrect for Jest 27. verbose is related to how the test outputs pass/fail results. silent is for suppressing console output. jestjs.io/docs/cli#--silentDarsey
this (--verbose false) doesn't work for me, I still don't see the console.log statements outputBumper
Nobody else thinks it's weird that to get more output you have to set verbose mode to false?Transudate
YES, ITS WEIRD.Bolding
no it's not weird. it's super weird.Donohue
T
106

This is a pretty old question and still there's no accepted answer. However, none of the suggested solutions worked for me (settings like --silent --verbose etc.). The main problem is that Jest changes the global console object. So, the easiest solution is to not use the global console object.

Instead import dedicated log functions from the console module and work with those:

import { log, error } from "console";

error("This is an error");
log("Something to log");

As easy as that.

Triserial answered 19/1, 2022 at 9:49 Comment(7)
worked for me but I had to add // @ts-ignore above the importKolk
The only solution that works for me, thank you! I did import { log } from 'console', and use log instead of console.log.Lauder
Since JEST redefines the global console object, this is the only workaround. Works perfectly.Citrin
Simple. Works (in Jest 29). +10 if I could.Darsey
Or use a dedicated logging module...Agnomen
One-liner:require('console').error('hello jest');Terwilliger
This should be the accepted solution. import { log } from 'console', and use log instead of console.logAlcove
P
74

As per comment on https://github.com/facebook/jest/issues/2441,

Try setting verbose: false (or removing it) in the jest options in package.json.

Puffin answered 30/6, 2018 at 14:2 Comment(4)
Just to add to this, if you have --verbose in your command line flags you need to remove that as well. I thought this answer was incorrect until I realized that. Thanks. Also, the relevant comment is here: github.com/facebook/jest/issues/2441#issuecomment-286248619Hydr
Removing "verbose": true didn't solve it for me, I had to change it to false "verbose": false, which seems to have fixed it. Thank you!Springe
In my case there was a --silent flag in the package.json file, had to remove it in order to see the console.log messagesHarrison
This answer is incorrect for Jest 27. verbose is related to how the test outputs pass/fail results. silent is for suppressing console output. jestjs.io/docs/cli#--silentDarsey
Q
33

Try using console.debug() instead.

Run console.debug('Message here', yourValueHere) inside test function and it should show in the console output when running test script. You can verify if it works using Ctrl+F and find Message here in the standard output.

This does the trick of showing output in the console, while it is not an answer quite on how to use console.log I understand.

I am running @testing-library/jest-dom and jest-junit 12.0.0 as devDependencies. jest-junit has a minimal configuration of

  "jest-junit": {
    "usePathForSuiteName": "true"
  },

in package.json. This is mainly to configure coverage reporting. jest is configured like this:

  "jest": {
    "testMatch": [
      "**/__tests__/**/*.[jt]s?(x)",
      "**/?(*.)+(spec|test).[jt]s?(x)",
      "!**/utilities.ts",
    ],
Quotable answered 24/8, 2021 at 13:7 Comment(0)
Q
21

One of the potential reason that logging is not printing is due to console.log has been mocked. Something as below

// jest-setup.js
global.console = {
  // eslint-disable-next-line no-undef
  log: jest.fn(), // console.log are ignored in tests
  // log: console.log,

  // Keep native behaviour for other methods, use those to print out things in your own tests, not `console.log`
  error: console.error,
  warn: console.warn,
  info: console.info,
  debug: console.debug,
};
// package.json
  "jest": {
    "preset": "react-native",
    "moduleFileExtensions": [
      "ts",
      "tsx",
      "js",
      "jsx",
      "json",
      "node"
    ],
    "setupFilesAfterEnv": [
      "@testing-library/jest-native/extend-expect",
      "<rootDir>/src/config/jest-setup.js"
    ],
    "testMatch": [
      "<rootDir>/src/**/__tests__/**/*.test.{ts,tsx}"
    ]
  },

This is commonly used if you wish to disable console.log in jest

Quartile answered 24/8, 2021 at 13:14 Comment(1)
I've seen global.console = { log: process.env['JEST_VERBOSE'] ? console.log : jest.fn(), }; in some projects. I think it's a better way to control the console output.Polysepalous
T
19

Check for your command line flags in package.json to see that you don't have --silent in there.

Teleutospore answered 27/4, 2020 at 12:53 Comment(0)
M
18

If using Webstorm with Jest configuration, click on the file name instead of the test name.

Webstorm Jest panel

Macronucleus answered 21/4, 2022 at 13:26 Comment(2)
That is the case for not just webstorm, but any jetbrain IDEPaperback
This of all the things is what I needed to know. Thank you so much!Bluing
M
17

in addition to --verbose option which can cause this as mentioned, be aware that the --watch may also cause this bug.

Murderous answered 17/10, 2018 at 6:50 Comment(0)
M
16

Also be sure that your jest config does not have silent: true. In my case, I didn't realize that someone else had added that to our config.

I don't see it in the list of config options, but the command line flag is documented here.

Marinamarinade answered 21/2, 2019 at 3:24 Comment(0)
T
12

Having tried a few of the config options in the previous replies, using console.debug() instead of console.log() worked.

Tuff answered 10/3, 2022 at 10:32 Comment(1)
Also works in vitest 🙏Past
D
8

According to the v27 docs silent is what you want here. verbose false (the default) prevents Jest from outputting the result of every test in a hierarchy while silent true (the default) will:

Prevent tests from printing messages through the console.

Use npx jest --silent false if you want to run Jest with that option from the CLI. Tested this just now with console.log and it works as expected.

Darsey answered 15/10, 2021 at 20:21 Comment(0)
F
8

This is what worked for me: jest --verbose true

Frill answered 8/2, 2023 at 11:43 Comment(0)
V
7

In my case, the issue was caused by [only] flag in: it.only() or test.only('some text',()=>{})

Vachel answered 11/8, 2020 at 12:48 Comment(2)
Can you please provide more info? Where is the flag applied?Hesychast
@Hesychast Not really a flag, but when you do this test.only( ... ). remove the only function. See the docsDemagnetize
P
5

Tried the advice given regarding jest config settings to no avail. Instead, in my case, the issue seemed related to not awaiting asynchronous code:

test("test", async () => {
  console.log("Does output")

  new Promise(resolve => {
    // some expectation depending on async code
    setTimeout(() => resolve(console.log("Does not output")) , 1)
  })
})

Rather, awaiting the promise does output the async log:

test("test", async () => {
  console.log("Does output")

  await new Promise(resolve => {
    // some expectation depending on async code
    setTimeout(() => resolve(console.log("Does output")) , 1)
  })
})

Possibly related background: https://github.com/facebook/jest/issues/2441

Preternatural answered 19/8, 2021 at 19:2 Comment(1)
this was my case as well. I have the console.debug right before the expect(calls.length).toBe(1); and only after having await expect(calls.length).toBe(1); the console.debug worked. 🤯Catcher
G
4

Try using console.info() which is an alias for console.log(). I tried almost all the above answers but still console.log() didn't worked for me by any means. So, used console.info() which did the work.

Gather answered 11/1, 2022 at 11:17 Comment(0)
A
4

On MacOS with jest version 26.6.3 I had to append --silent="false"

Activity answered 2/11, 2022 at 7:37 Comment(0)
K
3

Nothing else was working for me, not verbose or runInBand etc.. Finally I tried:

--useStderr=true

Even though it is a setting for errors, I was able to see console.log('test log'); after setting this option.

Kinchen answered 14/8, 2023 at 15:55 Comment(1)
This is a useful answer & solves the printing issue efficiently.Gattis
R
1

In my case the problem was importing the functions from the compiled version (present in dist folder) instead of the src folder. And therefore it was using the old version. So rebuilding the project and/or importing from src fixed my issue.

Romulus answered 19/9, 2022 at 9:9 Comment(0)
A
0

In my case the problem was that the logs where made when the module is required, so before the start of an actual test case. Change from a top-level import to using require inside the test case fixed the problem.

Automat answered 15/2, 2019 at 8:59 Comment(0)
N
0

In my case transform-remove-console plugin is present in my babel.config.js

if (api.env() !== 'development') {
  plugins.push('transform-remove-console');
}

I remove the plugin and it now works.

Nacre answered 6/7, 2023 at 10:32 Comment(0)
M
0

Try to set "verbose: false" in jest.config.js

Marceau answered 20/10, 2023 at 9:13 Comment(0)
V
0

As of November 2023 with jest v. 29.7.0 you need to set the env variable DEBUG to test, like this:

DEBUG=jest npx jest -t 'my test' 

...and it results in the test running with the console.log outputs, for example:

  console.log
    Testing redirect from /foo to /foo/bar

      at Object.log (src/index.test.js:143:15)

  console.log
    Map(21) {

(...)

But it's not specific to running a single test - running all of them gives the same result.

Vanhorn answered 14/11, 2023 at 9:8 Comment(2)
Where is this documented?Preferential
I can't find any docs about it, @smac89... I found this suggestion here: github.com/jestjs/jest/issues/8208#issuecomment-595188558Vanhorn
U
-4

renaming my file to index.test.js from index.spec.js did the trick for me.

Ulphi answered 4/3, 2021 at 5:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.