How to Debug Flaky Tests That Pass Locally and Fail in CI
A test that passes on your laptop and fails in CI is not lying to you. It is telling you that the two environments are not the same, or that the test depends on something you did not notice. To debug flaky tests in CI, use the same discipline you would use for any other bug: reproduce the failure, narrow down the cause, fix it, and prove the fix holds. Retrying the build until it turns green is not a fix.
This guide walks through that process: reproduce the failure, check timing, test order, shared state, and environment differences, then quarantine the test while you fix it properly.
What Makes a Test Flaky
A flaky test gives different results for the same code. The code did not change between the green run and the red run, so something outside the code changed: timing, order, data, network, or the machine itself. Your laptop is warm, fast, and full of state from earlier runs. A CI runner is usually cold, slower, and shared with other jobs.
Stop treating the failure as random. Read the CI log and write down three facts: which test failed, what the error said, and whether it failed before. A timeout, an assertion on a value that arrived too late, or a record that already existed is already a lead. If the log gives no useful message, improve the logging first. A test that fails silently wastes far more time than one that fails loudly.
It helps to know what kind of test you are dealing with. A unit test that touches no network and no clock should almost never be flaky, so flakiness there points at shared state or test order. An integration test that talks to a database or an external service has more moving parts. If you are unsure where your test sits, this explanation of when unit tests are not enough and integration testing earns its place draws a clear line between the two.
How to Debug Flaky Tests in CI: Start With Reproduction
Do not change the test until you can make it fail on demand, or at least often enough to test a fix against. Start with the cheapest reproduction: run the single failing test locally, ten times in a row.
for i in 1 2 3 4 5 6 7 8 9 10; do
npm test -- tests/checkout.test.js || echo "failed on run $i"
done
If it fails locally, you have a local bug, which is good news. You can add logging, use a debugger, and iterate in seconds instead of waiting for a CI run. If it passes ten times locally, run the whole suite. Many flaky tests only fail when other tests run first, because an earlier test leaves data, files, or a running server behind.
If the full suite still passes locally, make your machine look more like CI. Run the tests in a fresh container or a clean checkout, with a cold cache. Use the same command CI uses, with the same flags and environment variables, copied from your pipeline config. If your pipeline lives in GitHub Actions, this guide to build a CI/CD pipeline with GitHub Actions shows where that command usually lives.
Timing and Async: The Most Common Cause
Most flaky tests are timing tests in disguise. The test expects something to happen by a certain moment, and on a slower CI machine that moment arrives a fraction too late. The usual suspects are hard-coded waits, real network calls, and code that assumes one async operation finishes before another starts.
Look for setTimeout and sleep calls in the test first. A wait of 500 milliseconds works on a fast laptop and fails on a loaded CI runner. Replace fixed waits with a wait for a condition: poll for the element, the row, or the message until it appears or a generous timeout expires. The test then passes as soon as the work is done, instead of passing only when the machine is fast enough.
Test Order and Shared State
If a test passes alone and fails in the full suite, test order or shared state is the likely cause. One test changes something, and a later test reads the changed version instead of a clean one. The shared thing is often a database row, a file on disk, a global variable, a cache entry, or a port that is still in use.
Find the culprit by running the failing test together with each test file that runs before it, one at a time, until the failure appears. A faster shortcut is to run the suite in a different order. Many runners have a shuffle or random-seed option. If the failure moves to a different test when you change the seed, order dependence is confirmed.
Once you find the pair, look at what the earlier test leaves behind. The fix is usually to clean up in the test itself: delete the rows it created, close the server it started, or reset the global it changed. Each test should set up the state it needs and tear it down when it finishes, so it can run in any order, alone or with the rest of the suite.
Environment Differences Between Local and CI
When a test passes locally in every order and still fails in CI, the environment is the difference. Compare the two, setting by setting. Check the runtime version, the database version, the timezone, the locale, and the environment variables. CI runners often default to UTC while a developer laptop uses local time, and a test that formats a date can behave differently under each.
Check the resources too. CI runners often have less memory and fewer CPU cores than a developer laptop. Read the whole CI log, not just the last lines: the real cause may show up as an out-of-memory kill or a service that failed to start, several lines above the test failure. A slow build and a flaky test often share the same root cause, and the fixes in this guide to speed up slow CI builds cover the resource and caching settings that matter most here.
Quarantine, Then Fix: A Workflow That Stops the Bleeding
A flaky test that blocks every pull request needs a decision, not another retry. Quarantine it: move it out of the main suite into a separate job that runs on a schedule, or mark it to skip with a link to the issue that tracks the fix. The rest of the suite keeps running, and the team stops merging around a red build. Give the issue an owner and a date.
When you fix the test, change one thing at a time and measure the result. Replace a fixed wait with a condition wait, then run the test in a loop fifty times. Add the missing cleanup, then run the full suite with a shuffled order. Fifty clean runs is a reasonable bar for a test that used to fail once in twenty.
Picking the right tooling helps too. Different test runners handle parallelism, isolation, and retries differently, and the wrong default can create flakiness that looks like a test bug. This comparison of JavaScript testing frameworks in 2026, Jest vs Vitest vs Mocha covers those defaults, so you can check whether your runner is working against you before you rewrite the test.
Prevent Flaky Tests From Coming Back
Fixing one flaky test is good. Stopping the next ten takes a few habits, not a big project. First, write tests that catch bugs instead of chasing a coverage number. A test that asserts on real behaviour, with its own data and no hidden dependencies, is far less likely to turn flaky. This guide to write tests that catch bugs, not just coverage shows what that looks like in practice.
A checklist you can reuse
Work down this list the next time a test passes locally and fails in CI:
- Read the full CI log and note the test, the error, and any earlier failures.
- Run the single test locally ten times, then run the full suite.
- Run the suite in a different order to check for order dependence.
- Replace fixed waits with waits for a condition, and await every async task.
- Give each test its own data and clean up what it creates.
- Compare runtime, database, timezone, locale, and environment variables between local and CI.
- Quarantine the test with an owner and a deadline if it keeps blocking merges.
- Fix one cause, then run the test fifty times and run the full suite shuffled.
That last step is the one people skip, and it matters most. A fix you cannot measure is a guess with extra steps. When the test passes fifty times in a row, in a shuffled suite, on a clean CI runner, you know the job is done.