| name | arc-tdd |
| description | Drive implementation test-first through RED, GREEN, REFACTOR. Use when about to write any feature or bugfix code — write the failing test first, then the minimum code to pass, then refactor. Enforces the Iron Law for implementation. |
| category | discipline |
| status | promoted |
Test-Driven Development (TDD)
Overview
Write the test first. Watch it fail. Write minimal code to pass.
Core principle: If you didn't watch the test fail, you don't know if it tests the right thing.
Violating the letter of the rules is violating the spirit of the rules.
When to Use
Always: new features, bug fixes, refactoring, behavior changes.
Exceptions (ask user): throwaway prototypes, generated code, configuration files.
Thinking "skip TDD just this once"? Stop. That's rationalization.
Before Implementation
Find similar implementations first. Before implementing, ask: "Is there a similar existing implementation I can reference?" A reference reduces mistakes, matches project conventions, and avoids starting from scratch.
REQUIRED SUB-SKILL: This aligns with arc-debugging Phase 2 (Pattern Analysis) — applied to implementation context.
Failure Handling
| Attempts | Action |
|---|
| 1-2 | Normal debugging |
| 3 | Ask user if there's another reference to follow |
| 3+ | Stop, document, discuss with user |
REQUIRED SUB-SKILL: For 3+ failures, see arc-debugging Phase 4.4-4.5 (question architecture).
The Iron Law
NO PRODUCTION CODE WITHOUT A FAILING TEST FIRST
Write code before the test? Delete it. Start over.
No exceptions:
- Don't keep it as "reference"
- Don't "adapt" it while writing tests
- Don't look at it
- Delete means delete
Implement fresh from tests. Period.
Red-Green-Refactor
RED - Write Failing Test
Write one minimal test showing what should happen.
Requirements:
- One behavior
- Clear name
- Real code (no mocks unless unavoidable)
See references/examples.md for good-vs-bad RED and GREEN examples.
Verify RED - Watch It Fail
MANDATORY. Never skip.
npm test path/to/test.test.ts
Confirm:
- Test fails (not errors)
- Failure message is expected
- Fails because feature missing (not typos)
Test passes? You're testing existing behavior. Fix test.
Test errors? Fix error, re-run until it fails correctly.
GREEN - Minimal Code
Write the simplest code to pass the test. Don't add features, refactor other code, or "improve" beyond the test.
Verify GREEN - Watch It Pass
MANDATORY.
npm test path/to/test.test.ts
Confirm:
- Test passes
- Other tests still pass
- Output pristine (no errors, warnings)
Test fails? Fix code, not test.
Other tests fail? Fix now.
REFACTOR - Clean Up
After green only: remove duplication, improve names, extract helpers. Keep tests green. Don't add behavior.
Repeat
Next failing test for next feature.
Good Tests
| Quality | Good | Bad |
|---|
| Minimal | One thing. "and" in name? Split it. | test('validates email and domain and whitespace') |
| Clear | Name describes behavior | test('test1') |
| Shows intent | Demonstrates desired API | Obscures what code should do |
Common Rationalizations
| Excuse | Reality |
|---|
| "Too simple to test" | Simple code breaks. Test takes 30 seconds. |
| "I'll test after" | Tests passing immediately prove nothing. |
| "Tests after achieve same goals" | Tests-after = "what does this do?" Tests-first = "what should this do?" |
| "Already manually tested" | Ad-hoc ≠ systematic. No record, can't re-run. |
| "Deleting X hours is wasteful" | Sunk cost fallacy. Keeping unverified code is technical debt. |
| "Keep as reference, write tests first" | You'll adapt it. That's testing after. Delete means delete. |
| "Need to explore first" | Fine. Throw away exploration, start with TDD. |
| "Test hard = design unclear" | Listen to test. Hard to test = hard to use. |
| "TDD will slow me down" | TDD faster than debugging. Pragmatic = test-first. |
| "Manual test faster" | Manual doesn't prove edge cases. You'll re-test every change. |
| "Existing code has no tests" | You're improving it. Add tests for existing code. |
Any of these means: delete the code, start over with TDD.
Verification Checklist
Before marking work complete:
Can't check all boxes? You skipped TDD. Start over.
When Stuck
| Problem | Solution |
|---|
| Don't know how to test | Write wished-for API. Write assertion first. Ask your human partner. |
| 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. |
Debugging Integration
Bug found? Write a failing test reproducing it, then follow the TDD cycle. The test proves the fix and prevents regression. Never fix bugs without a test.
Testing Anti-Patterns
When adding mocks or test utilities, read testing-anti-patterns.md to avoid testing mock behavior instead of real behavior, adding test-only methods to production classes, or mocking without understanding dependencies.
Final Rule
Production code → test exists and failed first
Otherwise → not TDD
No exceptions without your user's permission.
- arc-debugging: Phase 4 requires a failing test before fixing.
- arc-writing-skills: Requires TDD mindset for documentation tests.
- arc-verifying: Do not claim pass without fresh test output.