Dealing with Flaky Tests in Cypress: Root Causes and Real Fixes
Flaky tests are one of the biggest pain points in UI test automation. They pass locally but fail in CI, or fail once and pass on rerun — killing developer confidence and slowing down deployments.
In this post, we’ll dive into why Cypress tests get flaky, and how to fix them using real-world techniques.
❗ What Are Flaky Tests?
Flaky tests are tests that fail unpredictably — not because of bugs in the app, but due to:
- Timing issues
- Async delays
- DOM instability
- External API dependencies
- Network latency
- Test pollution (leftover state)
🔍 Common Causes of Flakiness in Cypress
1. Improper Waiting for Elements
Bad:
cy.get('.user-card').click()
Better:
cy.get('.user-card', { timeout: 10000 }).should('be.visible').click()
2. Detached DOM Elements
Sometimes Cypress grabs an element, but it disappears before the action is performed.
Fix:
cy.get('.btn').should('exist').click()
Or force a retry:
cy.contains('Submit').click({ force: true })
3. Non-deterministic Test Data
Tests that rely on dynamic or random data can cause inconsistent results.
Fix:
- Use fixtures or mocks
- Stub network requests using
cy.intercept()
- Reset backend state before test runs
4. Test Pollution
Leftover cookies, localStorage, or unclosed modals can affect future tests.
Fix:
beforeEach(() => {
cy.clearCookies()
cy.clearLocalStorage()
})
🛠 How Cypress Helps
- Built-in retryability for
.get()
,.should()
,.contains()
- Time-travel UI to inspect exact DOM state during failure
- Custom commands to encapsulate reliable behavior
- Network intercepts to isolate tests from real API flakiness
✅ Best Practices to Prevent Flaky Tests
- Use
cy.intercept()
to stub unstable APIs - Prefer
cy.findByRole()
with Testing Library for stable selectors - Don’t use arbitrary
cy.wait(2000)
- Add
should('be.visible')
orshould('exist')
before actions - Always reset test state (
cy.session()
,cy.clear*()
)
🚀 Final Thoughts
Flaky tests erode trust in your automation suite — but they can be fixed.
With Cypress’s built-in power and the right test design, you can build stable, maintainable tests that ship confidently in CI/CD.
Next in Series: Parallelizing Cypress Tests in CI/CD for Blazing-Fast Pipelines