Skip to article content
FrontendXLesson 19 / 20

Test flake triage

Turn a vague failure report into a reproduction, isolate the cause, form and falsify hypotheses, instrument the code, fix async and state bugs, lock the fix behind a regression test and review changes for real trade-offs.

advanced · about 40 minutes · 2 exercises

  • Explain
  • Debug

What you will be able to do

#
  • Measure a flaky test's failure rate before changing it.
  • Classify a flake by cause and fix the cause instead of adding a retry.

Overview

#

A flaky test is a bug in the test or in the code, not noise. It passes and fails on the same input, which means something outside the assertion is varying: time, order, randomness or shared state.

Triage starts by measuring. Run the test many times and record the pass rate, then run it in isolation and again in the full suite. A test that only fails in the full suite depends on order or shared state; a test that fails in isolation depends on time, randomness or the environment.

The most common cause is a fixed sleep. A test that waits a fixed number of milliseconds assumes the work finishes in that window, which is true on a fast machine and false on a loaded one.

snippet.js
// flaky: assumes the work finishes within 100ms
await new Promise((resolve) => setTimeout(resolve, 100));
expect(list.items).toHaveLength(3);

The fix is to wait for the condition, or to control time with fake timers so the test is deterministic.

snippet.js
// deterministic: wait for the condition
await waitFor(() => expect(list.items).toHaveLength(3));

The second cause is shared state. A module-level counter, a database row or a cache that one test writes and another reads makes the result depend on order. Reset the state before each test, or give each test its own data.

The third cause is real time or randomness. A test that uses the current date fails at midnight or at a month boundary; a test that uses Math.random is not repeatable. Inject a fixed clock and a seeded generator.

The fourth cause is an unawaited async step. A test that starts an async action and asserts before it settles races the code under test. Await the action or wait for its observable result.

A retry hides a flake; it does not fix it. Use a retry only to gather evidence about the rate, and quarantine a known flake with a named owner and an expiry date rather than leaving it to fail intermittently. The fix is complete when the same measurement that showed the flake now shows a stable pass.

Exercises

#

Reference answers and expected output are authored, not executed in this browser. Nothing you type is run, and there is no Run or Pass control on this page.

  1. Exercise 1Debug

    This test waits a fixed 100 milliseconds and then asserts, so it flakes on a loaded machine. Make it deterministic by waiting for the condition instead of a fixed delay.

    starter.javascript
    it('renders three items', async () => {
      renderList(items);
      await new Promise((resolve) => setTimeout(resolve, 100));
      expect(list.items).toHaveLength(3);
    });

    Reveal the reference answer
    reference.javascript
    it('renders three items', async () => {
      renderList(items);
      await waitFor(() => expect(list.items).toHaveLength(3));
    });

    Authored expected output: The test waits until the list has three items and passes deterministically instead of depending on a fixed delay.

    A fixed delay encodes an assumption about how fast the work finishes. Waiting for the observable condition removes the assumption, so the test passes as soon as the result is ready and fails only if it never arrives.

  2. Exercise 2Explain

    Give a flake triage rubric: the measurements to take, the causes to check and the evidence that the flake is fixed.

    Reveal the reference answer
    reference.text
    First measure: run the test many times and record the pass rate, then run it in isolation and in the full suite. A failure only in the full suite points to order or shared state; a failure in isolation points to time, randomness or the environment. Check for a fixed sleep and replace it with a wait for the condition or fake timers. Check for shared state written by another test and reset it per test. Check for real time and randomness and inject a fixed clock and a seeded generator. Check for an unawaited async step. Do not add a retry as a fix; use it only to gather evidence, and quarantine with an owner and an expiry. The flake is fixed when the same measurement that showed it now shows a stable pass.

    Authored expected output: A rubric with the pass-rate and isolation measurements, the four causes, and the requirement that the original measurement now shows a stable pass.

    Measuring first separates a real flake from a slow test and points at the cause. Requiring the original measurement to turn stable prevents a retry from being mistaken for a fix.

Common misconceptions

#
  • Adding a retry fixes a flaky test.
  • A flake that only appears in CI is a CI problem.

Follow-up practice

#
  • debugging-review-20-cross-service-tracing

Related resources

#

Your progress

#

Answers, drafts and completion marks are stored only in this browser. Nothing is sent to a server and no account is used.

Verification: authored. Answers and outputs on this page are authored reference material, not executed results.

© 2026 FrontendX. All rights reserved.