Description
react-hooks-testing-library
version: v5.0.3
Reproduction:
In a blank project, run:
npm install react-test-renderer @testing-library/react-hooks --save-dev
In test.js
:
const { renderHook } = require('@testing-library/react-hooks');
function useExample() {
throw new Error('Message.');
}
renderHook(() => useExample());
Run:
node test.js
Notice the unexpected console error output in the terminal:
The above error occurred in the <TestComponent> component:
at TestComponent (/[redacted]/node_modules/@testing-library/react-hooks/lib/helpers/createTestHarness.js:20:5)
at Suspense
at ErrorBoundary (/[redacted]/node_modules/react-error-boundary/dist/react-error-boundary.cjs.js:40:35)
React will try to recreate this component tree from scratch using the error boundary you provided, ErrorBoundary.
Problem description:
The documentation says:
To keep test output clean, we patch
console.error
when importing from@testing-library/react-hooks
(or any of the other non-pure imports) to filter out the unnecessary logging and restore the original version during cleanup.
— https://github.com/testing-library/react-hooks-testing-library/blob/master/docs/api-reference.md#consoleerror
In truth, patching only occurs if certain beforeEach
and afterEach
globals, arbitrary to specific testing frameworks, are present:
react-hooks-testing-library/src/core/console.ts
Lines 6 to 7 in c6de60e
Presumably it's assumed that every project in the world uses Jest 🙃
In another part of the docs:
In order to run tests, you will probably want to be using a test framework. If you have not already got one, we recommend using Jest, but this library should work without issues with any of the alternatives.
— https://github.com/testing-library/react-hooks-testing-library/blob/master/docs/installation.md#testing-framework
“this library should work without issues with any of the alternatives” is not accurate. Plenty of testing frameworks don't mess with globals at all, while some do, but not in the same way as Jest. I use test-director
to test my published packages (55.9 kB install size vs 34.4 MB for jest
). Sometimes it makes sense to avoid a "framework" altogether and just use a vanilla Node.js script with the Node.js assert
API.
Suggested solutions:
-
Do nothing, except update all the documentation to explain specifically what globals need to present for the automatic console error suppression to work, and ideally list what testing frameworks do or don't provide these globals.
This is not great because it doesn't provide an elegant solution for users with a test environment that doesn't provide these globals; they would be forced to either accept the console noise or hackily define
beforeEach
andafterEach
globals in test scripts just forreact-hooks-testing-library
to use. It smells to have to copy the globals of an arbitrary testing framework not used in your codebase. The people that specifically chose a testing library that doesn't pollute the global namespace will be opposed to a globals based hack. From a code quality / maintenance perspective, it would not be intuitive without elaborate comments whatbeforeEach
andafterEach
refers to or why that workaround code is there. -
Support console error output suppression for any testing environment. Export functions to monkeypatch and restore the global
console
, available via the index and deep imports. Internally, these same functions could be used with the automaticbeforeEach
andafterEach
globals detection.