Flaky tests · mid
Flaky tests: timing, shared state and time zones
Most flaky tests are races, shared state or time. Find which one with evidence, then remove the cause.
Explain it out loud
Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.
Practise by voiceThe notes
What you must know
- Click that does nothing one run in ten: the page is server rendered, so the button is visible and enabled before JavaScript has loaded and attached the handler. Auto-waiting checks readiness of the element, not of the framework.
- Evidence: a trace (trace: on-first-retry or retain-on-failure), repeat runs (--repeat-each=50), CPU throttling. In the trace look at the action log, DOM snapshots and the network tab.
- Test fix: wait for a real readiness signal. Do not use waitForTimeout.
- Fails only in parallel: shared account (the cart lives on the server per user), limited stock, global settings, static variables, the same download folder, the same inbox.
- Fix: one user per worker, or a new user per test through an API, and products with enough stock. Do not turn parallel off.
- Passes alone and fails in the full suite: leaked state such as a reused browser session, a cookie, a changed setting that is never reset, static variables, data never cleaned.
- Find it by bisecting the suite or by running in random order. Each test must set its own state. Do not use priority to fix order.
- Fails only after midnight: CI usually runs in UTC, and 12:30 am IST is 7:00 pm UTC the day before. Compute expected dates in the business time zone, or control now with an injected Clock or a test clock.
The red pen
Common traps: mistake → fix
Mistake: Reduce parallel threads to 1
Fix: it hides the shared state and slows the suite.
Mistake: Empty the cart at the end of each test
Fix: another test may be using it at that moment.
Mistake: Move the nightly run to 2 am so it passes
Fix: the bug is still there.
Mistake: Click did not happen
Fix: it happened before the page was ready to react.
Easy to hard
Questions interviewers ask, easy to hard
- A test fails once in ten runs after a click does nothing. How do you investigate?
- Tests fail only in parallel. What do you suspect?
- A test passes alone but fails in the full suite. How do you find the leak?
- The nightly run fails only after midnight. Why?
Practise
Practise with these questions
Explain it out loud
Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.
Practise by voiceYour first interview is free
Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.