Add Tests to Legacy Code Without Rewriting It
Got a pile of untested legacy code? You don't need to refactor it before adding tests — Claude Code can write tests around the existing behaviour as-is.
Read `src/Billing/InvoiceGenerator.php` and write PHPUnit tests that
cover the existing behaviour. Do not change the source file.
Use mocks for any external dependencies like the mailer or database.
The critical phrase here is "do not change the source file." Without it, Claude might refactor the code to make it more testable — which alters the very behaviour you're trying to lock down.
This approach lets you build a safety net first, then refactor incrementally with confidence. Works for any language or test framework.
// For JavaScript/Jest
claude "Read the `calculateDiscount` function in pricing.js and write
Jest tests for all the edge cases you can infer from the implementation.
Do not modify pricing.js."
Once the tests pass against the current code, you have a baseline. Now you can refactor or extend with much more confidence — if you break something, the tests will tell you immediately.
Now that the tests are in place, refactor `InvoiceGenerator` to
separate the PDF rendering from the data calculation logic.
Tests first, refactor second — even when the code already exists.
Log in to leave a comment.
Set up Claude Code as an automated reviewer in your CI pipeline — on every pull request, it reads the diff, checks for bugs, security issues, missing tests, and convention violations, then posts its findings as a PR comment. Your human reviewers get a head start because the obvious issues are already flagged before they look.
Before deploying, tell Claude to read your project — migrations, environment variables, queue workers, scheduled tasks, caching, third-party integrations — and generate a deployment checklist that's specific to your app. Not a generic "did you run migrations?" list, but one that knows YOUR infrastructure and catches the things YOUR deploy can break.
Instead of writing a README from memory or copying a template, tell Claude to read your project and generate one that's actually accurate — real setup instructions from your config, real architecture from your directory structure, real API examples from your routes, and real prerequisites from your dependency files.