Skip to content
Development
Skill

/test-preserving-refactor

Use when code needs restructuring and observable behavior must stay unchanged, with existing tests kept green at every step.

From plugin
agent-powerups
8114 skills46 agents54 commands

Claude Code

$ npx -y skills add yeaight7/agent-powerups --skill test-preserving-refactor --agent claude-code

SKILL.md

test-preserving-refactor.SKILL.md
name: test-preserving-refactor
description: Use when code needs restructuring and observable behavior must stay unchanged, with existing tests kept green at every step.

Purpose

Refactoring is only safe if it is backed by tests. Work in a strict loop — green baseline, one structural change, immediate re-run, revert on red — so you are never trapped in an uncompilable state.

When to Use

  • Code needs restructuring (extract/inline/move) without behavior change
  • A previous refactor spiraled into a broken, hard-to-recover state
  • Tests covering the target area exist or can be run

Inputs

  • The target area and the command that runs just its tests

Workflow

1. **Run tests first.** Before touching any code, run the tests covering the target area. They MUST be green. If they are red, stop and fix the tests (or the code) first:

   npx jest path/to/target           # or: pytest tests/test_target.py -q

2. **Small steps.** Make one structural change at a time (e.g., extract a method).

3. **Run tests immediately** after the single structural change. The per-change loop:

   # repeat per structural change: edit -> test -> commit (green) | revert (red)
   npx jest path/to/target && git commit -am "refactor: extract X" || git checkout -- .

4. **Revert on red.** If the tests fail, you made a mistake. Revert the change (`git checkout -- .` or `git restore .`) and try a different approach. Do not attempt to "fix" the refactor while tests are failing.

5. **Commit on green.** Once the small change is green, consider it a safe checkpoint.

This strict loop prevents you from getting trapped in an uncompilable state.

Output

  • A chain of small, individually green commits
  • The final test run output proving unchanged behavior

Verification

  • [ ] Baseline test run was green before the first edit
  • [ ] Exactly one structural change per test cycle
  • [ ] Every red result handled by revert, not forward-fixing
  • [ ] Each green step committed as a checkpoint
  • [ ] No test was modified to make the refactor pass

Failure Modes

  • **Refactoring on red** — starting from failing tests means you cannot tell what you broke.
  • **Step batching** — three changes per test run; when it fails, you don't know which one did it.
  • **Forward-fixing a broken refactor** — "fixing" while red digs the hole deeper; revert is cheaper.
  • **Bending the tests** — editing assertions to match new behavior is a behavior change, not a refactor.
Read more
@yeaight7
Ships withagent-powerups

Curated power-ups for coding agents: skills, slash commands, MCP configs, hooks, AGENTS.md templates, and workflows for serious software engineering. Claude Code, Codex, Antigravity CLI, Cursor and more

8114 skills46 agents
Get the whole plugin

Stars on GitHub

Momentum

stars in total
8
Star history of yeaight7/agent-powerups on GitHub, a chart by star-history.com

Other skills on agent-powerups.

More skills from other plugins.

brainstorming
Auto-invoked

brainstorming

You MUST use this before any creative work - creating features, building components, adding functionality, modifying behavior, or…

@obra@obraInstall Skill
ask-matt

ask-matt

Ask which skill or flow fits your situation. A router over the skills in this repo.

skills12d309k
@mattpocock@mattpocockInstall Skill
accessibility
Auto-invoked

accessibility

Design, implement, and audit accessible UI to WCAG 2.2 Level AA across Web, iOS, and Android — semantic ARIA roles and labels,…

ecc13d293k
@affaan-m@affaan-mInstall Skill
compiler-commit

compiler-commit

Use when you want to verify compiler changes and commit with the correct convention. Runs tests, lint, and format, then commits…

react3d252k
@facebook@facebookInstall Skill

Questions about test-preserving-refactor.

Which coding agents can use test-preserving-refactor?
test-preserving-refactor installs in Claude Code, Codex, Cursor, Gemini CLI, GitHub Copilot, OpenCode and Windsurf with the skills CLI, using each agent's flag shown on this page. Flowy's auto-invocation, which fires a skill when your prompt matches, runs in Claude Code only.