Skip to content
mentoor

Topic 1 of 5 · 4 min read

Building a reliable minimal reproduction

Goal: Turn a vague bug report into a minimal reproduction that fails every time and can be saved as a test.

A bug you cannot reproduce is a bug you cannot fix or prove fixed, so the first job is a small case that fails every time. Everything later, from bisecting to asking an AI assistant for help, works better with one.

Check yourself · 1 of 3

A teammate reports: "The export fails sometimes." What is the best first step?

Choose one answer

Why it matters

A report like "checkout sometimes fails for some users" gives you nothing to run. Without a repro you guess, change code, and cannot tell whether the bug went away or just hid. With one, the fix has a before and an after, and the repro becomes the regression test.

How to do it

  1. Capture the facts. Exact input, exact error text, app version, runtime version (Node, Python, JVM), operating system, config flags, feature flags and the data involved. Ask the reporter for a request id, a timestamp and a screenshot of the error, not a description of it.
  2. Make it fail on your machine. Same version, same input. If it will not fail, the difference between their setup and yours is the first clue. Check data, time zone, locale, and concurrency.
  3. Make it deterministic. Fix the random seed, freeze the clock, stub the network, pin dependencies. For a flaky bug, loop it: run it 200 times and count failures. A bug that fails 3 times in 200 is reproduced; you now have a rate to compare against.
  4. Shrink it. Remove one thing at a time: a field, a step, a dependency, a line. After each removal, run again. Keep the removal if it still fails. Stop when removing anything makes it pass.
  5. Save it as a test. Put the minimal case in the test suite and watch it fail for the right reason, with the same error as the report.

Before and after

Weak report: "Invoice totals are wrong sometimes."

Minimal repro:

import { test, expect } from "vitest";
import { invoiceTotal } from "./invoice";

test("totals round half up per line, not on the sum", () => {
  const lines = [{ price: 0.105, qty: 1 }, { price: 0.105, qty: 1 }];
  expect(invoiceTotal(lines)).toBe(0.22); // fails: returns 0.21
});

Two lines, one assertion, fails every time. "Sometimes" turned out to mean "when a price has three decimals".

What goes wrong

  • Fixing before reproducing. The change "works" on your machine because you never saw the failure there.
  • Repros that need the whole system. A case that needs production data and three services cannot be bisected or handed to a teammate. Shrink it.
  • Failing for a different reason. The test fails, but with a typo-level error, not the reported one. Compare the error text with the report.
  • Ignoring the environment. Many bugs live in versions, locale or time, not in the code you are staring at.

Examples

Shrinking a flaky test

A test fails about once in 20 CI runs. You loop it 200 times locally and it fails 11 times. Removing the database setup still fails 10 times, so the database is not involved. Removing the second await makes it never fail. The bug is a race between two promises, and the repro is now eight lines.

A report that becomes a command

The report says "export is broken". Asking for the request id and file size shows it fails only above 10 MB. A repro is then one line:

head -c 11000000 /dev/urandom | base64 > big.txt && ./export big.txt

The size limit, not the export logic, is the thing to inspect.

Key points

  • Capture exact input, error text, versions and environment before touching the code.
  • A repro must fail every time; make randomness, time and network deterministic, or measure a failure rate over many runs.
  • Shrink the case by removing one thing at a time until removing anything makes it pass.
  • Save the minimal repro as a test and confirm it fails with the same error as the report.
  • If it will not fail on your machine, the difference between setups is the first clue.

Common mistakes

  • Changing code before the bug has been seen failing.
  • Keeping every step and fixture from the original report instead of shrinking.
  • Stubbing so much that the repro no longer fails for the reported reason.
  • Reporting 'cannot reproduce' without comparing versions, data and time zone with the reporter.

Check yourself · 2 of 3

You have a failing case with 40 steps. How do you shrink it?

Choose one answer

Check yourself · 3 of 3

A test fails 7 times in 100 runs. What does that tell you?

Choose one answer

Practice items, not counted towards your mastery.

Start here