react testing_error ai_generated true

Warning: An update to Component inside a test was not wrapped in act(...)

ID: react/react-act-warning

Also available as: JSON · Markdown
87%Fix Rate
91%Confidence
180Evidence
2022-03-01First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
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().

generic

Workarounds

  1. 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

  2. 90% success Use userEvent from @testing-library/user-event which wraps interactions in act() automatically
    const user = userEvent.setup(); await user.click(button);

    Sources: https://testing-library.com/docs/user-event/intro

Dead Ends

Common approaches that don't work:

  1. 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

  2. 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

Leads to:
Preceded by:
Frequently confused with: