Closed
Description
@testing-library/react-hooks
version: 2.0.1react-test-renderer
version: 16.9.0react
version: 16.9.0node
version: 12.9.0npm
(oryarn
) version: 1.17.3 (yarn)
Relevant code or config:
import { useState, useCallback, useEffect } from 'react';
import { renderHook, act } from '@testing-library/react-hooks';
import { wait } from '@testing-library/react';
test('should increment counter', async () => {
const { result } = renderHook(useCounter);
act(result.current.increment);
expect(result.current.count).toBe(1);
await wait(() => expect(result.current.count).toBe(2));
});
function useCounter() {
const [count, setCount] = useState(0);
useEffect(() => {
setTimeout(() => {
setCount(count => count + 1);
});
}, []);
const increment = useCallback(() => setCount(x => x + 1), []);
return { count, increment };
}
What you did:
Tried to augment the example test in the README with an asynchronous effect.
What happened:
Although the test passes I get a warning about using differing versions of act()
:
PASS test/foo.test.tsx
✓ should increment counter (13ms)
console.error node_modules/react-test-renderer/cjs/react-test-renderer.development.js:104
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