silenceMessage does not work when shouldFailOnError=false
Hi, I wanted to use jest-fail-on-console as a silencer for the error logs that come from the third-party libraries but still allow errors and warnings to appear in logs and not break tests. I tried this setup:
failOnConsole({
shouldFailOnError: false,
shouldFailOnWarn: false,
silenceMessage: (errorMessage) => {
if (
/Warning: The current testing environment is not configured to support act/.test(
errorMessage
) ||
/Support for defaultProps will be removed from function components in a future major release/.test(
errorMessage
)
) {
return true;
}
return false;
}
});
However, the selected messages were not silenced unless I set shouldFailOnError to true but then other tests started failing on other console.errors.
Is this expected behavior?
The interceptors are indeed only setup on each console method if the corresponding shouldFailOnXxx is true.
If you only want to silence some messages, you can simply do
const originalConsoleError = console.error
jest.spyOn(console, 'error').mockImplementation((errorMessage) => {
if (
/Warning: The current testing environment is not configured to support act/.test(
errorMessage
) ||
/Support for defaultProps will be removed from function components in a future major release/.test(
errorMessage
)
) {
return;
}
originalConsoleError(errorMessage)
})
in a beforeEach
I would have expected the behaviour the @mstangret expected as well.
If one would like to use this to just suppress some noisy console spam (eg. we have a codebase which has some legacy Enzyme tests, and it spits out lots of logging about ReactDOM deprecations, while we work to remove them progressively) it'd be great to have the ability to set errors to not necessarily fail tests, but also have some messages be suppressible.
The idea of this library is that we want to prevent new errors to be added to the logs.
If you have known errors that you are progressively removing (Enzyme's), you can add a list of them to silenceError.