TDD Cycle Command
Orchestrates test-driven development with a strict red-green-refactor cycle. Write the failing test first, implement the minimum code to pass, then refactor. Ensures high test coverage and clean design through disciplined TDD practice.
Command
/tdd
Description
Guides you through a strict test-driven development cycle: write a failing test (red), implement the minimum code to make it pass (green), then refactor while keeping tests green. Enforces discipline by verifying each phase before moving to the next.
Behavior
Arguments
$ARGUMENTS-- What to implement (e.g., "password validation function with strength scoring")
The TDD Cycle
Step 1: RED -- Write the Failing Test
- Create or open the test file
- Write a single, focused test case for the smallest unit of behavior
- Run the test and verify it fails with the expected reason
- The test should fail because the function doesn't exist or returns the wrong value -- NOT because of syntax errors
# Run the test -- must FAIL npm test -- --grep "should reject passwords shorter than 8 characters" # Expected: FAIL (function not implemented yet)
Step 2: GREEN -- Write Minimum Code to Pass
- Write the simplest implementation that makes the test pass
- Do NOT add extra functionality, handle edge cases, or optimize
- Hardcoded return values are acceptable if they make the test pass
- Run the test and verify it passes
# Run the test -- must PASS npm test -- --grep "should reject passwords shorter than 8 characters" # Expected: PASS
Step 3: REFACTOR -- Improve While Green
- Clean up the implementation (remove duplication, improve names)
- Run all tests after each refactor step
- If any test fails, undo the last change immediately
- Do NOT add new behavior during refactoring
# Run ALL tests -- must ALL pass npm test # Expected: ALL PASS
Step 4: Repeat
Add the next test case and restart the cycle:
RED: "should accept passwords with 8+ characters"
GREEN: Add length check >= 8
REFACTOR: Extract length constant
RED: "should require at least one uppercase letter"
GREEN: Add /[A-Z]/ check
REFACTOR: Combine regex patterns
RED: "should return strength score 0-4"
GREEN: Add scoring logic
REFACTOR: Extract scoring rules into config
Rules
- NEVER write implementation before the test
- NEVER write more than one failing test at a time
- NEVER add behavior during the refactor phase
- Each test should be independent and isolated
- Test names should describe behavior, not implementation
- Keep the cycle fast -- each iteration should take 2-5 minutes
Output Format
Report each cycle iteration:
## TDD Cycle: Password Validator ### Iteration 1 - RED: `should reject empty passwords` -- FAILS (function not found) - GREEN: Created `validatePassword()` returning `{valid: false}` for empty input -- PASSES - REFACTOR: No refactoring needed yet ### Iteration 2 - RED: `should reject passwords under 8 chars` -- FAILS (no length check) - GREEN: Added `input.length >= 8` check -- PASSES - REFACTOR: Extracted `MIN_LENGTH = 8` constant ### Current Test Coverage: 4 tests, 100% on validatePassword()
Examples
# TDD a new function /tdd password validation with strength scoring # TDD a React component /tdd search autocomplete component with debounce # TDD an API endpoint /tdd POST /api/bookmarks endpoint with validation
Reviews
No reviews yet. Be the first to review this template!
Similar Templates
Git Commit Message Generator
Generates well-structured conventional commit messages by analyzing staged changes. Follows Conventional Commits spec with scope detection.
React Component Scaffolder
Scaffolds a complete React component with TypeScript types, Tailwind styles, Storybook stories, and unit tests. Follows project conventions automatically.
CI/CD Pipeline Generator
Generates GitHub Actions workflows for CI/CD including linting, testing, building, and deploying. Detects project stack automatically.