Fake timers in a Jest and Testing Library test never fire, or the test hangs
Fix it fast
Most common cause, 1 of 3user-event 14 is set up without advanceTimers
Give user-event the function that advances Jest's clock.
import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
beforeEach(() => {
jest.useFakeTimers();
});
test('saves the draft', async () => {
const user = userEvent.setup({ advanceTimers: jest.advanceTimersByTime });
render(<Editor />);
await user.type(screen.getByRole('textbox'), 'hello');
expect(screen.getByText('Draft saved')).toBeInTheDocument();
});Short answer
jest.useFakeTimers() looks broken in a React Testing Library test for three reasons. user-event 14 waits on timers nobody advances, so pass userEvent.setup({ advanceTimers: jest.advanceTimersByTime }). Code that awaits a promise around its timer needs await jest.advanceTimersByTimeAsync(). And pending timers have to run before useRealTimers() in afterEach. Applies to Jest 30 and user-event 14.
Which cause do you have?
List, for the failing test file, where fake timers are switched on, how user-event is created and how the clock is moved. Whether the setup call passes advanceTimers, and whether the async advance functions appear at all, already points at one cause.
grep -rnE "useFakeTimers|advanceTimers|userEvent|runOnlyPendingTimers|useRealTimers" src --include="*.test.*"Then find the line you see in this list and open its fix:
- 1user-event 14 is set up without advanceTimers
The test stops at an awaited user.type() or user.click() and Jest reports Exceeded timeout of 5000 ms - 2A promise sits between the timer and the update
The clock is advanced, but the timer was created after an await (a fetch, a resolved mock), so its callback has not run when the assertion executes - 3Fake timers leak past the test, with timers still pending
The test passes on its own (test.only) but fails, or later tests hang, when the whole file runs
What does the error look like?
Exceeded timeout of 5000 ms for a test.Add a timeout value to this test to increase the timeout, if this is a long-running test. See https://jestjs.io/docs/api#testname-fn-timeout.The setTimeout callback never runs after jest.advanceTimersByTime()
Cause 1 of 3
user-event 14 is set up without advanceTimers
You seeThe test stops at an awaited user.type() or user.click() and Jest reports Exceeded timeout of 5000 ms
Check
With jest.useFakeTimers() on, a test that awaits a user-event 14 call runs into the 5000 ms timeout because user-event waits between some inputs with setTimeout, and under fake timers that wait ends only when something moves the clock. Testing Library's fake timer guide names this pairing as a source of test timeouts.
grep -rn "userEvent.setup" src --include="*.test.*"Fix
Give user-event the function that advances Jest's clock. Its docs advise against the older delay: null workaround, which they say can cause unexpected behaviour.
import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
beforeEach(() => {
jest.useFakeTimers();
});
test('saves the draft', async () => {
const user = userEvent.setup({ advanceTimers: jest.advanceTimersByTime });
render(<Editor />);
await user.type(screen.getByRole('textbox'), 'hello');
expect(screen.getByText('Draft saved')).toBeInTheDocument();
});Verify
The test completes in well under the timeout, and removing the advanceTimers option makes it hang again.
npx jest Editor.testConfirmed by testing-library.com
Cause 2 of 3
A promise sits between the timer and the update
You seeThe clock is advanced, but the timer was created after an await (a fetch, a resolved mock), so its callback has not run when the assertion executes
Check
Fake timers that seem not to fire are often never scheduled in time: jest.advanceTimersByTime executes only macro tasks from setTimeout, setInterval and setImmediate, so when the component awaits a promise and only then calls setTimeout, that timer does not exist yet at the moment the clock moves.
grep -rn "advanceTimersByTime(" src --include="*.test.*"Fix
Use the async variant, which lets scheduled promise callbacks run before the timers, and wrap it in an awaited act() so React applies the resulting state update.
import { act } from 'react';
await act(async () => {
await jest.advanceTimersByTimeAsync(1000);
});
expect(screen.getByText('Saved')).toBeInTheDocument();Verify
The assertion right after the advance passes without a waitFor around it.
npx jest Editor.testConfirmed by jestjs.io
Cause 3 of 3
Fake timers leak past the test, with timers still pending
You seeThe test passes on its own (test.only) but fails, or later tests hang, when the whole file runs
Check
Jest's fake timers are file-wide: after jest.useFakeTimers() every following test in that file keeps them until jest.useRealTimers() is called, and timers still queued at the switch never execute. Testing Library's guide asks you to run the pending timers first and then restore the real ones, so that cleanup code and later tests get the timers they expect.
grep -rn -A3 "afterEach" src --include="*.test.*"Fix
Pair the setup with a cleanup that flushes and restores in the same describe block.
beforeEach(() => {
jest.useFakeTimers();
});
afterEach(() => {
jest.runOnlyPendingTimers();
jest.useRealTimers();
});Verify
Run the full file several times; the results no longer depend on test order.
npx jest Editor.testConfirmed by testing-library.com
How do I stop it coming back?
- Wrap userEvent.setup in one test helper that passes advanceTimers whenever fake timers are on, and import that helper in every test file.
- Prefer the Async advance functions in components that mix promises and timers; the docs note runAllTimersAsync is missing under legacyFakeTimers.
- Keep useFakeTimers in beforeEach and the runOnlyPendingTimers plus useRealTimers pair in afterEach, never inside a single test body.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine on the same stack: TaxiDex, QistLock, Taskvare.