FRAMEWORK GUIDES · 6 MIN READ

How to Detect and Fix Jest Flaky Tests in CI

Detect and fix Jest flaky tests caused by open handles, fake timers, mock leaks, and worker contention. Integrate Jest JSON with FlakeCheck.

Common Causes of Jest Flaky Tests

Jest runs test files across parallel child workers. The most common sources of Jest flakiness are uncleaned timers (leading to "Cannot log after tests are done"), shared module mocks not cleared in afterEach, open HTTP/database handles, and CPU starvation when --maxWorkers is left unconstrained on 2-vCPU CI runners.

# Run Jest with JSON output and open-handle detection
npx jest --json --outputFile=jest-results.json --detectOpenHandles --maxWorkers=2
npx @onurdrsn/flakecheck ingest jest-results.json --format jest

Enforcing Clean Hooks and Fake Timers

Enable clearMocks: true and restoreMocks: true in jest.config.ts, and call jest.useRealTimers() in afterEach whenever a suite uses fake timers. Combine this with FlakeCheck skip-list generation to quarantine chronic offenders while keeping pull requests green.

Eliminate flaky tests automatically with FlakeCheck

Stream your JUnit, CTRF, Jest, Vitest, Playwright, Pytest, Mocha, or Go reports to FlakeCheck and get statistical scoring + auto-quarantine in minutes.

Related Guides