Skip to article content
FrontendXLesson 08 / 20

Regression tests: lock the fix

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.

intermediate · about 40 minutes · 2 exercises

  • Explain
  • Test

What you will be able to do

#
  • Write a regression test that fails before the fix and passes after.
  • Assert observable behaviour instead of implementation details.

Overview

#

A bug you fixed once will come back unless a test stops it. A regression test is a test that fails before the fix and passes after.

Write the test first, while the reproduction is fresh. Run it and confirm it fails for the right reason. Then apply the fix and confirm it passes. This red-green sequence proves the test actually exercises the bug; a test written after the fix often passes for the wrong reason.

snippet.js
// before the fix this fails because sum([]) throws without an initial value
it('returns 0 for an empty list', () => {
  expect(sum([])).toBe(0);
});

Test the boundary that broke, not the whole function. If the bug was an empty array, add the empty case. If it was a negative number, add the negative case. A regression test should be specific enough that it would have caught this exact bug.

Avoid overfitting. A test that asserts an internal helper was called couples the test to the implementation and breaks on a harmless refactor. Assert observable behaviour instead: inputs, outputs and errors.

snippet.js
// brittle: breaks when the implementation changes
it('calls normalize once', () => {
  const spy = jest.spyOn(utils, 'normalize');
  parse('x');
  expect(spy).toHaveBeenCalledTimes(1);
});

// durable: asserts the contract
it('parses a trimmed value', () => {
  expect(parse('  x  ')).toBe('x');
});

Name the test after the bug, not the function. A name like returns 0 for an empty list documents the requirement; a name like sum works documents nothing. The test suite becomes the record of every bug you have already paid for.

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 1Test

    Write a regression test that fails before the fix and passes after. The bug: sum([]) throws because reduce has no initial value instead of returning 0.

    starter.javascript
    function sum(values) {
      return values.reduce((total, v) => total + v);
    }

    Reveal the reference answer
    reference.javascript
    function sum(values) {
      return values.reduce((total, v) => total + v, 0);
    }
    
    it('returns 0 for an empty list', () => {
      expect(sum([])).toBe(0);
    });

    Authored expected output: Before the fix the test fails because sum([]) throws without an initial value; after adding the initial value 0 it passes.

    reduce with no initial value uses the first element as the seed and throws on an empty array, or with a single element returns that element. Supplying 0 makes the empty case well defined and the test pins it.

  2. Exercise 2Explain

    Why write the regression test before applying the fix?

    Reveal the reference answer
    reference.text
    Writing the test first proves it fails for the right reason on the current code. If you write it after the fix, it may pass for an unrelated reason and give false confidence. The failing run is the evidence that the test actually exercises the bug.

    Authored expected output: An explanation that the failing run proves the test exercises the bug.

    A test written after the fix can pass without ever touching the bug, because the code is already correct. The failing run before the fix is the evidence that the test actually exercises the broken path.

Common misconceptions

#
  • A test written after the fix is as good as one written before.
  • A test that checks internal calls is stronger than one that checks output.

Follow-up practice

#
  • debugging-review-09-performance-regressions

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.