Lesson 9 of 9 · 28m
Debugging Flakes, Traces, and the Quarantine Trap
Triage intermittent failures systematically with traces, repeatability experiments, ownership, and quarantine rules that expire instead of becoming a graveyard.
Flake triage is detective work, not vibes. The worst response is to rerun the job and move on. The second worst is to open a ticket called 'flaky test' with no evidence. A flaky result means the system is nondeterministic somewhere: the test, browser, application, network, data, scheduler, or product code. Your job is to narrow the nondeterminism until it has a name.
Start with the trace, not the test source
Playwright traces are time-ordered evidence. Open them locally:
npx playwright show-trace trace.zipInspect the action that failed, the DOM snapshot before and after, network requests, console errors, response timing, and screenshots. Ask whether the action happened, the request fired, the backend responded, and the UI rendered the result.
This breaks the problem into test harness, transport, backend, and rendering. Most teams jump directly to step four and miss the actual failure.
Classify the failure signature
Do not aggregate every timeout into one bucket. Separate locator, stability, strictness, response, data, browser-crash, and visual-diff signatures.
The same test can flake for different reasons. A ticket that treats them as one issue will never converge.
Reproduce probability, not certainty
You cannot prove a flaky test fixed by running it once. Use npx playwright test tests/checkout.spec.ts --repeat-each=20 at both --workers=1 and --workers=4.
Compare failure rates by worker count, browser project, time of day, environment, and commit. If failures cluster under CPU contention, use tracing and test timing rather than guessing. If they only happen when another file runs first, you have an isolation failure even if the failing line is a timeout.
Instrument the application deliberately
Test IDs are not enough for race diagnosis. Correlate browser tests with server logs using a run ID:
test.beforeEach(async ({ page }, testInfo) => {
await page.setExtraHTTPHeaders({
'x-e2e-run-id': `${testInfo.workerIndex}-${testInfo.retry}`,
});
});Better: generate one run ID per test and attach it to API requests through app-level headers or query context where supported. Suddenly 'the backend timed out' becomes 'request 0193... waited 8.2 seconds behind migration lock X.' Evidence beats folklore.
Quarantine must expire
Sometimes a failing flaky test should stop blocking unrelated work while you investigate. Quarantine is legitimate when it is explicit, owned, visible, and temporary.
Do not use a permanent skip list. Record the test title, owner, failure signature, first observed date, tracking issue, and expiration. Do not quarantine whole directories because one third-party dependency is unstable. If the test covers a release-critical workflow, quarantine may be worse than fixing the pipeline or reverting the change.
Fix the layer that owns the nondeterminism
Do not patch a backend race with a longer browser timeout. Do not patch ambiguous UI with .first(). Do not patch missing seed isolation with retries. Test-harness fixes belong in the harness, application races belong in the application, and infrastructure contention belongs in CI sizing or scheduling. The correct fix usually makes the failure impossible or at least loudly diagnosable, not merely less frequent.
Field notes
- Keep the first failing artifact. Retried success often overwrites the evidence you needed.
- Console warnings are not noise when they indicate hydration mismatch, duplicate keys, or deprecated APIs with timing effects.
- A flake rate below a few percent can still block merges at scale; probability compounds across large suites.
- Time-of-day flakes often mean cron jobs, cache expiry, rate limiting, or external dependency maintenance windows.
- When in doubt, make the system more observable before making the test more tolerant.
Was this lesson helpful?