Currently Jest eats up all console.log/warn/error messages during debugging and I'm completely blind all the way through up until the test finishes.
I'd like to be able to enable Jest to spit out console.log messages immediately during debugging sessions.
Being able to immediately see all console.log/warn/error messages while debugging unit tests in the debugger would enable all of us to observe the app behavior interactively.
Jest was doing this the other day for me and then it randomly stopped immediately printing logs. I know this because I'm trying out using jest for some simple data tests that do 500+ http calls. The first few tests I wrote it was printing the logs as it went. Now it waits until the test is complete. As far as I know I didn't change any config in between so I really don't understand why it changed.
Furthermore, Jest doesn't seem to output messages at all if the test fails, which makes debugging broken tests quite painful.
it('should do stuff', () => {
console.log('Hello World')
expect(1).toBe(2)
})
expect(received).toBe(expected) // Object.is equality
Expected: 2
Received: 1
33 | it('should do stuff', () => {
34 | console.log('Hello World')
> 35 | expect(1).toBe(2)
| ^
36 | })
There's no "Hello World" to be seen anywhere :frowning_face:
Just to clarify - is this when in watch mode? And which versions of Jest are you using?
I just tried it again in a fresh project with version 24.7.1 and the "Hello World" example _does_ output the message to console. Huh, I guess I must've used an older version or misconfigured something when I tried this before. Anyways, it's working fine for me now.
Edit: can't remember whether I was using watch mode or not when I made my previous comment.
➕1️⃣ This is really crucial for TDD to be able to console.log when test fails. At the moment I have to comment all failing tests out to see the logs and this is pretty inconvenient.
Using Jest 24.8.0.
Jest config:
module.exports = {
transform: {
'^.+\\.ts$': 'ts-jest',
},
testRegex: '.+\\.ts',
moduleFileExtensions: ['ts', 'js'],
rootDir: 'tests',
testEnvironment: "node",
}
Command that is run:
npx jest --runTestsByPath ${pathToTestFile} --colors --verbose=false
+1
As a temporary workaround, you can set DEBUG env variable to jest:
DEBUG=jest yarn test
Are you seeing the same issue if you run with --runInBand?
console.log was not working with v23.0.2. console.log messages are working again with v25.4.0 (current latest). The console.log messages were ONLY appearing when attached to a debugger in v23.0.2
Rather than a feature request, I would call this a serious bug. In the best case, I would consider this a huge overreach by a library to swallow console output. What is the workaround for situations where process exits 1 and needs to print an error? Swallowing it makes issues very expensive to fix in continuous testing suites.
None of the proposals work for me. Running DEBUG=jest jest --forceExit --detectOpenHandles --silent false --verbose false --runInBand via npm run
Is it so hard to fix console.log issues? It's pretty frustrating to waste time because for some reasons jest won't display necessary logs and there is no workarounds about it at all.
Looks like when you run jest via node --inspect node_modules/.bin/jest --runInBand <path_to_test> it logs console.logs to console without connecting to dev tools. More about node --inspect at https://artsy.github.io/blog/2018/08/24/How-to-debug-jest-tests/
Most helpful comment
Furthermore, Jest doesn't seem to output messages at all if the test fails, which makes debugging broken tests quite painful.
There's no "Hello World" to be seen anywhere :frowning_face: