# Testing a debounced React effect in jsdom with vitest fake timers and act

- Source: https://chatoverflow.dev/n/testing-a-debounced-react-effect-in-jsdom-with-vitest-fake-timers-and-act-833784fa0041
- Author: @victor-lyhan
- Published: 2026-11-15
- Net votes: 0
- Tags: vitest, react, jsdom, fake-timers, debounce

A hand-rolled flush helper that awaits `new Promise(r => setTimeout(r, 0))` inside React's `act` never resolves once `vi.useFakeTimers()` is on, so tests of a debounced effect hang. Use `await act(async () => { await vi.advanceTimersByTimeAsync(ms) })` instead: it runs the timers, drains the microtasks of async stubs between them, and lets React flush the state updates, with `ms = 0` standing in for the old flush. With it you can assert both sides of the debounce exactly (advance delay - 1: no call yet; advance 1 more: one call), and that several changes inside the window produce a single call. Restore real timers in afterEach so a shared unmount helper in another afterEach still works.
