GuideClaude CodeIntermediate30 min to do

I expected "write the tests first" to save my legacy code. Claude kept every quirk without it, and the tests it wrote missed my sabotage.

Anthropic's modernization advice says to agree on checkable proof of correctness before you migrate. I tested it on an old callback-style module with hidden quirks, six runs, and found the advice mattered less than I thought, and in a different way.

12 of 12 either way; the tests missed my sabotage
CC
Claude Code Lab
·November 25, 2026·5 min read
I expected "write the tests first" to save my legacy code. Claude kept every quirk without it, and the tests it wrote missed my sabotage.

Level: Intermediate. About 30 minutes. Needs Node 20+ and a Claude Code login.

Every team has an old module nobody wants to touch. Callbacks everywhere, odd little behaviours nobody remembers adding, and no tests. Moving it to modern async code is a perfect job to hand to an AI, and a perfect way to break something quietly.

Anthropic's guide on preparing for code modernization has a sharp idea for this. Before you migrate anything, agree on the certificate: checkable evidence that the new code behaves like the old. Tests that pass, outputs that match. Nothing that needs a human squinting at a diff.

I wanted to know what that is worth on a small, realistic job. So I tested it.

The job: a ledger with quirks

I wrote a small account ledger in the old style: CommonJS, callbacks, 40 lines.

The legacy file

It has some deliberate oddities, the kind real code collects:

  • Amounts are floats in dollars, turned into cents with Math.round(amount * 100). That means 1.005 becomes 100 cents, not 101.
  • A zero amount is silently ignored, with no error.
  • Memos are trimmed and cut to 20 characters, with no "...".
  • Negative balances print as -$12.30, and there are no thousands separators.

The task: make it an ES module with async functions, same names, same behaviour. Errors that went to the callback are thrown instead.

To judge the result I wrote a separate 12-point behaviour check, which Claude never saw. I tested it first on a hand-written correct migration (12 of 12), and every quirk above is in it. I'm calling it hidden because Claude can't see it, which matters later.

Two setups, three runs each

Setup A, migrate only. I described the target and said "keep everything else behaving exactly as it does today."

Setup B, the certificate first. Same prompt, plus: before changing any code, write tests that pin down the current behaviour of every function, including odd cases and error messages. Make them pass on the old code. Then migrate, changing the tests only mechanically.

The results

Six runs, two setups

All six runs scored 12 of 12. Setup A, with no tests at all, kept every quirk, three times out of three. Claude read the old code, saw the odd rounding and the silent zero, and carried them over untouched.

That surprised me. I'd assumed the 1.005 rounding would be the thing it "helpfully fixed."

What the certificate cost: about four times as long and two and a half times the price. Setup A took 24 to 30 seconds and about 19 cents. Setup B took 87 to 107 seconds and 42 to 51 cents. Claude Code reported those figures; I'm on a Pro plan, so I wasn't billed per run.

What it bought: a test file, 16 to 24 tests, left behind in the project. That's real value, since the old module had none.

So are the tests any good?

Here's where I got suspicious. A certificate is only worth what its tests check. So I sabotaged the finished code in the most natural way I could think of: a well-meaning rounding "fix" that adds Number.EPSILON, the standard answer to floating-point rounding questions online.

The sabotage passes the generated tests

All three test suites Claude wrote stayed green. My hidden check caught it immediately. The 1.005 case now returned 101 cents instead of 100.

Why? The tests Claude wrote covered the obvious behaviours (negative amounts, padding of single-digit cents, error messages, ordering). Only one of the three suites even mentioned a float-rounding case, and it didn't catch my change. The quirk that actually matters was the subtle one.

What I'd take from this

  1. For a small module, Claude is already careful. Telling it to keep behaviour identical was enough, six times out of six, in this test. Don't assume you need elaborate process for 40 lines.
  2. Generated tests are not a certificate by themselves. Look at what they check. If you know a quirk exists, name it in the prompt: "include a test that 1.005 becomes 100 cents."
  3. Still ask for the tests. You get a safety net the project didn't have, for about 25 cents.
  4. Try to break your own safety net. Make one plausible wrong change and see whether the tests notice. It takes two minutes.
  5. Scale changes things. Anthropic's guide is about large codebases, with partitions, replay of real traffic and review tiers. My result says nothing about those.

What this doesn't tell you

  • One invented 40-line module, six runs, one model on one day. A messier codebase could behave very differently.
  • My hidden check only covers the quirks I thought of. Claude may have kept others I didn't plant, or missed ones I didn't test.
  • Setup B's tests were not graded for quality beyond the sabotage test. I checked one mutation, not many.
  • I didn't test migrating a module that other code depends on, which is where migrations usually hurt.

Sources

  • https://claude.com/resources/articles/how-to-prepare-for-ai-driven-code-modernization-projects

Editorial note: We ran the steps in this guide ourselves and the screenshots come from those runs. Your results may differ with your plan, model or settings, so check anything important. Cover illustrations are AI-generated. Discovia is independent and not affiliated with Anthropic.

Share this guide X LinkedIn

New guides and tools

Hands-on Claude guides, straight to your inbox

Step-by-step walkthroughs of what's new in Claude and Anthropic's tools, with real screenshots and honest results, including what didn't work. No hype, no jargon, no noise.

By subscribing, you agree to receive newsletter and marketing emails, and accept our Terms of Use and Privacy Policy. You can unsubscribe anytime.

Claude Code Lab

Tests of Claude Code features: hooks, skills, rules files and workflows, with the numbers.

More Claude Code guides →

More from this author

Claude Code

I asked Claude to review a pull request with six planted bugs. Without the spec it found three. With it, all six.

Nine reviews, six planted bugs, three setups. Claude never invented a problem, but it could only catch what the code itself shows. The thing that moved the score from three to six was one short file saying what the change was meant to do.

C
Claude Code Lab
5 min read
Read next

Comments

Related Guides