34. Root-Cause Analysis & Flaky Test Elimination

Why Tests Become Flaky

1. Hard Sleeps (waitForTimeout): Relying on arbitrary sleep timers instead of actionability checks. 2. Shared State: Test A mutating data that Test B depends upon. 3. Fragile Locators: Relying on generated CSS classes that mutate between builds. 4. Animation Latency: Clicking an element while it is sliding into view.

---

Retry Configuration in Playwright

export default defineConfig({
  // Retry 2 times on CI, 0 times locally
  retries: process.env.CI ? 2 : 0,
});

When a test fails on attempt 1, passes on attempt 2:
- Playwright marks the test as Flaky (yellow status).
- Captures full traces of the failed attempt for inspection.

---

Stress Testing for Flakiness

Before merging a new test into the main branch, run it in a loop 20 times:
npx playwright test tests/checkout.spec.ts --repeat-each=20

If it fails even once, fix the locator or synchronization before merging!

Task

  • Implement classifyTestResult:
  • 1. Accept attempts (number) and finalPassed (boolean).
  • 2. Return 'FAILED' if finalPassed is false; 'FLAKY' if attempts > 1 and finalPassed is true; otherwise 'PASSED'.
πŸ’‘Common doubts
Did you like the problem?
Lesson 34 of 36
TypeScript
Loading code editor…
Next