The act() Testing Utility and Why It Matters
Understand what act() does under the hood and why React testing utilities require wrapping state updates in it.
act() is a testing utility that ensures all state updates, effects, and their resulting re-renders triggered within its callback are fully processed and flushed before your test proceeds to make assertions — mirroring how React batches and processes updates in a real browser environment inside event handlers. Testing Library's own utilities (render, fireEvent, userEvent) already wrap their internal operations in act() for you, which is why most everyday component tests don't need to call it explicitly.
act() is like insisting a chef fully plate and finish a dish (flush all pending updates and re-renders) before letting a food critic (your test assertion) taste and review it, rather than letting the critic take a bite mid-preparation and unfairly judge an unfinished plate.