Course contentsShow
← React
Lesson 2205 of 2,39648. End-to-End TestingFree lesson

Flaky Test Detection and Retries

Configure automatic retries for flaky tests and implement strategies to identify and isolate unreliable tests.

Flaky Test Detection and Retries

You'll learn how to configure automatic retries for unstable tests and implement strategies to identify flaky end-to-end tests that fail unpredictably.

The Flaky Test Problem

Flaky tests are the worst kind of testing debt: they pass sometimes and fail other times without any code changes. In E2E testing, flakiness often comes from timing issues, network instability, or race conditions. Think of them like a smoke detector with a loose battery—it works most of the time, but occasionally fails when you need it most, eroding trust in your entire test suite.

Configuring Automatic Retries

Playwright and Cypress both support retry logic. In Playwright, you can configure retries in your playwright.config.js:

export default {
  retries: process.env.CI ? 2 : 0, // Retry twice in CI, never locally
  use: {
    actionTimeout: 10000,
  },
};

This means if a test fails in CI, Playwright automatically reruns it up to 2 more times before marking it as failed.

Identifying Flaky Tests

The key is tracking retry patterns. If a test passes on retry, it's flaky. Playwright's HTML reporter shows which tests needed retries:

  • ✅ Passed (on retry 2) — this is your flaky test alarm
  • ❌ Failed after 3 attempts — legitimately broken

Isolation Strategies

When you spot flaky tests:

  1. Add explicit waits for network requests using waitForResponse()
  2. Use waitForLoadState('networkidle') before assertions
  3. Isolate database state between tests
  4. Check for leftover timers or animations

Retries are a safety net, not a solution—the goal is to fix the root cause, not mask it with retries.

Key Takeaway: Configure retries (sparingly) to handle occasional CI environment issues, but treat any test that passes on retry as a bug to investigate—flaky tests hide real problems and slow down your deployment pipeline.