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
What Is a Flaky Test? Causes, Impact, and How to Eliminate Test Flakiness
Learn what a flaky test is, why flaky tests break CI velocity, how to measure test flakiness, and how FlakeCheck quarantines unstable tests.
How to Detect Flaky Tests Automatically in CI/CD Pipelines
Step-by-step guide to flaky test detection using rerun tracking, flip-rate statistics, Wilson confidence intervals, JUnit XML, and CTRF.
How to Fix Flaky Tests: Root Causes, Code Patterns, and Prevention
Actionable engineering playbook on how to fix flaky tests across timing races, shared state, network calls, concurrency, and test order.