Sub topic 11
Keeping flaky tests from blocking merges
This chapter assumes you know how required status checks work and have written a workflow in GitHub Actions or a similar CI system. It covers how to spot flaky tests, how to contain them, and how to keep them from stopping merges.
What a flaky test is
A flaky test is a test that passes and fails on the same code. Nothing in the commit changed, but the result did. Common causes are:
- Timing assumptions, such as a fixed
sleepthat is too short on a busy runner. - Shared state, such as tests that depend on run order or a common database.
- External services, such as a real network call that sometimes times out.
- Randomness or clock dependence, such as unseeded random data or tests that break near midnight.
On a required check, one flaky test blocks every pull request until someone reruns the job. Developers then learn to click "rerun" without reading the failure. Real failures get rerun too, and trust in the pipeline erodes.
Detect flakiness with data
Do not rely on memory ("that test is always red"). Measure it. A test is flaky if it both passed and failed on the same commit. Two sources give you that signal:
- Reruns. If a job fails, is rerun with no code change, and then passes, record the failing test names.
- Main branch history. A test that fails on
mainand passes on the next commit with unrelated changes is a suspect.
Keep a simple table of each suspect with its failure count over the last 30 days. The tests at the top of that table are the ones to fix or quarantine first.
Contain it: retry, quarantine, or fix
| Option | What it does | Risk |
|---|---|---|
| Retry | Reruns a failed test a limited number of times | Hides the flake and adds runtime |
| Quarantine | Moves the test out of the blocking check | Coverage drops until it is fixed |
| Fix | Removes the root cause | Takes engineering time |
Fixing is the goal. Retries and quarantine are short-term containment. Limit retries to one or two, and log every test that needed one. Otherwise the flakiness stays invisible.
Quarantine in practice
Quarantine means the test still runs, but its failure does not block a merge. A common approach is to tag flaky tests and run them in a separate, non-required job.
name: pr
on:
pull_request:
branches: [main]
jobs:
fast-checks:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
cache: npm
- run: npm ci
- run: npm run lint
- run: npm test -- --testPathIgnorePatterns=quarantine
quarantined:
runs-on: ubuntu-latest
continue-on-error: true
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
cache: npm
- run: npm ci
- run: npm test -- --testPathPattern=quarantineThis example assumes Jest, with flaky tests moved into files whose path contains quarantine. Only fast-checks should be listed as a required status check. The quarantined job still reports results, so you can see whether a test has stabilized.
Where flaky tests go next
A quarantined test does not have to leave the pipeline. Moving it to the nightly build keeps it running against the default branch, where a failure blocks nobody. You can then watch its failure rate over several nights. A test that passes consistently can return to the pull request stage. A test that keeps failing goes to its owner for a fix.
Common mistakes
- Marking a whole job
continue-on-errorand forgetting it. A real failure then never blocks anything. - Retrying everything by default. Runtime grows and flakes go unreported.
- Quarantining tests that fail because of a real bug. Confirm the failure is intermittent before moving it.
- Leaving the quarantine job out of any review, so no one looks at it.
Exercise
A test fails on a pull request, then passes when the job is rerun with no code change. The team lead suggests adding retries: 3 to all tests so this stops happening. Give two reasons this is a weak fix and describe a better first step.
Show answer
Retries hide the flake, so nobody learns which test is unreliable, and they add runtime to every run. A real intermittent bug could also be retried into a pass and ship. A better first step is to record the failing test name, check its failure history, and if it is flaky, quarantine it into a non-required job with an owner and deadline while the root cause is fixed.