ValentinH / jest-fail-on-console

Utility to make jest tests fail when console.error() or any other methods are used
MIT License
141 stars 20 forks source link

Is there a way to not fail a test on console.error, but still print? #28

Closed billyvg closed 3 months ago

billyvg commented 1 year ago

I would still like to see the console.error message when it happens, but be able to have the test not fail.

ValentinH commented 1 year ago

What you are looking for might be achievable by using the skipTest option: https://github.com/ValentinH/jest-fail-on-console#skiptest

doug-wade commented 1 year ago

Afaict, there is no way currently to emit the warning, but not fail the test. If you would like to submit a PR, you'd want to take an option, and then use that to determine whether or not to throw here

clemp6r commented 11 months ago

This library could call the original console method, as following:

console[methodName] = (...args) => {
  newMethod(...args);
  originalMethod(...args);
}

That makes sense to me: the goal of this library is to fail the tests under some conditions, not to alter what is printed is the console.

ValentinH commented 11 months ago

I like this idea. We could add this behavior as a new option and make it the default. We would then release a new major version.

vernak2539 commented 5 months ago

I would love this functionality as well.

My use case is that I have multiple tests where console.error is called, but I cannot control when it gets called as the code being executed is outside of my control.

I'd really like to have the option to:

  1. Fail tests if the output of the console[method] call matches on certain text
  2. Output all other console messages (even errors) and not fail

I can potentially submit a PR if you could point me in the right direction

ValentinH commented 5 months ago

I think that @clemp6r proposal is good. We could change the default behavior to let all logs go to the console and only make the test fail.

vernak2539 commented 5 months ago

@ValentinH I've opened https://github.com/ValentinH/jest-fail-on-console/pull/48 and would appreciate any feedback you have