v2/examples/06-tutorials/sparc/sparc-tdd-guide.md
Learn how to use SPARC methodology for systematic TDD with Claude Flow.
SPARC stands for:
Define what we're building:
cd examples
../claude-flow sparc run spec-pseudocode \
"Design a user authentication system with:
- Email/password login
- JWT tokens
- Password reset
- Rate limiting
- Security best practices"
This creates:
Plan the implementation logic:
../claude-flow sparc run spec-pseudocode \
"Create pseudocode for authentication flow:
- Login process
- Token generation
- Password hashing
- Session management"
Output includes:
Design the system structure:
../claude-flow sparc run architect \
"Design authentication service architecture:
- API endpoints
- Database schema
- Security layers
- Integration points"
Creates:
Now we implement using Test-Driven Development:
../claude-flow sparc tdd \
"Implement user authentication system" \
--spec ./output/auth-spec.md \
--architecture ./output/auth-architecture.md
RED Phase - Write failing tests first:
// Generated test example
describe('Authentication', () => {
it('should hash passwords securely', async () => {
const password = 'testPass123';
const hash = await authService.hashPassword(password);
expect(hash).not.toBe(password);
expect(hash.length).toBeGreaterThan(50);
});
});
GREEN Phase - Implement minimal code to pass:
// Generated implementation
async hashPassword(password) {
const salt = await bcrypt.genSalt(10);
return bcrypt.hash(password, salt);
}
REFACTOR Phase - Optimize and clean up:
// Refactored version
async hashPassword(password, rounds = 10) {
if (!password || password.length < 8) {
throw new Error('Invalid password');
}
const salt = await bcrypt.genSalt(rounds);
return bcrypt.hash(password, salt);
}
Test the complete system:
../claude-flow sparc run integration \
"Test authentication system integration:
- API endpoints
- Database operations
- Token validation
- Error handling"
Ensure security best practices:
../claude-flow sparc run security-review \
"Review authentication system for:
- OWASP compliance
- SQL injection
- XSS vulnerabilities
- Token security"
Run multiple SPARC modes simultaneously:
../claude-flow sparc run parallel \
--modes "spec-pseudocode,architect,code" \
"Create payment processing system"
Store and retrieve SPARC artifacts:
# Store specifications
../claude-flow memory store auth_spec "$(cat ./output/auth-spec.md)"
# Retrieve for future work
../claude-flow memory query auth_spec > ./restored-spec.md
Define test templates:
../claude-flow sparc tdd \
"Create user service" \
--test-pattern "AAA" \ # Arrange-Act-Assert
--coverage-threshold 90
../claude-flow sparc tdd "implement login flow" \
--pattern "request-validate-authenticate-respond"
../claude-flow sparc tdd "implement user CRUD" \
--pattern "rest-api" \
--with-validation
../claude-flow sparc tdd "implement event handlers" \
--pattern "pub-sub" \
--async
You've learned:
Continue to Advanced SPARC Patterns