CodePark/ questions / Async UI & act()
START PRACTICINGPRACTICE
Frontend question · Testing › Async UI & act()

Your test passes but prints 'Warning: An update to Profile inside a test was not wrapped in act(...)'. What does it mean and how do you fix it properly?

The warning means React saw a state update that your test didn't wait for. Fix it by waiting for the result of that update.

RTL (React Testing Library) already wraps render, user-event and its async utilities in act. act is React's helper that applies updates before you assert. So the warning usually means something async finished after your last await.

A typical case:

  • The test asserts the loading state and ends.
  • Then the fetch resolves and calls setState while the test is tearing down.
  • Your test never checked the final UI, so it may hide a bug.

The proper fix is to wait for the result of that update:

  • await screen.findBy* the loaded content.
  • Or await waitFor(...) the state you expect.
  • For timers, advance fake timers inside act, or use the advanceTimers option of user-event.

Wrapping random code in act to silence the warning is the wrong fix. If the component does work you don't care about in this test, mock that work or let it finish anyway.

  • The warning means a state update happened after the point the test waited for.
  • RTL already wraps render, events and its async utilities in act.
  • Fix it by awaiting the UI change with findBy* or waitFor.
  • Don't add act everywhere just to silence the warning.
  • Common causes are unresolved fetches, timers and effects after the last assertion.