New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

console.log does not output when running tests #2441

Open
udbhav opened this Issue Dec 25, 2016 · 157 comments

Comments

Projects
None yet
@udbhav

udbhav commented Dec 25, 2016

Do you want to request a feature or report a bug?

Report a bug.

What is the current behavior?

Calling console.log using the default testEnvironment of jsdom doesn't print to stdout.

If the current behavior is a bug, please provide the steps to reproduce and either a repl.it demo through https://repl.it/languages/jest or a minimal repository on GitHub that we can yarn install and yarn test.

  1. Clone https://github.com/udbhav/jest-test
  2. Run yarn test
  3. Confirm that you're not seeing anything from console.log
  4. Change the testEnvironment Jest configuration setting to node
  5. Re-run yarn test
  6. Confirm that you're seeing output from console.log

What is the expected behavior?

I would expect to have console.log always output while my tests are running.

Please provide your exact Jest configuration and mention your Jest, node, yarn/npm version and operating system.

See package.json and yarn.lock in the repo for package versions. I'm running node 7.3.0 and yarn 0.18.1

@udbhav

This comment has been minimized.

udbhav commented Dec 25, 2016

Just tested this using repl.it: https://repl.it/EwfT/0

Good news is it works as expected and console.log outputs. I attempted downgrading my jest version to 17.0.3, was still seeing the same issues. Installed nvm to test with node v6.9.2, and hurray console.log works with jsdom, so I'm assuming the issue is tied to node v7.

@thymikee

This comment has been minimized.

Collaborator

thymikee commented Dec 27, 2016

Thanks for reporting this, but that's a duplicate of #2166 and happens on Node 6 too.

@thymikee thymikee closed this Dec 27, 2016

@thisissami

This comment has been minimized.

thisissami commented Dec 29, 2016

@thymikee How is this a duplicate of #2166? In this scenario, console.log outputs nothing at all. I'm having this issue with Node v4. The frustrating thing is that it was working fine earlier today, and with 0 changes to my environment, I don't get any more console.log outputs to my terminal.

@cpojer

This comment has been minimized.

Contributor

cpojer commented Dec 29, 2016

@thisissami it seems like your test or code isn't running then. Jest's test suite passes on node 4 and node 6 which makes sure console printing works fine and we are working on a fix for 7.3.

@thisissami

This comment has been minimized.

thisissami commented Dec 29, 2016

@cpojer - My tests pass/fail properly - just console.log messages do not show up. I discovered this when trying to figure out what the properties of a particular object are by console.loging it, and seeing no output. I've added many console.log statements and none are showing up in my terminal now. =/ I'm currently reverting to Jest v17 to see if that changes anything.

@cpojer

This comment has been minimized.

Contributor

cpojer commented Dec 29, 2016

If it was working earlier today for you and isn't any more, you must have made an update or broken something yourself. We didn't publish a Jest release in two weeks.

@thisissami

This comment has been minimized.

thisissami commented Dec 29, 2016

ok so the only thing that's changed in my test code is that i've added a multiline comment after the code that's supposed to run (as a reference for myself). I'll see if removing that makes a difference.

@thisissami

This comment has been minimized.

thisissami commented Dec 29, 2016

@cpojer I don't know what to say, I don't see anything wrong in my test code but nothing is being outputted to stdout:

import React from 'react';
import {shallow} from 'enzyme';
import FundPercentage from '../../src/reactClasses/fund_percentage';

console.log('cmon');
describe('Normal Percentage', () => {
  console.log('do this');
  const percentage1 = shallow(
    <FundPercentage percentage={0.5}/>
  );
  const percentage2 = shallow(
    <FundPercentage percentage={0.26}/>
  );
  it('should work', () => {
    console.log(percentage1.childAt(0));
    expect(0).toBe(1);
  });
});

The test fails, as expected, so I know it's working. My config in package.json is really simple, solely:

  "jest": {
    "collectCoverageFrom": [
      "src/*.jsx"
    ]
  }

Jest ran totally normally, no extra flags attached. Same issue with Jest v17 & 18.

@thisissami

This comment has been minimized.

thisissami commented Dec 29, 2016

I haven't changed any files other than this one in the entire afternoon. I've just been getting to understand how enzyme works by outputting various things to stdout, and after beginning to add in some expects, the console.logs stopped working when I needed them again, and now they don't work at all - no matter what I have in the test. I haven't changed anything in my environment either (beyond downgrading just now to v17), which is certainly confusing.

@cpojer

This comment has been minimized.

Contributor

cpojer commented Dec 29, 2016

It seems like you updated to node 7.3. We have some fixes up for that. Please note this issue tracker is not a help forum; use stackoverflow for questions :)

@thisissami

This comment has been minimized.

thisissami commented Dec 29, 2016

I am using node v4.4.7, and as far as I'm concerned - not having anything show up to stdout when using console.log is an issue. I'm not trying to get help with coding, I'm reporting what appears to be a bug to me. The only other thing that's changed is that I had multiple test files running before, and now only one. let me see if re-enabling the other tests makes console.log outputs appear again.

@thisissami

This comment has been minimized.

thisissami commented Dec 29, 2016

@cpojer Fairly certain you guys have a bug here.

Having 3 tests run (as in 3 different files with .test.js, with 2 of those files being examples from your tutorials) works without issue. My test (copied above) renders all the console.logs.

Having just 1 test run (AKA me renaming .test.js to .teast.js on 2 of the files) results in no console.log outputs being rendered.

I'm gonna keep running a second arbitrary test so that I see the output I need, so I'm good for my own personal needs - but this should be addressed imo, assuming it's reproducable elsewhere.

@cpojer

This comment has been minimized.

Contributor

cpojer commented Dec 29, 2016

The test suite of Jest tests this behavior on node 4, node 6 and node 7.3. It works for 4 and 6 but was broken in 7.3. I'm fixing this for node 7.3 and will publish a new release of Jest shortly: #2464

If Jest's own test suite using node 4 fails for you, then something is likely wrong with your setup.

@thisissami

This comment has been minimized.

thisissami commented Dec 29, 2016

Cloning the repository and giving it a try now.

@thisissami

This comment has been minimized.

thisissami commented Dec 29, 2016

Everything passed except for these 3 tests. Not sure what implications that might have. You have all the info of what the errors related to console.log that I'm having are, as well as the image below. If that isn't a bug in jest, then so be it. Seems weird to me though that doing nothing but following the guides would result in a scenario where I can't see my logs when only running one test file.

screen shot 2016-12-28 at 5 55 29 pm

@cpojer

This comment has been minimized.

Contributor

cpojer commented Dec 29, 2016

These just indicate you don't have mercurial (hg) installed, unrelated to your issue. It seems like the test suite passes for you and as said; we explicitly test for the logging behavior in the test suite so it must be something going wrong with your code or setup.

@thisissami

This comment has been minimized.

thisissami commented Dec 29, 2016

Ok cool - thanks for your feedback. Do you have any inkling as to what could be causing it to work when there are multiple files but not when there is only 1? If there's no obvious "oh this sometimes causes an issue like that" that comes to your mind - no worries, I'll just ensure that there's always at least 2 files running. :)

@Alex-Mann

This comment has been minimized.

Alex-Mann commented Jan 5, 2017

I am experiencing the same issue, console.log is not outputting for me now (and it previously was about an hour ago). I'm using node 6.9.1 and also enabling the --forceExit flag. When I do not have this flag enabled, the console.log output appears.

However, I have another test script that utilizes the --forceExit flag and console.log appears, so I cannot say that the --forceExit flag is causing this behaviour.

As @thisissami is doing, I experience the logging issue only when I try to test a single file.

@philwhln

This comment has been minimized.

philwhln commented Jan 18, 2017

I was having the same issue, but found that it was due to running jest via jest-cli (jest.runCLI) within gulpfile.js and it was swallowing the output of console.log. If I run jest directly I see the console output.

@philwhln

This comment has been minimized.

philwhln commented Jan 19, 2017

I'm now having seeing some funky behaviour, which I cannot isolate to a simple test case yet, otherwise I'd file a new issue.

  1. Jest runs a test
  2. Jest outputs the console.log statements (don't blink here)
  3. Jest scrolls back up an arbitrary number of lines, which sometimes covers all the console.log lines, sometimes some and sometimes all.
  4. Jest runs the next test (console.log lines from previous test disappear).

jest v18.1.0

Since I cannot isolate this in a simple test, I'm guessing it has something to do with running an more complex asynchronous test.

jeffbski added a commit to jeffbski/react-boilerplate-logic that referenced this issue Jan 30, 2017

upgrade jest-cli
Upgrade jest-cli since it was not logging stdout under node 7.4

Apparently there was a bug that was recently fixed to be compatible with Node v7.

facebook/jest#2441
@davidgilbertson

This comment has been minimized.

davidgilbertson commented Feb 8, 2017

I'm getting the same issue, I wonder if it's output overwriting other output. Occasionally I'll see something like this:

image

It's like one process outputted an error (failed prop types) but it just gets written right over by the test output logs.

More:

image

And sometimes if I Ctrl + c while the tests are running I'll see console logs that I wouldn't see if I let the tests finish.

Versions
Jest: 17.0.1
Node: 6.6.0
NPM: 3.10.3
macOS: 10.12.2
Terminal: 2.7.1 (and also in iTerm2 3.0.13)
Script (in package.json): jest /src/main/js --watch or jest /src/main/js --coverage or jest --watch --onlyChanged all have the same behaviour.

@thymikee

This comment has been minimized.

Collaborator

thymikee commented Feb 8, 2017

@davidgilbertson does it happen on v18.1?

@davidgilbertson

This comment has been minimized.

davidgilbertson commented Feb 16, 2017

I've tried 18.1.0 and it seems even buggier. And I still see things like this:

image

I can't pick any particular pattern, but some things that have happened (I have a console.warn('----------------') in one of my components.)

  • If I'm scrolled up a bit from the bottom when I run the tests, I see the log. If I scroll down the terminal window while the tests are rolling (so that it begins auto scrolling) then when the test are finished, I scroll back up and the console.warn line is gone (presumably overwritten).
  • If I run the tests and straight away hit ctrl + c, I see some console logs. But if I do the same and don't interrupt, those console logs aren't visible.
  • When I run jest /src/main/js --watch once, then hit a to run again, it picks up a whole lot of legacy tests in a directory src/main/assets/legacy - not matching the glob.
  • Same as the above if I just run jest --watch (all my tests end in .test.js or .test.jsx). So hitting 'a' in watch mode seems to go looking everywhere, including an old jasmine test called src/test/js/spec/categoryselector/spec.js. Hitting Enter seems to do what I expect.

Could it be that all the errors thrown by missing props causes this issue? I have to keep ctrl+cing the output to try and catch these errors before they're overwritten.

@jarecsni

This comment has been minimized.

jarecsni commented Mar 13, 2017

Not working for me either (jest 19.0.1, node 5.12). Interestingly it does work on another project with same setup. Not sure how to debug Jest tests so being able to see some console messages would be quite important I think.

@philwhln

This comment has been minimized.

philwhln commented Mar 13, 2017

Have you tried using the jest flag --runInBand to see if that solves it by running tests serially? Somebody mentioned this to me, but haven't had opportunity to test it.

@PaulTomchik

This comment has been minimized.

PaulTomchik commented Mar 13, 2017

The bail flag prevent the console logs from printing.

@jarecsni

This comment has been minimized.

jarecsni commented Mar 13, 2017

I will try. The slightly unnerving thing is that in the same project some console log statements are being output on the console, some aren't. Unfortunately the ones I tried to add where a test case is failing doesn't get output (I tried to add everywhere in the test case, in the unit tested, etc to no avail). So I don't actually think this can be down to a flag, as the behaviour is not concise. It's a react native project, following standard layout btw.

@jarecsni

This comment has been minimized.

jarecsni commented Mar 13, 2017

None of this seems to help. The --bail flag just stops other tests from being run (the offending one is last one anyway in my case), and --runInBands doesn't seem to have any observable difference. Jest does run the updated files btw, so if I insert a console.log statement, the error stack trace shows the error coming from the updated line number. It's just the console log doesn't happen. Since it's hard/impossible to debug these tests in a browser (pls correct me if this is not the case) console.log is absolutely vital to fix some issues in the tests.

wcalderipe added a commit to wcalderipe/easy-toggles that referenced this issue Sep 5, 2018

Disable Jest verbose option
Jest is presenting a bug when running in verbose mode that's impossible
to use `console.log` outside a test file. Use logs inside the source
modules is one of the main debugging actions to understand what is
going on.

References:
facebook/jest#2441 (comment)
facebook/jest#6871
@lukepighetti

This comment has been minimized.

lukepighetti commented Sep 25, 2018

Adding --verbose false to package.json "test" script solved the problem for me. I would have never found that without this thread.

eg

"scripts": {
    "test": "jest --watch --verbose false"
}
@kevinsimper

This comment has been minimized.

kevinsimper commented Sep 30, 2018

Weird how some of the last console disappears but not all of them, fixed it with --verbose false in my watch command.

Works fine when you run without --watch so it is something about the watch flag that does it.

@lukepighetti

This comment has been minimized.

lukepighetti commented Oct 1, 2018

What's so ironic is that it's more verbose when console.log works!

Grsmto added a commit to PolymathNetwork/polymath-apps that referenced this issue Oct 1, 2018

@jamespolanco

This comment has been minimized.

jamespolanco commented Oct 1, 2018

I have started noticing this issue with verbose in Jest 23.6, I rolled back to 23.5 and I am seeing it still. If I run --clearCache this fixes verbose for a while until it fails again. I'm unclear what the trigger is, such as a lot of log failures or something along that line. Once that happens it appears to cause Jest to corrupt the cache. I am trying the --verbose false and see if that prevents it going forward.

@saltycrane

This comment has been minimized.

saltycrane commented Oct 2, 2018

Thanks @jamespolanco Using the --clearCache option fixed the issue for me (for the time being). I'm going to run my tests using the --no-cache option and see if that prevents the issue from recurring in the future.

EDIT: I should have mentioned that my problem was that only some of my console.log messages were printed out. I used 5 console.log lines in one test and only the first 3 lines were printed. Using --clearCache fixed this issue for me. Maybe there is a different issue where no console.logs show up?

@elarue53078

This comment has been minimized.

elarue53078 commented Oct 4, 2018

I am having this issue as well. I have tried using console.log, console.debug, and console.error. I have also tried using the --no-cache flag. In all cases, my console statement is getting completely ignored.

@capaj

This comment has been minimized.

Contributor

capaj commented Oct 11, 2018

I am having this issue in the watch mode too. Version 23.6.0

I even recorded it if anyone likes to see for themselves:
asciicast

like others-it only is erased when running with --watch. Tried --verbose false and that didn't help.

@alexandermckay

This comment has been minimized.

alexandermckay commented Oct 15, 2018

I am also having the same issue.

@LiranBri

This comment has been minimized.

LiranBri commented Oct 17, 2018

I'm also experiencing this when using verbose or when in watch mode

@r01010010

This comment has been minimized.

r01010010 commented Oct 19, 2018

Believe it or not I sometimes log throwing an Error. Also doing node --inspect node_modules/.bin/jest --runInBand mypath/to/my.test.js shows the real console.log outputs in my case.

Node v9.11.1
Jest: v23.6.0

Jest config in package.json:

  "jest": {
    "preset": "react-native",
    "verbose": true,
    "testEnvironment": "node",
    "haste": {
      "defaultPlatform": "android",
      "platforms": [
        "android",
        "ios",
        "native"
      ],
      "providesModuleNodeModules": [
        "react-native"
      ]
    },
    "setupFiles": ["<rootDir>/__tests__/setup"]
  },

@facebook facebook deleted a comment from nothingismagick Oct 19, 2018

@facebook facebook deleted a comment from RohitoOo Oct 19, 2018

@unional

This comment has been minimized.

unional commented Oct 25, 2018

I confirm this issue is with verbose. The issue is the line count calculation is messed up when verbose is on.

If anyone can point me to the place where it prints the message I can take a stab at it.

You can mitigate the issuing using jest-watch-toggle-config for the time being.

Set it up like this:

module.exports = {
  "watchPlugins": [
    ["jest-watch-toggle-config", { "setting": "verbose" }]
  ]
}

When you run your test, press v to enable verbose and then v again to disable it.

After that, as long as you keep verbose off, you can see all logs.

billyvg added a commit to getsentry/sentry that referenced this issue Oct 29, 2018

build(jest): Disable "verbose" to see console logs
If you've been writing frontend tests then I'm sure you've noticed your `console.log` output being gobbled up from stdout. It's due to a bug in jest with `verbose` mode enabled. I think this is a good temporary solution since I've seen many complaints about `console.log` output.

See this thread for more information: facebook/jest#2441

billyvg added a commit to getsentry/sentry that referenced this issue Oct 30, 2018

build(jest): Disable "verbose" to see console logs (#10307)
If you've been writing frontend tests then I'm sure you've noticed your `console.log` output being gobbled up from stdout. It's due to a bug in jest with `verbose` mode enabled. I think this is a good temporary solution since I've seen many complaints about `console.log` output.

See this thread for more information: facebook/jest#2441
@benkermode

This comment has been minimized.

benkermode commented Nov 19, 2018

--verbose=false fixed the issue for me when testing enzyme shallow. The console.logs were, though, working without the fix for enzyme mount.

@ljharb

This comment has been minimized.

Contributor

ljharb commented Nov 20, 2018

That suggests a difference between how the errors are logged by the react-test-renderer, versus how they're logged by react's builtin renderer (since enzyme doesn't mess with console functions at all)

@spion

This comment has been minimized.

spion commented Nov 21, 2018

FYI this PR fixes the problem for me - #6871

Its not ready yet, it might need a change in the approach - but it does display all logged data in verbose mode.

patricksmithdds added a commit to dod-ccpo/atst that referenced this issue Nov 27, 2018

Run tests in watch mode without cache
Running in watch mode was not printing out console.log.
facebook/jest#2441 suggests setting --no-cache
and that fixed it.
@Vadorequest

This comment has been minimized.

Vadorequest commented Dec 4, 2018

Haven't updated yet (running jest 22.4.2) but using the watch mode my logs wouldn't show. Running with --runInBand fixed it in watch mode.

    "test": "jest --watch --runInBand",
    "test:once": "jest",

KingHenne added a commit to sinnerschrader/feature-hub that referenced this issue Dec 8, 2018

fix(integration-tests): do not emit declaration files (to memory-fs)
First of all, we don't need them. And secondly, this reduces the webpack
output when running the tests.

I would have liked to also reduce the stats that are printed, but because
of the following bug, only the one of the two bundled files is printed:
facebook/jest#2441
Jest PR with a fix: facebook/jest#6871

When the PR is merged, we could use this stats config for example:

```json
{
  all: false,
  assets: true,
  colors: true,
  timings: true
}
```

KingHenne added a commit to sinnerschrader/feature-hub that referenced this issue Dec 10, 2018

fix(integration-tests): do not emit declaration files (to memory-fs) (#…
…160)

First of all, we don't need them. And secondly, this reduces the webpack
output when running the tests.

I would have liked to also reduce the stats that are printed, but because
of the following bug, only the one of the two bundled files is printed:
facebook/jest#2441
Jest PR with a fix: facebook/jest#6871

When the PR is merged, we could use this stats config for example:

```json
{
  all: false,
  assets: true,
  colors: true,
  timings: true
}
```
@aymericbouzy

This comment has been minimized.

Contributor

aymericbouzy commented Dec 10, 2018

I have not updated either (Jest 23.6.0), and running with "jest --watch --verbose=false" fixes it for me too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment