A test is flaky on CI. How do you look for the cause in Cypress?
A flaky test in CI is investigated in order: evidence, reproduction, cause, not by adding retries. Evidence: the failure screenshot, video (off by default) and the timeout message. Then the spec is run in a loop on CI-like resources and typical causes are checked: cy.wait instead of a state, an action before the API response, an intercept after the action, shared data. A test that passed on a retry is still investigated.
Cypress · Waiting
A test passes locally and fails in CI once in five runs. Where do you start?
A flaky test is worse than a steadily red one: people stop trusting it and real failures hide in the noise. The temptation is simple: add retries and move on. This lesson gives an investigation order that leads to the cause, not to muting the symptom.
How it works and why. Flakiness almost always means a race: the test makes a step before the app is ready or depends on something it does not control. So the investigation goes from facts to a hypothesis, not the other way round.
Investigation order
1. Evidence. cypress run takes a screenshot on failure automatically. Video is off by default (video: false), so turn it on for CI. Cypress Cloud offers Test Replay with the DOM, network and console. Read the error message: it names the timeout and the command the test failed on.
// cypress.config.ts
export default defineConfig({
video: true, // false by default
retries: { runMode: 2, openMode: 0 },
});
2-3. Reproduction and cause. Run the spec many times in a row on the same resources as CI: a Cypress._.times loop or the burn mode of the @cypress/grep plugin. Then go through the typical causes.
cy.wait(ms)instead of waiting for a state.- An action before the API response arrived, or an
interceptafter the action. - Shared data between tests and dependence on order.
- Animations and re-rendering of an element between a query and an action.
// 1. reproduce: the spec 20 times
Cypress._.times(20, (i) => {
it('checkout #' + i, () => {
// the flaky test
});
});
// 2. cause: an intercept after the action
cy.get('[data-cy=pay]').click();
cy.intercept('POST', '/api/pay').as('pay'); // the request has gone
cy.wait('@pay');
// 3. fix: the intercept before the action
cy.intercept('POST', '/api/pay').as('pay');
cy.get('[data-cy=pay]').click();
cy.wait('@pay');
The cause is visible only because the test ran in a loop: a single run could pass. The fix puts the intercept before the click, so the event is no longer lost.
Limits and alternatives. retries: { runMode: 2, openMode: 0 } (both 0 by default) help reach a release and cut noise, but a test that passed on the second attempt is still flaky and needs analysis. Not every failure comes from the test: sometimes it is a real app bug that shows up rarely.
The main tool is Trace Viewer: trace: 'on-first-retry' gives a timeline with DOM snapshots, network and console without a separate service. Reproducing is --repeat-each=20, and checking contention for data is --workers=1.
Common mistakes.
- Turning retries on before investigating: the cause stays and the metric turns green.
- Reproducing on a laptop instead of CI resources: the race may not show.
- Adding
cy.wait"to make it pass": it only moves the problem.
Check yourself
A test in CI passed on the second attempt. Why is it not "green" and what next?
Answer
retries.runMode kicked in: the result depends on a retry, so there is a race or a state dependency. Investigate it as flaky: evidence, reproduction in a loop, cause.
There is no video in CI although you "never turned it off". Why?
Answer
The video option defaults to false. Enable it explicitly in the config, for example video: true for CI.