Test-Driven Development (TDD) Expert
You are a senior software engineer with expertise in Test-Driven Development (TDD). Help me implement TDD for this feature:
**Feature Context**:
- Feature Description: [WHAT ARE YOU BUILDING?]
- Technology Stack: [LANGUAGE/FRAMEWORK]
- Testing Framework: [JEST/PYTEST/RSPEC/etc.]
- Complexity Level: [SIMPLE/MODERATE/COMPLEX]
- Dependencies: [EXTERNAL_APIS/DATABASES/etc.]
- Performance Requirements: [SPEED/MEMORY_CONSTRAINTS]
Please provide:
1. **TDD Cycle**: Red-Green-Refactor approach
2. **Test Planning**: What to test and test structure
3. **Unit Tests**: Isolated component testing
4. **Integration Tests**: Component interaction testing
5. **Mocking Strategy**: External dependencies and services
6. **Test Data**: Fixtures and test data management
7. **Assertions**: Effective test assertions and expectations
8. **Code Coverage**: Measuring and improving coverage
9. **Test Maintenance**: Keeping tests up-to-date
10. **CI/CD Integration**: Automated testing in pipelines
11. **Best Practices**: TDD principles and patterns
12. **Common Pitfalls**: Mistakes to avoid in TDD
Complete TDD implementation guide covering the red-green-refactor cycle, testing strategies, and best practices for robust software development.
Sample
**TDD Example - User Authentication**:
```javascript
// 1. RED: Write failing test
describe('User Authentication', () => {
test('should authenticate valid user credentials', () => {
const authService = new AuthService();
const result = authService.login('user@example.com', 'password123');
expect(result.success).toBe(true);
expect(result.token).toBeDefined();
expect(result.user.email).toBe('user@example.com');
});
test('should reject invalid credentials', () => {
const authService = new AuthService();
const result = authService.login('user@example.com', 'wrongpassword');
expect(result.success).toBe(false);
expect(result.error).toBe('Invalid credentials');
});
});
// 2. GREEN: Write minimal code to pass
class AuthService {
login(email, password) {
// Minimal implementation
if (email === 'user@example.com' && password === 'password123') {
return { success: true, token: 'fake-token', user: { email } };
}
return { success: false, error: 'Invalid credentials' };
}
}
// 3. REFACTOR: Improve code quality
// Extract constants, improve error handling, add validation
```