Closed
Description
react-hooks-testing-library
version: 3.7.0react-test-renderer
version: react-test-renderer@16.13.0react
version: 16.13.1node
version: v12.13.1
Relevant code or config:
Example of one of our Next.js mocks
...
import { act } from '@testing-library/react';
export const useRouter = () => {
const [router, setRouter] = React.useState(fakeRouterInstance);
// Mock the hook triggering a state change when there's any router state change.
React.useEffect(() => {
const update = () => {
act(() => {
setRouter({ ...fakeRouterInstance });
});
};
fakeRouterInstance.events.on('beforeHistoryChange', update);
return () => {
fakeRouterInstance.events.off('beforeHistoryChange', update);
};
}, []);
return router;
};
What you did:
The act()
from react-testing-library and the one from the hook testing library are mismatching. This makes it hard to create global mocks (with for examples jest __mocks__
folder) with which to replace APIs who're not playing well with test environment.
What happened:
Warning: It looks like you're using the wrong act() around your test interactions.
Be sure to use the matching version of act() corresponding to your renderer:
// for react-dom:
import {act} from 'react-dom/test-utils';
// ...
act(() => ...);
// for react-test-renderer:
import TestRenderer from 'react-test-renderer';
const {act} = TestRenderer;
// ...
act(() => ...);
in TestHook
in Suspense
Suggested solution:
I understand the 2 libraries are using a different react test tool. But I wonder if it'd be possible to unify the testing-library toolchain for react to run with the same environment? This would make it much easier to combine both library tool chain in the same application code.
If you know work around to detect which act should be use when (programmatically), this could also be good to document.