Flaky tests in Cypress and retries
Retries rerun a test from the start after a failure, but only mask instability. A test that passed on a retry is a signal, not a fix. Typical causes are a fixed cy.wait, a race with the network, dependencies between tests and an unstable selector. They are removed by waiting for a condition: a request alias with a specific enough URL and retrying assertions.
Cypress · Flaky
A test is green only on the third attempt. Was it cured or hidden?
Turning on retries is easy and CI turns green at once. It is harder to notice that the unstable test has not gone anywhere: it just costs extra seconds and trust on every run. The lesson separates two things: how to find and remove the root cause and how to keep retries under control.
How it works and why. Cypress retries commands and assertions inside a test, but if the test still fails, a retry starts it again from the beginning: the beforeEach and afterEach hooks run again, while before and after do not. By default both runMode and openMode are 0, which means no retries. The report shows the attempts: a test that passed on a retry is marked as flaky. This is information, not a verdict: it tells you the test is unstable.
Two roles of retries
Retries as a cure
Set retries: 5 everywhere, CI is green, the cause remains. Flaky tests grow and the run slows down.
Retries as a safety net
Few attempts (1-2) only in run mode, and every test that passed on a retry is recorded and investigated.
Example: from a pause to waiting for a condition. The most frequent root cause is a fixed pause that guesses when the data arrives:
// Flaky: a fixed pause and a race with the server response
cy.visit('/orders');
cy.get('[data-cy=filter]').type('paid');
cy.wait(2000);
cy.get('[data-cy=row]').should('have.length', 3);
Two seconds is enough on a fast machine and not enough on slow CI, so the test fails "every other run". The cure is to wait for exactly the request that supplies the data.
// Stable: wait for exactly the right request
cy.intercept('GET', '/api/orders?status=paid').as('paid');
cy.visit('/orders');
cy.get('[data-cy=filter]').type('paid');
cy.wait('@paid');
cy.get('[data-cy=row]').should('have.length', 3);
One detail matters here: the alias fires on the first matching request. If the intercept were for a general /api/orders*, cy.wait would catch the request from cy.visit, not the one from the filter, and the test would stay unstable. So the URL must be specific enough. Flaky causes go well beyond pauses:
| Cause | How it shows up | Cure |
|---|---|---|
Fixed cy.wait | Passes on a fast machine, fails on a slow one | Wait for a condition: a request alias or an assertion |
| Race with the network | Data has not arrived when the test asserts | cy.intercept + cy.wait('@alias') |
| Dependency between tests | Fails only in another order or in parallel | Each test prepares its own state through the API |
| Unstable selector | Sometimes finds the wrong element | data-cy instead of classes and text |
| Slow CI | Timeouts only on CI | Check resources instead of raising timeouts at random |
Retries are set in the config, and can be overridden for a single test or a describe block:
// cypress.config.ts
export default defineConfig({
retries: { runMode: 2, openMode: 0 }, // 0 and 0 by default
});
// a single test or a block
describe('Orders', { retries: 1 }, () => {
it('filters by status', { retries: { runMode: 3 } }, () => {});
});
Limits and alternatives. openMode: 0 is kept deliberately: during development a failure must be visible at once. Retries hide not only flaky tests but real rare bugs too (a race in the product), so a test that passes on a retry deserves to be treated as a signal. For diagnosis, screenshots of each attempt, video and Test Replay in Cypress Cloud help, and Cloud shows flaky tests separately. Quarantine and re-run strategies at the pipeline level are covered in a separate lesson. Playwright has the same idea (retries in the config and trace: 'on-first-retry'), so the principle does not depend on the tool.
Typical mistakes.
- Treating instability by raising
retriesor timeouts: the run slows down and the cause remains. - Not looking at which tests passed on a retry: a green CI creates false confidence.
- Relying on
beforefor state that the test changes: after a retry it is not restored, becausebeforedoes not repeat. - Tests that depend on each other and on shared data: they fail only in another order or in parallel.
Check yourself
A test always passes on the second attempt. CI is green. Why is that not a reason to relax?
Answer
A pass on a retry means the test is unstable: the cause (race, pause, shared data) remains and the retry hides it. It may also be a real rare bug in the product. The root cause has to be found.
There is one alias for /api/orders* covering cy.visit and the filter, and cy.wait('@orders') stands after the filter. Why does the test still flake?
Answer
The first cy.wait('@orders') catches the request from cy.visit, not from the filter, so the assertion may run before the filter response. Make the intercept URL specific (?status=paid) or wait for the second request.