Closed martinfrancois closed 2 years ago
Upon closer investigation, I found the reason for the failing tests, see here: https://react-hooks-testing-library.com/installation#pure-imports
It mentions console.error is patched to hide some React errors
, and in fact if I replace the following import:
import { renderHook } from '@testing-library/react-hooks';
With this import:
import { renderHook } from '@testing-library/react-hooks/pure';
I don't see the error anymore, but also the side effects performed by @testing-library/react-hooks
are not executed.
Is there any way to make jest-fail-on-console
restore the mock if it is patched as it is by @testing-library/react-hooks
, to get rid of the error?
I have no idea though why this error only occurs with Jest 27 and not with Jest 26...
Additional reference that may be useful:
Code used by @testing-library/react-hooks
to patch console.error
Disabling console.error filtering in @testing-library/react-hooks
For anyone also having this issue, we are using the following workaround for now, by changing the jest
config to:
"setupFilesAfterEnv": [
"@testing-library/react-hooks/disable-error-filtering.js",
"./src/testSetupAfterEnv.ts"
]
Thanks for sharing this solution. I actually faced the exact same issue a few weeks ago and solved it by using the /pure
version. On my case, not having the side effect run was not an issue.
The problem is that @testing-library/react-hooks
isn't using a Jest mock (they support other test runners) to patch the console so I don't know how to make an exception for it.
For people who can't use /pure
module - my temporary solution is to mock error implementation in beforeEach
hook in a test file.
beforeEach(() => {
jest.spyOn(console, "error").mockImplementation();
});
My colleague @KevinHerklotz fixed it in a more robust way:
setupFilesAfterEnv: [
'@testing-library/react-hooks/disable-error-filtering.js',
// other setup
]
More infos here: https://react-hooks-testing-library.com/reference/api#consoleerror
Could it be an option for you @martinfrancois?
I think the answers in this issue solved it.
After upgrading to Jest 27, every test which uses the method
renderHook
from@testing-library/react-hooks
fail with this error:We are using a
testSetupAfterEnv.ts
file with the following content:And the following jest v27 config (shortened for brevity):
The config for Jest v26 is the same, except that it doesn't contain
"testEnvironment": "jsdom",
The error occurs with the following dependencies:
The error does NOT occur with the following dependencies: