Testing · Senior · Question 33 of 75
Why can jest.useFakeTimers() hang a test that awaits a promise using setTimeout?
vi.useFakeTimers();
await sleep(1000); // inside: new Promise(r => setTimeout(r, 1000))- ✓Correct answer: The timer never fires until time is advanced, but the test is blocked on
awaitand can't advance it - 2Fake timers break promises
- 3
awaitis not allowed with fake timers
Explanation
Start the promise, then await vi.advanceTimersByTimeAsync(1000), then await the promise - async variants also flush microtasks.