tdd
Test-Driven Development workflow - write tests first, then implementation
What this skill does
# Test-Driven Development (TDD)
Follow the Red-Green-Refactor cycle strictly. Never write production code without a failing test first.
## The TDD Cycle
```
┌─────────────────────────────────────────┐
│ │
│ ┌───────┐ │
│ │ RED │ Write a failing test │
│ └───┬───┘ │
│ │ │
│ ▼ │
│ ┌───────┐ │
│ │ GREEN │ Write minimal code to pass│
│ └───┬───┘ │
│ │ │
│ ▼ │
│ ┌──────────┐ │
│ │ REFACTOR │ Improve without │
│ └────┬─────┘ breaking tests │
│ │ │
│ └──────────────────────────────┘
```
## Rules (Non-Negotiable)
1. **No production code without a failing test**
- Write the test first
- Watch it fail
- Only then write implementation
2. **Write the minimum test to fail**
- One assertion per test
- Test behavior, not implementation
- Start with the simplest case
3. **Write the minimum code to pass**
- Don't anticipate future needs
- Hardcode if that makes it pass
- Generalize only when forced by tests
4. **Refactor only when green**
- All tests must pass before refactoring
- Keep tests passing during refactoring
- Small steps only
## Workflow
### Phase 1: RED (Write Failing Test)
```typescript
// Start with what you want the API to look like
describe('Calculator', () => {
it('should add two numbers', () => {
const calc = new Calculator();
expect(calc.add(2, 3)).toBe(5);
});
});
```
Run test → Watch it fail → Confirm it fails for the RIGHT reason
### Phase 2: GREEN (Make It Pass)
```typescript
// Write the SIMPLEST code that passes
class Calculator {
add(a: number, b: number): number {
return 5; // Yes, hardcoding is fine here!
}
}
```
Run test → Watch it pass → Celebrate (briefly)
### Phase 3: Add Another Test (Force Generalization)
```typescript
it('should add different numbers', () => {
const calc = new Calculator();
expect(calc.add(1, 1)).toBe(2); // Forces real implementation
});
```
### Phase 4: GREEN Again
```typescript
class Calculator {
add(a: number, b: number): number {
return a + b; // Now we generalize
}
}
```
### Phase 5: REFACTOR
With green tests, improve the code:
- Remove duplication
- Improve names
- Extract methods
- Keep tests green throughout
## Test Naming Convention
Use this pattern: `should [expected behavior] when [condition]`
```typescript
it('should return empty array when input is null')
it('should throw ValidationError when email is invalid')
it('should retry 3 times when connection fails')
```
## TDD Checklist
Before writing ANY code, verify:
- [ ] I have a failing test
- [ ] The test fails for the expected reason
- [ ] The test is testing behavior, not implementation
- [ ] The test name describes what it verifies
Before marking GREEN:
- [ ] Test passes
- [ ] I wrote the minimum code necessary
- [ ] I didn't add "extra" functionality
Before REFACTORING:
- [ ] All tests are green
- [ ] I have a specific improvement in mind
- [ ] Changes are small and incremental
## Common TDD Mistakes
### ❌ Writing Tests After Code
This is not TDD. Tests written after tend to test implementation, not behavior.
### ❌ Writing Too Many Tests at Once
Write ONE test, make it pass, then write the next. Stay in the cycle.
### ❌ Making Big Jumps
If your implementation is more than a few lines, you skipped steps. Add intermediate tests.
### ❌ Testing Implementation Details
```typescript
// BAD - tests implementation
expect(user._hashedPassword).toMatch(/^[a-f0-9]{64}$/);
// GOOD - tests behavior
expect(user.verifyPassword('correct')).toBe(true);
```
### ❌ Refactoring While Red
Never refactor with failing tests. Get green first.
## Starting a New Feature with TDD
1. List behaviors the feature needs (user stories → test cases)
2. Order from simplest to most complex
3. Write first test for simplest behavior
4. Cycle through Red-Green-Refactor
5. Add tests for edge cases
6. Add tests for error handling
## Example Session
**Goal:** Implement a `slugify` function
```typescript
// Test 1: Simplest case
it('should lowercase the input', () => {
expect(slugify('Hello')).toBe('hello');
});
// Implementation: return input.toLowerCase();
// Test 2: Handle spaces
it('should replace spaces with hyphens', () => {
expect(slugify('Hello World')).toBe('hello-world');
});
// Implementation: return input.toLowerCase().replace(/ /g, '-');
// Test 3: Handle special characters
it('should remove special characters', () => {
expect(slugify('Hello, World!')).toBe('hello-world');
});
// Implementation: add .replace(/[^a-z0-9-]/g, '');
// Test 4: Edge case
it('should handle empty string', () => {
expect(slugify('')).toBe('');
});
// Already passes! No change needed.
```
## Remember
> "TDD is not about testing. It's about design."
The tests drive you toward better, more modular code. Trust the process.
Related in Code Review
gstack
IncludedFast headless browser for QA testing and site dogfooding. Navigate pages, interact with elements, verify state, diff before/after, take annotated screenshots, test responsive layouts, forms, uploads, dialogs, and capture bug evidence. Use when asked to open or test a site, verify a deployment, dogfood a user flow, or file a bug with screenshots. (gstack)
startup-due-diligence
IncludedLegal due diligence review for seed-stage and Series A startups (US, Delaware C-Corp focus). Supports both investor and founder perspectives. Capabilities include: (1) Interactive document review and issue spotting; (2) Document request list generation; (3) Cap table and SAFE/convertible note analysis; (4) Red flag identification with severity ratings; (5) Diligence report generation. TRIGGERS: due diligence, DD, startup investment, cap table review, Series A, seed round, investor diligence, legal review startup, SAFE analysis, convertible note, 409A, founder vesting.
interview-master
IncludedThis skill should be used when the user asks to "generate interview questions", "prepare for interview", "optimize resume", "conduct mock interview", "analyze git commits for resume", "generate resume from code", "review my resume", or mentions interview preparation, career assistance, or extracting project experience from git history. Provides comprehensive interview and career development guidance for both job seekers and interviewers.
fix-issue
IncludedFixes GitHub issues using parallel analysis agents for root cause investigation, code exploration, and regression detection. Reads issue context from gh CLI, searches codebase and memory for related patterns, generates a fix with tests, and links the resolution back to the issue via PR. Includes prevention analysis to avoid recurrence. Use when debugging errors, resolving regressions, fixing bugs, or triaging issues.
sf-apex
IncludedGenerates and reviews Salesforce Apex code with 150-point scoring. TRIGGER when: user writes, reviews, or fixes Apex classes, triggers, test classes, batch/queueable/schedulable jobs, or touches .cls/.trigger files. DO NOT TRIGGER when: LWC JavaScript (use sf-lwc), Flow XML (use sf-flow), SOQL-only queries (use sf-soql), or non-Salesforce code.
swift-development
IncludedComprehensive Swift development for building, testing, and deploying iOS/macOS applications. Use when Claude needs to: (1) Build Swift packages or Xcode projects from command line, (2) Run tests with XCTest or Swift Testing framework, (3) Manage iOS simulators with simctl, (4) Handle code signing, provisioning profiles, and app distribution, (5) Format or lint Swift code with SwiftFormat/SwiftLint, (6) Work with Swift Package Manager (SPM), (7) Implement Swift 6 concurrency patterns (async/await, actors, Sendable), (8) Create SwiftUI views with MVVM architecture, (9) Set up Core Data or SwiftData persistence, or any other Swift/iOS/macOS development tasks.