{"slug":"claude-code-refactor-with-tests","title":"Refactor one module and land tests in the same session","summary":"Claude Code reads a single module, writes characterization tests, then changes structure only where those tests pass.","agent":"Claude Code","agentSlug":"claude-code","category":"Coding","categorySlug":"coding","roles":["Software engineer"],"roleSlugs":["software-engineer"],"industries":["Software"],"industrySlugs":["software"],"jobs":["shipping-code","code-review"],"topic":"Dev & Coding","topicSlug":"dev-and-coding","score":18,"difficulty":"medium","estCost":"A Claude plan that includes Claude Code. Check Anthropic's current plan page. This catalog does not restate prices.","costTier":"low","timeSaved":"Several hours on a module you already understand well enough to review.","prompt":"Work only inside the directory I name. Do not touch unrelated packages.\n\n1. Read the module and name its public behavior in a short list.\n2. Add characterization tests for that behavior before changing production code. Use the test runner already in the repo.\n3. Refactor for readability. Do not change behavior. If a test fails after a refactor, revert that hunk.\n4. Show me the diff and the test command output.\n5. Stop if the module talks to the network or production data. Ask before stubbing those calls.\n\nDone means: tests you added are passing, and the public list still describes the code.","steps":[{"title":"Install Claude Code and open the repo","detail":"Use the current quickstart at code.claude.com. Run it from the repository root so the agent sees the real test script."},{"title":"Name one directory","detail":"A whole service is too large for a first refactor. Pick a module with a test runner already configured."},{"title":"Paste the prompt and watch the first test commit","detail":"If it starts editing production code before tests exist, stop the session and restate step 2."},{"title":"Read the diff yourself","detail":"Passing tests are not a review. Look for behavior hidden in snapshots or deleted assertions."},{"title":"Open a PR with the test command in the body","detail":"Claude Code can draft the branch. You merge."}],"sourceUrl":"https://code.claude.com/docs/en/overview","sourceNote":"Official documentation URL, checked when this seed was written. The prompt and steps are an original AI Playbook Gems playbook, not a quotation of a customer story, and have not been re-run.","date":"2026-10-15","verified":false,"status":"unverified","sponsorUrl":"","metrics":[{"label":"Hands-on time","value":"30–90 minutes of review"},{"label":"You still approve","value":"The merge"},{"label":"Cost bucket","value":"Low"}],"faq":[{"question":"Has this Claude Code playbook been re-run for the catalog?","answer":"No. Status is unverified. Paste the prompt, keep the version that worked, and do not treat the time or cost lines as measurements."},{"question":"Should it refactor the whole repository?","answer":"No. Constrain the directory. A wide prompt produces a diff nobody can review in one sitting."}],"url":"https://aiplaybookgems.com/usecases/claude-code-refactor-with-tests","jsonUrl":"https://aiplaybookgems.com/api/usecases/claude-code-refactor-with-tests"}