Skip to article content
FrontendXLesson 01 / 20

Reproduce the failure

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.

beginner · about 30 minutes · 2 exercises

  • Predict
  • Debug

What you will be able to do

#
  • Write a reproduction with starting state, exact actions, observed result and expected result.
  • Distinguish a deterministic failure from a flaky one and record a pass rate.

Overview

#

A bug report is not a bug. A report says the dashboard is broken. A bug says clicking Save twice with an empty title throws a TypeError. Debugging starts by converting the report into steps that fail the same way every time.

A usable reproduction has four parts: the starting state, the exact actions, the observed result and the expected result. If you cannot write those four lines, you are still guessing.

Consider a function that formats a price. A tester reports that some totals render as $NaN.

snippet.js
function formatTotal(cents) {
  return '$' + (cents / 100).toFixed(2);
}

The function is fine for whole numbers of cents. The report is vague, so the first job is to find the input that fails. Narrowing the inputs, you discover the failure only happens when the value arrives as a string that is not a clean number, because the division then produces NaN and the function returns the string $NaN instead of failing.

snippet.js
function formatTotal(cents) {
  const value = Number(cents);
  if (!Number.isFinite(value)) throw new TypeError('cents must be a finite number');
  return '$' + (value / 100).toFixed(2);
}

The corrected version makes the contract explicit and fails loudly on bad input instead of silently producing a wrong string. That is the difference between a reproduction and a guess: the reproduction names the input, the bad output and the corrected contract.

Flaky failures need one extra step. A test that passes half the time usually depends on time, order, network or shared state. Run it in a loop and record the pass rate before changing anything, so you can later show that the fix actually changed the rate.

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 function returns NaN for an empty list. Add a minimal reproduction by calling it with the exact failing input, then fix it so it throws a clear error instead of returning NaN.

    starter.javascript
    function average(values) {
      let total = 0;
      for (const v of values) total += v;
      return total / values.length;
    }
    
    // reproduce:

    Reveal the reference answer
    reference.javascript
    function average(values) {
      if (values.length === 0) throw new RangeError('average requires at least one value');
      let total = 0;
      for (const v of values) {
        if (typeof v !== 'number') throw new TypeError('average expects numbers');
        total += v;
      }
      return total / values.length;
    }

    Authored expected output: average([]) throws RangeError instead of returning NaN, and average([2, 4]) returns 3.

    The original divides by zero when the array is empty, producing NaN. The reproduction pins the exact input. The fix turns an ambiguous NaN into an explicit, testable error.

  2. Exercise 2Predict

    Which single detail is missing from this reproduction: open the app, click Save, it fails?

    Choose the best answer

Common misconceptions

#
  • A clear description of the symptom is the same as a reproduction.
  • A flaky test can be fixed without first measuring how often it fails.

Follow-up practice

#
  • debugging-review-02-read-the-error

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.