| name | tdd-enforcer |
| description | Enforces Test-Driven Development discipline with RED-GREEN-REFACTOR cycle |
| trigger | always |
| priority | 2 |
TDD Enforcer Skill
Enforces strict Test-Driven Development discipline. You cannot prompt your way into TDD discipline - you need forcing functions that make TDD the path of least resistance.
Activation
This skill activates when:
- Creating new features
- Implementing new functionality
- Keywords: "implement", "create", "add feature", "build"
TDD Cycle
Phase 1: RED (Write Failing Test First)
Before writing ANY implementation code:
-
Understand the requirement
- What behavior should the code exhibit?
- What are the inputs and expected outputs?
- What edge cases exist?
-
Write the test FIRST
// The test must:
// - Define expected behavior
// - Be specific and focused
// - FAIL when run (no implementation yet)
-
Run the test - confirm it FAILS
- If test passes without implementation → test is wrong
- Red phase complete only when test fails for right reason
Phase 2: GREEN (Minimum Implementation)
-
Write MINIMUM code to pass
- Don't over-engineer
- Don't add extra features
- Just make the test pass
-
Run the test - confirm it PASSES
- If still failing → fix implementation
- Green phase complete when test passes
Phase 3: REFACTOR (Improve Code)
- Refactor while keeping tests green
- Improve code structure
- Remove duplication
- Enhance readability
- Run tests after each change
Enforcement Rules
Pre-Implementation Check
Before writing any feature code, verify:
Blocking Conditions
BLOCK implementation if:
- No test exists for the feature
- Test already passes (skipped RED phase)
- Test doesn't cover the feature being implemented
Quality Gates
- Minimum 80% coverage for new code
- All tests must pass before commit
- No implementation without corresponding test
Framework Detection
JavaScript/TypeScript
npm test -- --coverage
npx vitest run --coverage
npx playwright test
Go
go test -cover -coverprofile=coverage.out ./...
go tool cover -html=coverage.out
Python
pytest --cov=. --cov-report=html
Example Workflow
## Task: Add user authentication
### 1. RED: Write failing test
```typescript
// auth.test.ts
describe('authenticateUser', () => {
it('should return token for valid credentials', async () => {
const result = await authenticateUser('user@test.com', 'password123');
expect(result.token).toBeDefined();
expect(result.expiresIn).toBe(3600);
});
it('should throw error for invalid credentials', async () => {
await expect(
authenticateUser('user@test.com', 'wrong')
).rejects.toThrow('Invalid credentials');
});
});
2. Run test → FAILS (function doesn't exist)
3. GREEN: Minimum implementation
export async function authenticateUser(email: string, password: string) {
const user = await findUserByEmail(email);
if (!user || !verifyPassword(password, user.passwordHash)) {
throw new Error('Invalid credentials');
}
return {
token: generateToken(user),
expiresIn: 3600
};
}
4. Run test → PASSES
5. REFACTOR: Improve code quality
- Extract constants
- Add input validation
- Improve error messages
## Integration with Hooks
### Pre-commit Hook
```bash
#!/bin/bash
# Block commits without test coverage
# Get changed files
changed_files=$(git diff --cached --name-only --diff-filter=ACM)
# Check for test files
for file in $changed_files; do
if [[ $file =~ \.(ts|js|go|py)$ ]] && [[ ! $file =~ (test|spec) ]]; then
# Implementation file - check for corresponding test
test_file="${file%.*}.test.${file##*.}"
if ! git diff --cached --name-only | grep -q "$test_file"; then
echo "ERROR: No test file for $file"
echo "TDD requires tests FIRST. Add $test_file"
exit 1
fi
fi
done
Metrics Tracked
- Tests written before implementation: Count
- RED-GREEN-REFACTOR cycles completed: Count
- Coverage percentage: Percentage
- Test-first compliance rate: Percentage