Product case study · November 12, 2026
Make Claude Code prove a bug fix with a failing test
Fix a sorting bug with Claude Code, preserve the caller's array, and check that your regression test catches the original mistake.

A useful regression test catches the old bug before it congratulates the new code.
A sorting helper can return exactly the right answer and still break the page that called it. If it rearranges the original array, another view may suddenly lose its carefully chosen order.
That is a good bug to give Claude Code: small enough to understand, easy to miss in a happy-path test, and impossible to settle with “looks fixed.” The useful trick is to make the test reject the original implementation before accepting the repair.
This lab uses fictional data, Bash on macOS, Linux or WSL, Node.js 22 or later, and an installed, signed-in Claude Code. The JavaScript checks were run locally; the prompts are an authored walkthrough, not a report of a live Claude session. Official documentation was checked November 12, 2026.
Build a function with a quiet side effect
Create a disposable directory. No packages or application setup are needed:
regression_lab=$(mktemp -d /tmp/claude-regression.XXXXXX)
cd "$regression_lab"
node --version
cat > sorted-copy.mjs <<'JS'
export function sortedCopy(values) {
return values.sort((a, b) => a - b);
}
JS
cp sorted-copy.mjs sorted-copy.original.mjs
cat > SPEC.md <<'SPEC'
sortedCopy accepts an array of finite numbers.
Return a new array in ascending numeric order.
Do not change the input array.
Support empty arrays, duplicates and negative numbers.
Input validation and nested objects are outside this exercise.
SPEC
The name promises a copy. The implementation sorts the caller’s array directly. A check of the returned values alone will not expose that mismatch.
Keep sorted-copy.original.mjs: it is your baseline for a later comparison. It is a copy in a practice directory, not a protected backup or security boundary.
Ask for the failure before the repair
Start claude from this directory. Anthropic’s common workflows describes starting a project session this way and giving Claude a specific bug to investigate. Paste:
Read SPEC.md and sorted-copy.mjs. Work only in this practice directory.
Create sorted-copy.test.mjs using node:test and node:assert/strict.
Import sortedCopy from './sorted-copy.mjs'.
Test numeric ordering, unchanged input, and a distinct returned array.
Include empty input, duplicates and negative numbers.
Run node --test sorted-copy.test.mjs and show the failing assertion.
Do not edit the implementation, SPEC.md or the original copy yet.
Stop after reproducing the bug. Do not install packages or delegate work.
Review any permission requests against that small task. The prompt sets scope; it does not isolate the process from the rest of your machine.
Anthropic’s best-practices guide recommends giving Claude an executable way to check its work. Here, the check must cover the function’s promise to its caller, not merely its return value.
A useful test contains the equivalent of this:
import test from 'node:test';
import assert from 'node:assert/strict';
import { sortedCopy } from './sorted-copy.mjs';
test('sorts numbers without rearranging the input', () => {
const input = [10, 2, -1, 2];
const before = [...input];
const result = sortedCopy(input);
assert.deepEqual(result, [-1, 2, 2, 10]);
assert.deepEqual(input, before);
assert.notStrictEqual(result, input);
});
test('returns a separate empty array', () => {
const input = [];
const result = sortedCopy(input);
assert.deepEqual(result, []);
assert.notStrictEqual(result, input);
});
These are authored reference tests; Claude may choose different names or split the assertions across more cases. With this exact pair, the original code fails both tests: the first at the input comparison, the second at the identity comparison. Node’s test-runner documentation explains node:test and running a file with node --test.
The mixed input is doing real work. Negative numbers and 10 catch a missing numeric comparator, duplicates check that sorting does not discard values, and the saved before array catches the side effect. Already-sorted input would make that last mistake much harder to notice.
Keep the test still while the code changes
If Claude reports success immediately, inspect the test. Did it assert that the input stayed unchanged? Did it use a separate snapshot, or write const before = input, which keeps another reference to the same mutable array?
A syntax error or failed import is also a red test run, but it has not reproduced this bug. Resolve setup errors until the failure points to the stated behavior.
Once the test fails for the right reason, leave Claude and save the test before the repair:
cp sorted-copy.test.mjs sorted-copy.test.before.txt
claude
Give the new session this bounded task:
Read SPEC.md and the existing regression test.
Change only sorted-copy.mjs to meet the specification.
Leave the tests, specification and original copy unchanged.
Run node --test sorted-copy.test.mjs.
Explain the smallest repair and report the command's actual result.
Do not install packages, add features or delegate work.
One possible repair is:
export function sortedCopy(values) {
return [...values].sort((a, b) => a - b);
}
Copying the array before sorting preserves its original order. This is a shallow copy, which is enough for the stated finite-number input. There is no reason to add deep cloning or a utility library to solve this exercise.
After leaving Claude, run the checks yourself:
cmp sorted-copy.test.before.txt sorted-copy.test.mjs
node --test sorted-copy.test.mjs
cmp should exit zero without output. If it reports a difference, review the test changes before accepting the result. A changed test can be legitimate in other work; in this exercise, keeping it fixed makes the before-and-after comparison meaningful.
Check that the test still catches the old bug
A green run shows the repaired file satisfies these assertions. Now ask a different question: would those assertions still reject the old implementation?
The following creates a second test file that imports the saved original. It does not overwrite your repair:
node --input-type=module <<'JS'
import { readFileSync, writeFileSync } from 'node:fs';
const source = readFileSync('sorted-copy.test.mjs', 'utf8');
const baseline = source.replaceAll(
'./sorted-copy.mjs', './sorted-copy.original.mjs'
);
if (baseline === source) throw new Error('Expected import path not found');
writeFileSync('sorted-copy.baseline.test.mjs', baseline);
JS
node --test sorted-copy.baseline.test.mjs
Expect a nonzero exit and assertion failures about mutation or identity. Check the messages: a missing module is not useful evidence. If the original passes, inspect whether Claude weakened the assertions or edited the original copy.
Run test files by their explicit names here. Bare node --test can discover the deliberately failing baseline alongside the repaired version, making a healthy exercise look broken.
I would keep this extra comparison for a bug that matters, especially when an agent wrote both the test and the fix. It takes away an easy escape route: writing a test that agrees with whatever the implementation already does. It does not prove every possible input works, but it proves your test can recognize this particular mistake.
Your finished folder should contain a passing repaired implementation, an unchanged regression test and a baseline that fails for the intended reason. For a real project, carry that regression test into its normal suite; keep the deliberately broken baseline outside normal test discovery. Use the testing-scope lesson to decide which other behaviors deserve checks before release.
Sources and what was tested
Primary sources checked November 12, 2026:
- Claude Code best practices: give the agent a concrete verification command.
- Claude Code common workflows: project sessions and focused bug-fixing prompts.
- Node.js test runner: built-in tests and command-line execution.
The fixture, prompts and reference tests are authored teaching material. Local verification covered the exact reference tests against the original and repaired JavaScript, plus the baseline-file transformation. No Claude completion rate, speed comparison or production result is claimed.
Put this to work
Distinguish a correct return value from a function that respects its full contract.
Try
Repair a tiny JavaScript sorting helper in a disposable directory.
Prove it worked
Keep the failing baseline, passing repair and unchanged test file.
Keep in view
- Reproduce a sorting function that silently changes its input.
- Ask Claude Code for a failing regression test before allowing the fix.
- Run the same tests against a saved copy of the original implementation.