Does Jest swallow console.log statements? Is there a way to change this?

后端 未结 5 1784
闹比i
闹比i 2021-02-11 17:14

Does Jest swallow console.log output?

// __tests__/log.test.js

it(\'logs\', () => {
  console.log(\'hey\') // expect to see \"hey\" printed in t         


        
相关标签:
5条回答
  • 2021-02-11 17:31

    The problem is that I was using jest --forceExit. Jest's logging model saves all the logs and spits them out later. --forceExit causes the process to bail before it reaches the spit-out-logs point.

    0 讨论(0)
  • 2021-02-11 17:49

    I found a work around, despite the fact that sadly neither TERM=dumb, nor verbose: false nor upgrading to version 24 worked (I'm on 24.9). Just spy on the console, and you can see the results. Set it up with this:

    const consoleSpy = jest.spyOn(console, 'log')

    And then view the calls after running your tests using:

    consoleSpy.mock.calls[0][0]

    0 讨论(0)
  • 2021-02-11 17:50

    Try adding --verbose to the Jest arguments.

    This worked for me.

    0 讨论(0)
  • 2021-02-11 17:55

    This seems to be an ongoing issue.

    With Node 10.7.0 and Jest 23.4.1, adding verbose: false to the jest config (per this suggestion) worked for me.

    Edit

    Now that I've gone to Jest 23.6 I also need to pass TERM=dumb as an environment variable, per Tamlyn's answer.

    0 讨论(0)
  • 2021-02-11 17:57

    Update: this issue should be fixed as of Jest 24.

    Another partial solution to the current bug affecting tests in --watch mode is to pass TERM=dumb as an environment variable.

    TERM=dumb jest --watch
    

    This has a small price in that it no longer clears the console before each test run so you have to scroll to see the results.

    0 讨论(0)
提交回复
热议问题