Console.log statements output nothing at all in Jest

Jestjsconsole.log

Jestjs Problem Overview


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.

Jestjs Solutions


Solution 1 - Jestjs

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

Solution 2 - Jestjs

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

Solution 3 - Jestjs

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.

Solution 4 - Jestjs

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

Solution 5 - Jestjs

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

Solution 6 - Jestjs

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.

Solution 7 - Jestjs

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

Solution 8 - Jestjs

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",
    ],

Solution 9 - Jestjs

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 { error } from "console";

error("This is an error");

As easy as that.

Solution 10 - Jestjs

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

Solution 11 - Jestjs

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.

Solution 12 - Jestjs

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

Solution 13 - Jestjs

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

Solution 14 - Jestjs

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.

Solution 15 - Jestjs

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

Webstorm Jest panel

Solution 16 - Jestjs

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.

Solution 17 - Jestjs

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionHina DawoodView Question on Stackoverflow
Solution 1 - JestjsDavid DehghanView Answer on Stackoverflow
Solution 2 - JestjsArrHView Answer on Stackoverflow
Solution 3 - JestjsApurva MulayView Answer on Stackoverflow
Solution 4 - JestjsRaduView Answer on Stackoverflow
Solution 5 - JestjsLiran BrimerView Answer on Stackoverflow
Solution 6 - JestjsTom WaysonView Answer on Stackoverflow
Solution 7 - JestjsIsaacView Answer on Stackoverflow
Solution 8 - JestjsErkka MutanenView Answer on Stackoverflow
Solution 9 - JestjsMike LischkeView Answer on Stackoverflow
Solution 10 - Jestjscaptain-yossarianView Answer on Stackoverflow
Solution 11 - JestjsjcollumView Answer on Stackoverflow
Solution 12 - Jestjsdgh500View Answer on Stackoverflow
Solution 13 - JestjsJoe Van LeeuwenView Answer on Stackoverflow
Solution 14 - Jestjspegasus013View Answer on Stackoverflow
Solution 15 - JestjsAmbroise RabierView Answer on Stackoverflow
Solution 16 - JestjsMatt ZeunertView Answer on Stackoverflow
Solution 17 - JestjsJoelView Answer on Stackoverflow