$ recombobulate _
home / tips / add-tests-to-legacy-code-without-rewriting-it
0

Add Tests to Legacy Code Without Rewriting It

bagwaa @bagwaa · Mar 25, 2026 · Workflows
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.

~/recombobulate $ tip --comments --count=0

Log in to leave a comment.

~/recombobulate $ tip --related --limit=3
0
Scan Pending Changes for Security Issues with /security-review

The /security-review command scans your uncommitted changes for injection vectors, auth gaps, hardcoded secrets, and other common vulnerabilities.

bagwaa @bagwaa · 1 hour ago
0
Run Setup Scripts on Every Session with the SessionStart Hook

The SessionStart hook fires when any session begins or resumes, making it ideal for loading environment variables and running one-time setup scripts.

bagwaa @bagwaa · 1 hour ago
0
Write Property-Based Tests with fast-check and Claude

Ask Claude to write property-based tests for your functions using fast-check — it identifies the mathematical invariants in your code and generates tests that cover inputs you'd never enumerate by hand.

bagwaa @bagwaa · 2 hours ago