Flaky Test

Generalintermediate

// Definition

A test that passes and fails intermittently without any code changes, often caused by timing issues, shared state, async race conditions, or external dependencies. The single largest source of CI noise.

// Why it matters

A flaky test passes and fails without code changes, and it's corrosive: teams start ignoring red, which defeats the suite. The single largest source of CI noise. QA value is in diagnosing the flake class (timing, order, shared state, network) rather than blanket-retrying it.

// How to test

// Quarantine + repeat to confirm flakiness before fixing
// (run the single test many times; a stable test is green every time)
Cypress._.times(20, (i) => {
  it(`order list renders — run ${i}`, () => {
    cy.intercept('GET', '/api/tools').as('getTools')
    cy.visit('/tools')
    cy.wait('@getTools')               // wait on the dependency, not a sleep
    cy.get('[data-cy=tool-card]').should('have.length.greaterThan', 0)
  })
})

// Code Example

CypressFix a flaky test by waiting for state
TypeScript
// ❌ Flaky — clicks before the list renders
cy.visit('/tools');
cy.get('.tool-card').first().click();

// ✅ Stable — waits for the API the page depends on
cy.intercept('GET', '/api/tools').as('getTools');
cy.visit('/tools');
cy.wait('@getTools');
cy.get('.tool-card').first().click();

// Common mistakes

  • Fixing flakiness with cy.wait(3000) instead of waiting on a real signal
  • Sharing state between tests (order-dependence masquerading as flake)
  • Auto-retrying in CI so flakes are hidden instead of fixed

// Related terms

Learn more · TestNG

Chapter 4 · Lesson 3: Retry Logic for Failed Tests