act() Warnings and What They Mean
What you'll learn: How to interpret and fix those cryptic act() warnings that appear when your tests have untested state updates.
The Warning Message
You've probably seen this scary console warning:
Warning: An update to Component was not wrapped in act(...).
This warning is React's way of telling you: "Hey, your component updated its state after the test finished, and you didn't verify what happened!"
What act() Actually Does
Think of act() like a traffic controller at an intersection. It ensures all React updates (state changes, effects, renders) complete before your test continues. This prevents situations where:
- Your test finishes
- React updates happen afterward
- Your assertions miss important behavior
Why the Warning Appears
The warning signals untested state updates. Common causes:
- Async code without proper waiting: A
setTimeout,Promise, or API call updates state after your test ends - Effects that fire after render:
useEffectcallbacks that weren't awaited - Event handlers that trigger async updates: Clicking a button that fetches data
RTL's Automatic Handling
Good news: React Testing Library auto-wraps most updates in act(). When you use render(), fireEvent, or user-event, RTL handles it for you. You rarely need manual act() calls.
When Manual Handling Is Needed
Some async patterns fall outside RTL's automatic coverage:
// Manual act() needed for raw timers
await act(async () => {
await vi.advanceTimersByTimeAsync(1000);
});
The fix: Use waitFor() or findBy queries instead of manual act() calls. They're designed for async updates and make tests more readable.
Key Takeaway: act() warnings mean state updated after your test stopped watching—use waitFor, findBy, or proper async patterns to ensure all updates are tested and verified.