Debugging a flaky Jest test: `Timeout - Async callback was not invoked within the 5000 ms timeout`
A flaky test is a special kind of annoyance because you can't debug it when it's being flaky. Ours failed maybe one run in five on CI, always the same test, always the same timeout. Locally it passed every time. I finally caught it by running the whole suite in a loop.
## The error
$ npx jest --runInBand
FAIL test/sync.test.js
● sync retries once on 503 Timeout - Async callback was not invoked within the 5000 ms timeout
specified by Jest's Timeout.
at Spec.wait (node_modules/jest-jasmine2/build/queueRunner.js:39:38)
Test Suites: 1 failed, 11 passed, 12 total
Running that single file alone passed in about 300 ms. That's the clue: something in another suite was leaking state that changed the timing.
## How I found the other suite
I ran with the noisy flag and a longer timeout to see if it was just slow:
npx jest --runInBand --detectOpenHandles --testTimeout=20000
--detectOpenHandles printed a Timeout handle that was never cleared, and it pointed at a different file, test/webhook.test.js. That suite started a real setInterval in a helper and forgot to clear it. Jest kept the handle open, and under the full suite the event loop was busy enough that our retry test's fake timer never fired when expected.
The helper looked like this:
function startPolling() {
return setInterval(() => refresh(), 1000);
}// test called it and never stored the return value
startPolling();
## The fix
Return the handle and clear it in afterEach:
let poller;beforeEach(() => {
poller = startPolling();
});
afterEach(() => {
clearInterval(poller);
jest.useRealTimers();
});
I also found a jest.useFakeTimers() in that same file with no matching useRealTimers, which is why our retry test behaved differently depending on run order. Fixing both removed the flake entirely. We've had about sixty CI runs since and not one failure.
## The part I still don't love
--runInBand made the flake reproducible for me because it removed parallel-worker timing randomness, but it also made our suite about four times slower. I only use it for debugging now. In CI we run parallel with --maxWorkers=2, which is a compromise between speed and the kind of cross-suite interference that caused this.
The other thing that helped was npx jest --listTests, which shows the order files run in. Because the failure depended on one suite running before another, seeing that order made the connection obvious once I knew to look. I still think fake timers are the trap here. Calling jest.useFakeTimers() is easy and jest.runAllTimers() is tempting, but under a busy event loop the timing of when those calls happen changes. jest.advanceTimersByTime(1000) with an explicit value is less clever and much more predictable. We rewrote the retry test that way too, and the flake never came back after both changes.
If you've got a cleaner way to catch leaked timers than --detectOpenHandles, which is a bit of a sledgehammer, I'm all ears. For now the rule in our repo is: every fake timer gets a real one back in afterEach, and every interval gets cleared. Simple rule, but it took a flaky test to make us write it down.