Skip to content
Latchkey

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.

job log
FAIL src/cart.test.ts
  ● checkout applies discount
    expected 90 but received 100
# passes cleanly on rerun, no code change

Common 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

  1. Store JUnit results (store_test_results) so CircleCI flags flaky tests over time.
  2. Re-run only failed jobs to confirm it passes without a code change (a flake signal).
  3. Quarantine or skip the known-flaky test while you fix the root cause.

Make tests deterministic and isolated

.circleci/config.yml
- 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 debugging

How 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.

Frequently asked questions

What causes "intermittent test failures"?
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.
How do I fix intermittent test failures?
Identify flakes with CircleCI test insights
Can Latchkey fix this automatically?
Yes. Latchkey runs your GitHub Actions on managed runners that detect this failure, apply the fix, and retry the job automatically - self-healing is on by default.

Related guides

References

Latchkey auto-heals failures like this one - detected, fixed, and retried without you. Start free → 30-day trial · No credit card