TDD: Test-Driven Development Methodology
Execute tasks using strict TDD (RED → GREEN → REFACTOR). Outcome: Tasks completed with Happy/Failure tests passing, minimal code shipped.
Iron Law
NO PRODUCTION CODE WITHOUT A FAILING TEST FIRST
Wrote code before the test? Delete it. Start over. Don't keep it as "reference." Don't "adapt" it. Delete means delete. Implement fresh from tests.
Rules
- 2 tests per Test Opportunity (TO): 1 Happy path, 1 Failure path — then stop
- Scoped execution: Never run repo-wide tests; use
--testPathPattern, --findRelatedTests, or per-file lint
- YAGNI: No abstractions unless test forces it or ≥2 call sites exist
- Anti-flake: Use fake timers, stubs, seeded RNG
Step 1 - Generate TDD TODO List
- Action — ParseTaskList: Extract tasks from ARGUMENTS or thread context
- If no clear tasks → stop and ask for guidance
- Action — IdentifyTestOpportunities: Derive TOs (smallest behavior unit: function, route, bug fix, acceptance criterion)
- Action — TransformToTDD: Convert each TO to cycle using TodoWrite:
RED: Happy — {test} → RED: Failure — {test} → GREEN: Minimal impl → REFACTOR: Tidy → COMMIT
- Action — VerifyScope: Confirm TODO contains ONLY assigned tasks
Step 2 - RED Phase: Write Failing Tests
- Action — WriteHappyTest: Write first failing test (happy path)
- Execute only this test/file, not entire suite
- Action — WriteFailureTest: Write second failing test (primary failure mode)
- Action — VerifyRed: MANDATORY — Confirm each test:
- Fails (not errors)
- Fails for expected reason (feature missing, not typo)
- If passes → you're testing existing behavior; fix test
Step 3 - GREEN Phase: Minimal Implementation
- Action — ImplementMinimal: Write least code to pass tests
- No extra branches, params, or dependencies unless test forces them
- Action — VerifyGreen: MANDATORY — Run tests (narrowest scope)
- If fail → fix code, not test
- Remove any speculative code not forced by tests
Step 4 - REFACTOR Phase: Clean Code
- Action — RefactorSafely: Improve only if duplication ≥3 OR readability materially improves
- Keep tests green; If tests fail → revert
- Action — HandleLintFailures: Apply in order until clear:
- Guard clauses, split compound expressions
- Extract tiny private helpers (same file)
- Hoist literals to file constants
- Split into orchestrator + helpers
- Only if still failing: same-directory helper module
Step 5 - Loop or Complete
- If more TOs → return to Step 2
- Else → proceed to Step 6
Step 6 - Commit & Report
- Action — CommitCode: Conventional format (
feat({task}): description)
- Action — GenerateReport:
- Summary: Tasks completed, test status (✅ Happy ✅ Failure), files modified
- Artifacts: Test helpers, mocks, fixtures created
- API Surface: New/modified exports with signatures
- Patterns: Code/testing patterns to follow
- Deferred: Coverage gaps for follow-up
Red Flags — STOP and Restart
If any of these occur, delete code and start over with TDD:
| Red Flag |
Why It's Wrong |
| Code written before test |
Violates Iron Law |
| Test passes immediately |
Testing existing behavior, not new |
| Can't explain why test failed |
Don't understand what you're testing |
| "Just this once" thinking |
Rationalization — TDD has no exceptions |
| Keeping code "as reference" |
You'll adapt it; that's tests-after |
When Stuck
| Problem |
Solution |
| Don't know how to test |
Write wished-for API first, then assert on it |
| Test too complicated |
Design too complicated — simplify interface |
| Must mock everything |
Code too coupled — use dependency injection |
| Test setup huge |
Extract helpers; still complex? Simplify design |
Pre-Completion Checklist
Before marking complete, verify:
Converted and distributed by TomeVault — claim your Tome and manage your conversions.
1---2name: spectre-tdd3description: Load this skill when executing TDD (Test-Driven Development) methodology. Use when implementing features via strict RED-GREEN-REFACTOR cycles, or when a prompt instructs execution via TDD. Use when this capability is needed.4---56# TDD: Test-Driven Development Methodology78Execute tasks using strict TDD (RED → GREEN → REFACTOR). Outcome: Tasks completed with Happy/Failure tests passing, minimal code shipped.910## Iron Law1112```13NO PRODUCTION CODE WITHOUT A FAILING TEST FIRST14```1516Wrote code before the test? **Delete it. Start over.** Don't keep it as "reference." Don't "adapt" it. Delete means delete. Implement fresh from tests.1718## Rules1920- **2 tests per Test Opportunity (TO)**: 1 Happy path, 1 Failure path — then stop21- **Scoped execution**: Never run repo-wide tests; use `--testPathPattern`, `--findRelatedTests`, or per-file lint22- **YAGNI**: No abstractions unless test forces it or ≥2 call sites exist23- **Anti-flake**: Use fake timers, stubs, seeded RNG2425---2627## Step 1 - Generate TDD TODO List2829- **Action** — ParseTaskList: Extract tasks from ARGUMENTS or thread context30 - **If** no clear tasks → stop and ask for guidance31- **Action** — IdentifyTestOpportunities: Derive TOs (smallest behavior unit: function, route, bug fix, acceptance criterion)32- **Action** — TransformToTDD: Convert each TO to cycle using TodoWrite:33 - `RED: Happy — {test}` → `RED: Failure — {test}` → `GREEN: Minimal impl` → `REFACTOR: Tidy` → `COMMIT`34- **Action** — VerifyScope: Confirm TODO contains ONLY assigned tasks3536## Step 2 - RED Phase: Write Failing Tests3738- **Action** — WriteHappyTest: Write first failing test (happy path)39 - Execute only this test/file, not entire suite40- **Action** — WriteFailureTest: Write second failing test (primary failure mode)41- **Action** — VerifyRed: **MANDATORY** — Confirm each test:42 - Fails (not errors)43 - Fails for expected reason (feature missing, not typo)44 - **If** passes → you're testing existing behavior; fix test4546## Step 3 - GREEN Phase: Minimal Implementation4748- **Action** — ImplementMinimal: Write least code to pass tests49 - No extra branches, params, or dependencies unless test forces them50- **Action** — VerifyGreen: **MANDATORY** — Run tests (narrowest scope)51 - **If** fail → fix code, not test52 - Remove any speculative code not forced by tests5354## Step 4 - REFACTOR Phase: Clean Code5556- **Action** — RefactorSafely: Improve only if duplication ≥3 OR readability materially improves57 - Keep tests green; **If** tests fail → revert58- **Action** — HandleLintFailures: Apply in order until clear:59 1. Guard clauses, split compound expressions60 2. Extract tiny private helpers (same file)61 3. Hoist literals to file constants62 4. Split into orchestrator + helpers63 5. Only if still failing: same-directory helper module6465## Step 5 - Loop or Complete6667- **If** more TOs → return to Step 268- **Else** → proceed to Step 66970## Step 6 - Commit & Report7172- **Action** — CommitCode: Conventional format (`feat({task}): description`)73- **Action** — GenerateReport:74 - **Summary**: Tasks completed, test status (✅ Happy ✅ Failure), files modified75 - **Artifacts**: Test helpers, mocks, fixtures created76 - **API Surface**: New/modified exports with signatures77 - **Patterns**: Code/testing patterns to follow78 - **Deferred**: Coverage gaps for follow-up7980---8182## Red Flags — STOP and Restart8384If any of these occur, delete code and start over with TDD:8586| Red Flag | Why It's Wrong |87|----------|----------------|88| Code written before test | Violates Iron Law |89| Test passes immediately | Testing existing behavior, not new |90| Can't explain why test failed | Don't understand what you're testing |91| "Just this once" thinking | Rationalization — TDD has no exceptions |92| Keeping code "as reference" | You'll adapt it; that's tests-after |9394## When Stuck9596| Problem | Solution |97|---------|----------|98| Don't know how to test | Write wished-for API first, then assert on it |99| Test too complicated | Design too complicated — simplify interface |100| Must mock everything | Code too coupled — use dependency injection |101| Test setup huge | Extract helpers; still complex? Simplify design |102103## Pre-Completion Checklist104105Before marking complete, verify:106- [ ] Every new function has a test107- [ ] Watched each test fail before implementing108- [ ] Each failure was for expected reason109- [ ] Wrote minimal code to pass110- [ ] All tests pass, output clean111- [ ] Mocks used only when unavoidable112113---114> Converted and distributed by [TomeVault](https://tomevault.io/claim/codename-inc) — claim your Tome and manage your conversions.115<!-- tomevault:4.0:skill_md:2026-04-11 -->