I’m looking to understand how Cypress retry works for flaky test scenarios.
Occasionally in CI, a couple of tests fail with errors like “The element has an effective height of 0x0”, but they pass on a rerun.
I want to configure Cypress to automatically retry these failed tests, similar to Mocha’s
this.retries
feature.
For example:
cy.visit('/')
cy.get('.my-element').click()
How can I set up Cypress retry logic so the build passes on the first attempt without manually re-running failed tests?
Had a similar problem with flaky tests in CI, mostly due to animations or elements not being immediately interactable.
Cypress introduced built-in test retries in version 5.0+. You can configure it directly in your cypress.config.js like this:
retries: {
runMode: 2, // For CI
openMode: 0 // For local runs
}
This means Cypress will retry each failed test up to 2 times in CI before marking it as failed.
It really helped stabilize our builds. You can also set retries per test using .retries(n) if only certain tests are problematic.
I’ve run into this a lot when running tests in parallel across different browsers or screen sizes. One thing that helped beyond global retries was using conditional retries for specific tests.
You can do this inline like:
it('clicks the element', { retries: 3 }, () => {
cy.visit('/');
cy.get('.my-element').click();
});
i used this on some tests that were flaky only in Firefox or on slower environments. That way you don’t retry everything, just the ones that really need it. Combine that with smarter selectors or waits, and flakiness drops a ton.
One thing I learned the hard way: retries in Cypress only help if the failure is retryable, like network hiccups or timing issues. But if you’re clicking on an element that’s not yet visible, you might want to also pair retries with better waiting strategies.
We refactored some of our tests like this:
cy.get('.my-element', { timeout: 10000 }).should('be.visible').click();
This way, Cypress waits properly before trying the click, and retries the command before the test itself fails. Add test retries on top of that, and it’s way more stable. Just make sure you’re not masking real bugs behind retries.