Cursor Refactor of a Component With Zero Behaviour Change

Has the Cursor agent restructure a messy component into smaller pieces while proving the behaviour stays identical, using existing tests or new characterization tests first.

You get: the behaviour inventory, the list of refactor steps with the test result after each, a before-and-after summary (line count, number of components, what moved where), bugs noticed…

More like this

The prompt

cursor-refactor-of-a-component-with-zero-behaviour-change.md
You are the Cursor agent. Refactor the component in {file path of the component} to make it easier to read and change, without changing what it does.

Rules:
1. Before editing, write down the current behaviour: props, rendered output in each state, events, side effects and anything it exports.
2. If tests do not cover that behaviour, add characterization tests first and make sure they pass against the current code.
3. Refactor in small steps: extract subcomponents or hooks, remove duplication, name things clearly. Run the tests after each step.
4. Keep the public interface identical: same props, exports, class names used by styles or tests, and accessibility attributes.
5. Do not fix bugs you find along the way. List them instead, because a fix is a behaviour change.

Output format: the behaviour inventory, the list of refactor steps with the test result after each, a before-and-after summary (line count, number of components, what moved where), bugs noticed but not fixed, and any assumptions or risks, such as untested paths.

Tips and common edits

Run it in Cursor agent mode with the component file open. Commit before starting so you can diff or revert the whole refactor in one go.

Questions about this prompt

More in Cursor

Cursor Characterization-Test-First Refactor of a Legacy Module

Has Cursor lock in current behavior with characterization tests before restructuring a legacy module in small steps, so you can decide whether the refactor preserved behavior and is safe to merge.

Characterization Tests Before a Refactor

Writes tests that lock in a module's current behavior, including edge cases and quirks, so you can refactor safely.

Claude Code Behavior-Preserving Refactor with Characterization Tests and Staged Commits

Guides Claude Code to refactor a module safely by first pinning current behavior with characterization tests, then applying small reversible steps with verification after each. Delivers a diff, risk register, and rollback plan.

Cursor Incremental Dependency Migration Plan and Codemod Execution

Plans and executes a library or framework version migration in Cursor in small, verifiable batches, with an impact inventory, rollback points, and per-batch test gates instead of one risky bulk edit.

Unit Test Plan and Test Code for an Existing Function

Builds a behavior-based test matrix for a function or module and then generates test code in your framework. Covers edge cases, failures, and mocking boundaries, with coverage gaps stated honestly.

Codebase Tour for a Specific Task

Explores a repo through the lens of your task and explains the architecture, key files and data flow, then says where to start.

Coding Agent Task Brief with Guardrails, Test Plan, and Rollback Steps

Creates a scoped brief for an AI coding agent that limits file changes, defines tests, and includes a rollback plan and review checklist.

Cursor Agent Bug Triage: Reproduce, Root-Cause, Fix, and Regression Test

Guides Cursor Agent through reproducing a bug, isolating the root cause with evidence, applying a minimal diff, and adding a regression test, ending with a verification checklist and risks.

Cursor Project Rules Generator for Your Repo's Stack

Has Cursor inspect your repository and write 3-5 scoped .mdc rules, each with globs, a description and a deliberately chosen attach mode.

Dependency Graph Planner for a Multi-Agent Software Release Workflow

Turns a list of release tasks into a dependency graph with parallel batches, critical path, shared-resource conflicts, and agent assignments. Supports the decision on what to run now versus wait on.

Failing Test First Bug Fix

Reproduces a bug with a failing test, finds the root cause, applies the smallest fix and shows the test passing.

Plan Before Edit: Cursor Plan-Mode Prompt for a Feature Request

Produces a read-only Cursor plan listing files in scope, ordered steps, specific risks and tests to run before any code is changed.