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))

Explanation

Start the promise, then await vi.advanceTimersByTimeAsync(1000), then await the promise - async variants also flush microtasks.

Further reading

Problem with a question

Testing · Senior · question 33

Test yourself

Take the Testing · Senior quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz