Back to Library
Iterative DebuggingIntermediate

Generate Tests to Isolate Bugs

Ask Claude to generate a failing unit test that reproduces a bug before fixing it—this creates a regression guard and clarifies the exact failure mode.

testingtddregression

The Strategy

Writing a failing test before a fix is classical TDD. With Claude, this two-step process is fast and ensures you have a regression guard. The test also clarifies the "done" criterion.

prompt.txt
<task>
Write a failing unit test that reproduces this bug.
Use Vitest. Do NOT fix the bug—only reproduce it.
</task>

<bug_description>
The formatCurrency function returns "$NaN" when the input
is a string-formatted number like "1234.56" instead of 1234.56.
</bug_description>

<code>
export function formatCurrency(amount: number): string {
  return "$" + amount.toFixed(2);
}
</code>

You might also need