react
testing_error
ai_generated
true
Warning: An update to Component inside a test was not wrapped in act(...)
ID: react/react-act-warning
87%Fix Rate
91%Confidence
180Evidence
2022-03-01First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 18 | active | — | — | — |
Root Cause
State update in test not wrapped in act(). React 18 is stricter about batching updates in tests. All state-changing interactions must be wrapped in act().
genericWorkarounds
-
92% success Wrap state-triggering interactions in act() or use React Testing Library's async utilities
await act(async () => { fireEvent.click(button); }); or use await screen.findByText('result');Sources: https://react.dev/reference/react/act https://testing-library.com/docs/react-testing-library/api/#act
-
90% success Use userEvent from @testing-library/user-event which wraps interactions in act() automatically
const user = userEvent.setup(); await user.click(button);
Dead Ends
Common approaches that don't work:
-
Suppress the warning by mocking console.error
80% fail
Hides real issues; the test may pass but the component behavior is unreliable and updates may be lost
-
Add arbitrary waitFor or setTimeout calls without act()
70% fail
Timing-based fixes are flaky and do not guarantee React finishes processing updates
Error Chain
Preceded by:
Frequently confused with: