high

Timeout — Async callback was not invoked within 5000ms

An async test did not complete within the default 5-second timeout. The async operation may be hanging, or done() was never called.

Common Causes

  1. 1Forgotten await on an async function
  2. 2done() callback never called in callback-style tests
  3. 3API call or network request hanging
  4. 4Infinite loop in the code under test
  5. 5Timer (setTimeout) not faked and waiting for real time

How to Fix

Use async/await instead of done callback

// Instead of:
it('fetches data', (done) => {
  fetchData().then(data => {
    expect(data).toBeDefined();
    done();
  });
});

// Use:
it('fetches data', async () => {
  const data = await fetchData();
  expect(data).toBeDefined();
});

async/await is cleaner and less error-prone than the done() callback pattern.

Increase timeout for slow tests

it('slow test', async () => {
  // ...
}, 30000); // 30 second timeout

Pass a timeout value as the third argument to it() or test().

Use fake timers for setTimeout

jest.useFakeTimers();

it('debounces input', () => {
  handleInput('test');
  jest.advanceTimersByTime(500);
  expect(callback).toHaveBeenCalled();
});

Fake timers allow you to control time without actually waiting.

How ObserveOne Helps

ObserveOne identifies slow and flaky tests caused by timing issues and recommends fixes.

Start Monitoring Free

Related Errors