Skip to content
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 emit output #3853

Closed
iffy opened this issue Jun 19, 2017 · 138 comments
Closed

console.log does not emit output #3853

iffy opened this issue Jun 19, 2017 · 138 comments

Comments

@iffy
Copy link

iffy commented Jun 19, 2017

Please try out Jest 24 if you're having issues with missing console.log output


Branching from Issue #2441

@cpojer I'm not seeing any console.log output with this setup (macOS):

$ node --version
v7.4.0

Files

package.json:

{
  "dependencies": {
    "@types/jest": "19.2.4",
    "jest": "20.0.4",
    "ts-jest": "20.0.6",
    "typescript": "2.3.4"
  }
}

__tests__/jestconfig.json:

{
  "rootDir": "../",
  "globals": {
    "__TS_CONFIG__": {}
      
  },
  "moduleFileExtensions": [
    "ts",
    "tsx",
    "js",
    "jsx",
    "json"
  ],
  "transform": {
    "\\.(ts|tsx)$": "<rootDir>/node_modules/ts-jest/preprocessor.js"
  },
  "testRegex": "__tests__/.*test_.*\\.(ts|tsx|js)$"

__tests__/test_foo.ts:

import {} from 'jest';

console.log('CONSOLE before test');
test('fail', () => {
  console.log('CONSOLE inside test');
  expect(true).toEqual(false);
  console.log('CONSOLE end of test');
})

__tests__/test_bar.js:

console.log('BAR CONSOLE before test');
test('fail', () => {
  console.log('BAR CONSOLE inside test');
  expect(true).toEqual(false);
  console.log('BAR CONSOLE end of test');
})

Output

$ jest -c __tests__/jestconfig.json 
 FAIL  __tests__/test_foo.ts
  ● fail

    expect(received).toEqual(expected)
    
    Expected value to equal:
      false
    Received:
      true
      
      at Object.<anonymous> (__tests__/test_foo.ts:6:16)
      at Promise.resolve.then.el (node_modules/p-map/index.js:42:16)

 FAIL  __tests__/test_bar.js
  ● fail

    expect(received).toEqual(expected)
    
    Expected value to equal:
      false
    Received:
      true
      
      at Object.<anonymous>.test (__tests__/test_bar.js:4:16)
      at Promise.resolve.then.el (node_modules/p-map/index.js:42:16)

Test Suites: 2 failed, 2 total
Tests:       2 failed, 2 total
Snapshots:   0 total
Time:        1.379s
Ran all test suites.

Single JS test:

$ jest -c __tests__/jestconfig.json __tests__/test_bar.js 
 FAIL  __tests__/test_bar.js
  ● fail

    expect(received).toEqual(expected)
    
    Expected value to equal:
      false
    Received:
      true
      
      at Object.<anonymous>.test (__tests__/test_bar.js:4:16)
      at Promise.resolve.then.el (node_modules/p-map/index.js:42:16)

  ✕ fail (7ms)

Test Suites: 1 failed, 1 total
Tests:       1 failed, 1 total
Snapshots:   0 total
Time:        0.596s, estimated 1s
Ran all test suites matching "__tests__/test_bar.js".

Single TS test:

$ jest -c __tests__/jestconfig.json __tests__/test_foo.ts 
 FAIL  __tests__/test_foo.ts
  ● fail

    expect(received).toEqual(expected)
    
    Expected value to equal:
      false
    Received:
      true
      
      at Object.<anonymous> (__tests__/test_foo.ts:6:16)
      at Promise.resolve.then.el (node_modules/p-map/index.js:42:16)

  ✕ fail (116ms)

Test Suites: 1 failed, 1 total
Tests:       1 failed, 1 total
Snapshots:   0 total
Time:        1.27s
Ran all test suites matching "__tests__/test_foo.ts".
@rogeliog
Copy link
Contributor

@thymikee Any idea why this is happening?

@thymikee
Copy link
Collaborator

No idea, but I'd try to minimize the example, e.g. by removing typescript

@rogeliog
Copy link
Contributor

I'm able to repro without Typescript

@thymikee
Copy link
Collaborator

Can confirm Node 7.4 eats console logs, but it works on Node 7.5.0, 7.10.0, 8.0.0 and 8.1.2.
Please upgrade your Node version. Closing

@kimjoar
Copy link
Contributor

kimjoar commented Jun 19, 2017

@thymikee What about Node 6, which is the current LTS release? (Looks like I'm hitting this too, though haven't debugged it enough yet. However, I'm limited to LTS releases for now, so can't upgrade).

@thymikee
Copy link
Collaborator

Tested on v6.11.0, still shows console.logs.

@kimjoar
Copy link
Contributor

kimjoar commented Jun 19, 2017

Ah, I might be hitting some other problem. Thanks, @thymikee, I'll go back to debugging it a bit more.

@austinknight
Copy link

I was running Node 7.3.0 unknowingly (wrong version via n) and it wasn't logging. Switched to 8.1.1 and it logged again.

@fi0rini
Copy link

fi0rini commented Jul 10, 2017

@thymikee - I don't see how that is the solution? ...I am not able to upgrade my node version

@jasmouth
Copy link

I guess I'm a little confused how this would be considered a Node bug and not at least have something to do with Jest itself. Using Node v7.4.0, with Jest v19.0.2 I see console logging from my tests. Simply updating Jest to v20.0.4 (without making any changes to any other configuration) causes console logging to no longer appear. Is there something that I'm missing?

@nowherenone
Copy link

@thymikee So, I've upgraded my node version and I see no console logs on Node 8.2.1 Winx64 + Jest 20.0.4. I have to use a fallback for now

  console.log = s => {
    process.stdout.write(s + "\n");
  };

and I'm pretty sure it should be fixed in Jest, since previous versions didn't have this issue.

@thymikee
Copy link
Collaborator

@nowherenone can you test if it happens on latest alpha release jest@test?

@marvinhagemeister
Copy link
Contributor

@thymikee Just tried with jest@test on node 8.2.1 but same result. console.log statements are always swallowed.

@nowherenone
Copy link

@thymikee The problem is in the BufferedConsole module, where the Console constructor parameters don't match with the expected ones.

I've opened a PR, maybe it would help: #4157

@cpojer
Copy link
Member

cpojer commented Aug 24, 2017

I think the problem here is that Jest buffers messages but there is something that causes to bail out (or an infinite loop etc.). You'll need to use --verbose to print the messages directly to the output stream.

@fi0rini
Copy link

fi0rini commented Aug 24, 2017

This is ridiculous - I am triggered by the amount of useless responses from @cpojer (in every issue and PR I go to) and trying to put everything on everyone else like somehow we aren't smart enough.

Don't use Jest - that's my answer if you're wondering. Find a new testing framework.

describe('index', () => {
  it('doesnt print anything', () => {
    console.log('Hellllooo');
    expect(true).toBe(true);
  });
});
$ yarn test -- src/__tests__/index.spec.js --verbose
yarn test v0.27.5
warning package.json: No license field
$ jest "src/__tests__/index.spec.js" "--verbose"
 PASS  src/__tests__/index.spec.js
  Index
    ✓ doesnt print anything (2ms)

Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        0.969s, estimated 2s
$ jest "--version"
v20.0.4
$ node --version
v7.4.0

I am sure I'll just be told to install a newer version of Node Lmao - what a joke 😂 😂 😂

@thymikee
Copy link
Collaborator

thymikee commented Aug 24, 2017

@nf071590 cannot repro, works on repl.it with exactly the same Jest and Node versions: https://repl.it/KYLc/0

Please come up with a serious repro before ranting about project maintainers.
Cheers!

screen shot 2017-08-24 at 17 39 59

@cpojer
Copy link
Member

cpojer commented Aug 24, 2017

Not sure what to do about this. I literally pasted your example into a file, and ran Jest, and it works as advertised on my Mac, with the latest master version of Jest as of a minute ago. See:

screen shot 2017-08-24 at 4 38 11 pm

@ansballard
Copy link

Throwing in since I'm on windows (node 8.4, jest 21.0.0-alpha.2), console.logs are sometimes hidden if you don't use --verbose, but seem to show consistently with it. Happy to update with results using node 7 and stable jest when I get a minute.

@xild
Copy link

xild commented Sep 21, 2017

The same problem here.

describe('index', () => {
  it('doesnt print anything', () => {
    console.log('Hellllooo');
    expect(true).toBe(true);
  });
});
$ node --version
v7.4.0
$ jest "--version"
v21.1.0

screen shot 2017-09-21 at 14 34 32

@dkushner
Copy link

Currently encountering same issue on node v8.7.0 (npm v5.4.2).

@colinclarkbcgdv
Copy link

This is still an issue

@SimenB
Copy link
Member

SimenB commented Oct 24, 2017

Can you provide a reproduction?

@Rosensweig
Copy link

Rosensweig commented Oct 26, 2017

FWIW, I ended up here because I was having the exact same issue. Node v7.4.0. Upgraded my Node version, and console.log prints as expected now, even without --verbose. V7.4.0 may not be the only version to have this issue, but it does seem to be version-related, and a non-issue for some Node versions. I'm now on Node v8.3.0, which seems to work.

That said, prior to upgrade I had the same versions as @nf071590 , and the same problems. I'm not sure why it doesn't happen on repl.it, but it's not some weird thing only happening on his computer.

@MaxDesiatov
Copy link

MaxDesiatov commented Nov 7, 2017

I can reproduce this with node 8.9.0 and jest 21.2.1, macOS 10.12.6 (16G1036)

@tiborsaas
Copy link

tiborsaas commented Jan 27, 2019

Yeah, got confused with it, sorry. Thx @tobyhinloopen

const lol = await Promise.all(versions); worked as expected.

@SimenB
Copy link
Member

SimenB commented Jan 27, 2019

In that case, the log statements might be lost since the function is never called, but you should still see log statements in the original forEach case since node will wait for the promises to complete before exiting the process. So it's still a bug, even though it's a user error

@SimenB
Copy link
Member

SimenB commented Jan 27, 2019

PR: #7731

@tnguyen14
Copy link

I use https://www.npmjs.com/package/debug to do logging. Would that work with Jest?

@SimenB
Copy link
Member

SimenB commented Jan 28, 2019

No, not until we do #6524.

I'd recommend mocking debug in your tests and using console.log if you wanna se them

@jeysal
Copy link
Contributor

jeysal commented Jan 29, 2019

For reference, console messages can get lost if they are printed after the tests have completed, see the comments at the bottom of #7731. This might be the reason for some, but probably not all of the missing console messages reported here.

@ethanstrominger
Copy link

The --verbose worked for me. Before using --verbose, some, but not all messages were lost. I am using node v10.15.3 and jest v21.2.1

@ShetlandJ
Copy link

Still a problem for me, console logs don't show in Jest

@hosseinalipour
Copy link

despite the latest announcement in the blog that the problem has been finally solved, the problem still exists, my console logs sometimes do not show up again, I'm using jest v24.8.0.

@thymikee
Copy link
Collaborator

And my works just fine 🤷‍♂️. Please post a reproduction we can investigate. We're not wizards, we can't see your code that's failing to log output.

@hosseinalipour
Copy link

actually, after investigation, logs related to API testing(like supertest) doesn't work. expected :/

@kresli
Copy link

kresli commented May 23, 2019

@thymikee This is happening inconsistently so its really hard to reproduce it. example:
following runs by selecting single file from tests ( option p )

  • console.log('pantz') (works)
  • change console.log(myObject) (not working)
  • change it to fit (not working)
  • change to console.log('pantz') (not working)
  • change fit to it (not working)
  • restart jest (not working)
  • change it to fit (working)

Now I tried to replicate same steps and the result is inconsistent.

@tobyhinloopen
Copy link

tobyhinloopen commented May 23, 2019

Provide a sample repo where it isn't working so that the JEST guys can help debug this. @kresli

There is plenty of user-error possibilities as well, mostly missing await or otherwise async console.log's.

@kresli
Copy link

kresli commented May 23, 2019

I'll try find a time to reproduce this then. Until then I found my logs are eaten by results as you can see from bellow. Before FAIL appear you can see my 2 logs there. And also its worth to mention only 2 logs are removed. If I add 10 logs under each other, 8 would be shown. I think that's a good start :)

ezgif com-gif-maker

@tobyhinloopen
Copy link

tobyhinloopen commented May 23, 2019

Meantime, if you need a catch-all fix that just works, you can use something like winston to log to both file and console. Even if your console messages don't show, they are written to file.

With winston you can configure what you want logged where and it supports multiple transports, and transports you can implement yourself.

const logger = winston.createLogger({
  level: "verbose",
  format: winston.format.json(),
  defaultMeta: {},
  transports: [
    new winston.transports.Console(),
    new winston.transports.File({ filename: "combined.log" }),
  ],
});

logger.error(stuff)

Maybe you can even do something dirty like overriding console.* globally to winston's logger.

@spion
Copy link
Contributor

spion commented May 23, 2019

@kresli what version is your jest? That looks like v23 behavior

@yaelz
Copy link

yaelz commented Aug 5, 2019

This keeps happening to me with jest v^24.6.0 and node v10.14.2. Any idea why?

@tobyhinloopen
Copy link

@yaelz This is a stubbern issue that is commonly caused by user error but also has a history of hard to reproduce bugs.

I think it would really help the contributors to provide a reproducable case by providing an example repo or to provide another way to reproduce the issue.

@yaelz
Copy link

yaelz commented Aug 5, 2019

Thanks for the quick response!
That'll be hard because the current repo is private in my org... I'll let you know if I get to it :)

@normancarcamo
Copy link

normancarcamo commented Aug 5, 2019

This keeps happening to me with jest v^24.6.0 and node v10.14.2. Any idea why?

I recently upgrade some dependencies in a project and I don't have any issue, I face this one months ago, but it was solved in newer versions I believe...

@yaelz
Copy link

yaelz commented Aug 5, 2019 via email

@normancarcamo
Copy link

normancarcamo commented Aug 6, 2019

Can you please share the versions you're using now, when it's solved?

Sure:

jest

^24.8.0

node -v

v10.16.0

@normancarcamo
Copy link

And this are some npm scripts that I use to run both, e2e, integration, unit and acceptance:

"scripts": {
  "test": "NODE_ENV=test npm run test:unit && npm run test:integration:both",
  "test:unit": "NODE_ENV=test jest --config test/jest.config.unit.js --detectOpenHandles",
  "test:integration": "NODE_ENV=test MOCK=false jest --config test/jest.config.integration.js --runInBand --detectOpenHandles",
  "test:integration:mock": "NODE_ENV=test MOCK=true jest --config test/jest.config.integration.js --runInBand --detectOpenHandles",
  "test:integration:both": "NODE_ENV=test npm run test:integration:mock -- --coverage; npm run db:migration:test; npm run test:integration -- --coverage;",
  "test:report": "open docs/test/report/index.html",
  "test:report:coverage": "open docs/test/coverage/lcov-report/index.html"
}

An this is the jest.config:

"use strict";

module.exports = {
  "bail": true,
  "verbose": false,
  "collectCoverage": false,
  "expand": true,
  "testURL": "http://localhost:3000/",
  "coverageDirectory": "./docs/test/coverage",
  "testEnvironment": "node",
  "rootDir": "../",
  "setupFilesAfterEnv": [
    "./test/jest.setup.js"
  ],
  "jest.showCoverageOnLoad": true,
  "watchPathIgnorePatterns": ["node_modules"],
  "transform": {
    "^.+\\.js$": "babel-jest",
    '^.+\\.tsx?$': 'ts-jest',
  },
  "reporters": [
    "default",
    ["./node_modules/jest-html-reporter", {
      "pageTitle": "...",
      "outputPath": "./docs/test/report/index.html",
      "includeFailureMsg": true,
      "sort": "titleAsc",
      "dateFormat": "yyyy-mm-dd HH:MM:ss"
    }]
  ]
};

Hope it helps.

magjac added a commit to magjac/graphviz-visual-editor that referenced this issue Jan 2, 2020
magjac added a commit to magjac/graphviz-visual-editor that referenced this issue Jan 2, 2020
@m-denton
Copy link

m-denton commented Jan 22, 2020

I'll try find a time to reproduce this then. Until then I found my logs are eaten by results as you can see from bellow. Before FAIL appear you can see my 2 logs there. And also its worth to mention only 2 logs are removed. If I add 10 logs under each other, 8 would be shown. I think that's a good start :)

ezgif com-gif-maker

@kresli What is the status bar and time output that you have here? When I run my test suite, I see RUN HARNESS test-harness/index.js and nothing else until everything runs. I then see my console.log message at the very end and the line with RUN HARNESS... changes to <FAILED|PASS> HARNESS....

@IevgenRagulin
Copy link

IevgenRagulin commented Jun 12, 2020

Update: please ignore, turned out to be an issue in my code

Still facing this with node v12.16.1, jest 25.5.4, typescript 3.8.3 on MacOS. Tried the recommendations of using --runInBand, disabling/enabling verbose, using --silent=true, it didn't help.

@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 11, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests