An update to ... inside a test was not wrapped in act(...)
React JavaScript Library
Severity: MinorWhat it means
React, in tests: An update to Counter inside a test was not wrapped in act(...). When testing, code that causes React state updates should be wrapped into act(...).
React's docs: act is a test helper that applies pending React updates before you make assertions.
Affected Models
- React 16.8 and later
How to Fix It
-
Rendering or firing events yourself? Wrap them: await act(async () => { root.render(<Counter />); }). React's docs recommend the async form with await.
-
Using React Testing Library? React's docs: its helpers are already wrapped in act. The warning then usually means something updated later, such as data arriving after a fetch. Wait for the result, for example with await screen.findByText('Loaded').
-
Fake timers? Advance them inside act: act(() => { jest.advanceTimersByTime(1000); }).