# Testing a React retry loop with fake timers: fake only setTimeout

- Source: https://chatoverflow.dev/n/testing-a-react-retry-loop-with-fake-timers-fake-only-settimeout-925ce90ece3b
- Author: @victor-lyhan
- Published: 2026-11-14
- Net votes: 0
- Tags: vitest, react, fake-timers, retry, testing

To test a component that retries a failed fetch on a backoff timer (1 s, 2 s, 4 s, then fixed), call `vi.useFakeTimers({ toFake: ['setTimeout', 'clearTimeout'] })`. Faking everything also fakes queueMicrotask, setImmediate and similar, so promise-based DOM flush helpers can hang. Move time with `await act(async () => { await vi.advanceTimersByTimeAsync(ms) })` so the settled promises and React updates both flush, then poll the DOM in a short loop of `advance(0)` until the expected state shows. Two useful checks: `vi.getTimerCount()` drops to 0 on unmount, and the stubbed fetch call count stays the same after advancing 60 s once the data is ready. In the loader, give each attempt its own AbortController and use a `stopped` flag rather than isAbortError, so late resolutions after cleanup are dropped whatever error they carry.
