CircleCI Flaky Tests Failing Intermittently - Fix Test Flakiness
A test fails one run and passes the next with no code change. Flaky tests come from timing/race conditions, shared state between tests, or order dependence that surfaces when CircleCI splits tests across parallel containers.
What this error means
A red job turns green on "Rerun failed jobs" without any commit. The failing test differs run to run, or only fails on certain parallel containers - the hallmark of flakiness rather than a real regression.
FAIL src/cart.test.ts
● checkout applies discount
expected 90 but received 100
# passes cleanly on rerun, no code changeCommon causes
Timing / race conditions
Tests depending on timeouts, async ordering, clocks, or network latency pass or fail by chance. CI’s variable speed exposes races a fast local machine hides.
Shared or leaked state between tests
Tests that share a database row, global, or temp file pass alone but fail when another test runs first - and test-splitting changes which run together per container.
Order dependence across parallel containers
When circleci tests split distributes tests differently each run, an order-dependent test lands in a different group and intermittently fails.
How to fix it
Identify flakes with CircleCI test insights
- Store JUnit results (
store_test_results) so CircleCI flags flaky tests over time. - Re-run only failed jobs to confirm it passes without a code change (a flake signal).
- Quarantine or skip the known-flaky test while you fix the root cause.
Make tests deterministic and isolated
- run:
name: Run tests deterministically
command: |
TESTS=$(circleci tests glob "test/**/*.spec.js" | circleci tests split --split-by=timings)
npx jest --runInBand $TESTS # avoid intra-file races while debuggingHow to prevent it
- Store JUnit results so CircleCI surfaces flaky tests automatically.
- Isolate per-test state (db, temp files, globals) and seed randomness.
- Make order independence a test-suite invariant, since splitting reorders tests.