Add tests to legacy code without refactoring it
Claude Code writes tests for untested legacy code and tells you which behavior it cannot safely assert.
A test file covering the main paths, a list of untestable edges, and no changes to the production code.
- 1
Point Claude Code at the ugliest module
Start with the code that scares you. A file that is already clean does not need this case.
- 2
Run the tests and confirm they fail when they should
Change one line of the production code to break it. The tests should catch it.
- 3
File the refactor list
Keep the list of untestable edges. Those are the lines to refactor when you can afford to touch production.
The prompt to paste
Write tests for this legacy code without refactoring it. The code is in production and cannot change yet. File: [paste the file path or describe the module] Constraints: - Do not change the production code. If a dependency is hard to mock, document it as untestable and move on. - Use the test framework and runner already in the repo. - Cover the main paths: success case, expected errors, and the edge case most likely to be hit. - For each untested edge, explain why it cannot be tested without refactoring the code. Return: 1. A test file with at least three test cases 2. The command to run the tests 3. A short list: behavior covered, behavior skipped, and what refactor would make the skipped cases testable Do not add tests for code that is never called. Do not mock internal implementation details. Test observable behavior.
A test that mocks every dependency is testing the mocks. Stop before the test is more complex than the code.
Sample case written by Hidden Logic Lab. Not affiliated with Claude Code.
More in Coding
Get new cases in your inbox
Weekly notes on new use cases and agent workflows. No product pitches.