Test Timeout Exceeded
Error message
Exceeded timeout of 5000 ms for a test
Quick summary
The test didn't finish within the timeout period. This usually means an async operation is hanging or the test is doing too much work.
Why this happens
An async operation never resolves (e.g., waiting for an event that never fires).
An infinite loop in the code under test.
A real network call or database query is too slow.
The default timeout (5 seconds) is too short for the test's work.
Missing await/done causes Jest to wait indefinitely.
Minimal example
test('slow test', () => {
return fetchRealData().then(data => {
expect(data).toBeDefined();
}); // real network call might hang
});test('fast test', () => {
mockFetch.mockResolvedValue({ data: 'test' });
return fetchData().then(data => {
expect(data).toBeDefined();
}); // mock resolves immediately
});Mock the slow dependency so the test doesn't wait for real network calls.
How to diagnose
Check if the test has unawaited async operations or missing done() calls.
Add console.log statements to see how far the test gets before hanging.
Check if the code under test has infinite loops or unresolved promises.
Verify mocks are set up correctly — a mock that doesn't resolve can cause a hang.
How to fix
Fix the underlying issue (resolve promises, call done(), fix infinite loops).
If the test legitimately needs more time, increase the timeout: test('name', () => { ... }, 10000).
Mock slow external dependencies to make the test fast.
Use jest.useFakeTimers() if the test depends on setTimeout/setInterval.
How to prevent
Mock external dependencies to avoid real network/database delays.
Use fake timers for time-dependent tests.
Keep tests focused and fast — if a test needs 10+ seconds, it might be doing too much.
Related Resources
Related Lessons
- Async Testing
Testing asynchronous code.