Raw view: TDD red/green/refactor loop
No invisible, zero-width, or bidi-control characters detected.
---
name: tdd-loop
description: Runs a strict red/green/refactor test-driven development loop with explicit stop points where the user confirms before code is written. Use when the user asks for TDD, says "write the test first", wants a failing test before implementation, or asks to add a feature or fix a bug test-first.
license: CC0-1.0
metadata:
author: Waxmark
version: "1.0.0"
---
# TDD Loop
The discipline is the point. Each phase has one job, and the loop stops at the marked points so the user stays in control of scope.
## Setup (once)
1. Find the test command and runner: look at `package.json` scripts, `pyproject.toml`, `Makefile`, `Cargo.toml`, or CI config. State the command you will use, e.g. `pnpm vitest run src/foo.test.ts`.
2. Locate an existing test near the code under change and copy its conventions (imports, fixtures, naming). Consistency beats cleverness.
3. Write the behavior list: 3-7 one-line statements of what the code should do, ordered simplest first. Show it.
**STOP 1:** Ask the user to confirm or edit the behavior list before any test is written.
## Loop (once per behavior)
### Red
- Write exactly one test for the next behavior. Name it after the behavior: `test_rejects_expired_token`, `it("returns empty list when no matches")`.
- Run only that test. It must fail for the right reason: an assertion failure or a missing symbol, not a syntax error or import typo.
- Show the failing output verbatim (trim to the relevant lines).
If the test passes immediately, the behavior already exists or the test is wrong. Say which and do not proceed to Green.
### Green
- Write the smallest change that makes the test pass. Hard-coding a return value is acceptable at this stage if the next test will force generalization.
- Run the single test, then the full file. Show results.
- Do not refactor, rename, or clean up in this phase.
### Refactor
- With all tests green, improve structure only: remove duplication, name things, extract helpers. No behavior change.
- Run the full suite after each refactor step. If anything goes red, revert that step.
**STOP 2 (after each cycle):** Report `Behavior N/M: <name> - green. Suite: <X passed>.` and wait for "next" unless the user asked to run the whole list unattended.
## Rules
- One failing test at a time. Two red tests means the loop was skipped.
- Never edit a test to make it pass unless the test itself was wrong, and say so explicitly when you do.
- Never write production code without a failing test that requires it.
- When a behavior turns out to need a design change, stop and describe it in two sentences before touching code.
## Output per cycle
```
### Behavior 3/5: returns 404 for unknown id
RED tests/api/users.test.ts - "expected 404, received 200"
GREEN src/api/users.ts +6 -1 - 1 test, 12 total passing
REFACTOR extracted notFound() helper - 12 passing
```
## Finish
After the last behavior: run the full suite once more, list files touched, and name any behaviors from the list that were deferred and why.