Jest: Ability to provide a custom console handler to jest

Created on 7 Jan 2018  路  7Comments  路  Source: facebook/jest

Do you want to request a _feature_ or report a _bug_?
This is a feature request

What is the current behavior?
Currently jest prints the console.log() messages to the console

What is the expected behavior?
This feature would allow providing a custom logger via a CLI flag and the all the console.log() message would be given to the custom logger so it can decide what to do with it.

This would allow tools that are built on top of jest to handle the console logs the way they want.

Most helpful comment

For my use-case, overriding in a setup file is good enough. Thank you, all! Closing this for now.

All 7 comments

You can do this using a setupFile, is that not sufficient?

If I understand correctly, setupFiles should be configured in the config and cannot be passed as CLI args which means the config has to be modified by the tool. Also came across https://github.com/facebook/jest/issues/1022 but not sure how this was handled.

setupFiles can be specified through the CLI.

Great. I'll give it a try. @SimenB proposed this idea but i'm not sure what he had in mind.

My thought was to pass in a custom TestConsole implementation which could do whatever.

See https://github.com/facebook/jest/blob/b86d93263b378c7ea02a8c64efd45c74f2538335/packages/jest-runner/src/run_test.js#L88-L94

So the first condition could be globalConfig.console or something.

But if just overwriting console.{warn,log,info,error} in a setup file covers your use case, I suppose that's good enough?

For my use-case, overriding in a setup file is good enough. Thank you, all! Closing this for now.

Setup file overwriting breaks the --silent option. Passing a custom console through the config as @SimenB proposes is a good idea.

Was this page helpful?
0 / 5 - 0 ratings