🟢
Updated recently
Last updated:

Quick Answer: Flaky tests pass and fail inconsistently. Fix them by: (1) identifying root causes (race conditions, timing, external dependencies), (2) implementing automatic retries, (3) quarantining flaky tests, (4) adding network stubs and explicit waits.

Common Causes of Flaky Tests

Cause Example Fix
Timing issues Click before element ready Use explicit waits
Network dependency External API slow/down Stub network requests
Shared state Tests modify shared data Isolate test data
Parallel execution Two tests use same resource Unique data per worker
Dynamic content Timestamps change Flexible assertions

Strategy 1: Automatic Retries

// playwright.config.ts
export default defineConfig({
  retries: 2,
  workers: 4,
});

Strategy 2: Quarantine Flaky Tests

- name: Run stable tests
  run: npx playwright test --grep-invert="@flaky"

- name: Run flaky tests (non-blocking)
  continue-on-error: true
  run: npx playwright test --grep="@flaky"

Strategy 3: Stub External Dependencies

cy.intercept('GET', 'https://api.external.com/*', {
  statusCode: 200,
  body: { data: 'mocked' }
});

Training Resources

Master Playwright with SkilBrill Playwright Training.

FAQ

Should I delete flaky tests?

No. Quarantine them, fix the root cause, and restore them. Track flaky rates as a team metric.