Course contentsShow
← React
Lesson 2149 of 2,39647. Testing Patterns and Best PracticesFree lesson

act() Warnings and What They Mean

Understand act() warnings signal untested state updates; RTL auto-wraps most updates but some async code requires manual handling.

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:

  1. Your test finishes
  2. React updates happen afterward
  3. 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: useEffect callbacks 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.