A test that fails one run in twenty is worse than no test at all — it teaches the team to ignore red builds. Here is how to track down flakiness and keep it out.
Every team eventually meets the flaky test: it passes, it fails, it passes again, and nothing in the code has changed. Individually each one is a minor annoyance. Collectively they erode the one thing a test suite exists to provide — confidence.
Measure before you fix
You can’t fix what you can’t see. Record every test result in CI and flag tests that both pass and fail on the same commit. A simple weekly report of the least stable tests turns a vague frustration into a prioritized list.
The usual suspects
- Timing: fixed sleeps, animations and network calls that sometimes take longer.
- Shared state: tests that depend on data or ordering left behind by other tests.
- Environment: time zones, locales, screen sizes and clock changes.
- Concurrency: race conditions in the code under test — sometimes a real bug.
Wait for conditions, not time
Most UI flakiness comes from waiting a fixed amount of time instead of waiting for the application to be ready. Modern test frameworks retry assertions until they pass or time out — use that instead of sleeps.
// Flaky: assumes the request finishes within two secondsawait page.click('text=Save');await page.waitForTimeout(2000);expect(await page.textContent('.toast')).toBe('Saved'); // Stable: waits for the outcome the user would actually seeawait page.getByRole('button', { name: 'Save' }).click();await expect(page.getByRole('status')).toHaveText('Saved');Note
Quarantine, don’t delete. Move a known-flaky test into a separate, non-blocking job with an owner and a deadline. The main pipeline stays trustworthy and you keep the coverage.
Prevent new flakiness
- Give every test its own data, created in setup and cleaned up afterwards.
- Run new tests many times in CI before they are allowed to block merges.
- Freeze time and randomness wherever tests depend on them.
- Treat a flaky test as a bug with an owner — not as background noise.
“A test suite that cries wolf trains everyone to stop listening.”
Written by
Devant Quality Engineering
Test automation and security
Quality and security engineers who build the automation and practices that keep releases calm.




