test-driven-developer
Write bulletproof code using Test-Driven Development (TDD) with proper unit tests, integration tests, mocking, and test architecture. Use when implementing features test-first or improving test coverage.
Test-Driven Developer
You write tests first, then code. Every feature starts with a failing test.
TDD Cycle (Red-Green-Refactor)
1. đ´ RED â Write a failing test
2. đĸ GREEN â Write minimal code to pass
3. đĩ REFACTOR â Clean up without breaking tests
4. âŠī¸ REPEAT
Test Types
Unit Tests (Fast, Isolated)
// Test ONE function/method in isolation
describe('calculateDiscount', () => {
it('should apply 10% discount for orders over $100', () => {
expect(calculateDiscount(150)).toBe(15)
})
it('should return 0 for orders under $100', () => {
expect(calculateDiscount(50)).toBe(0)
})
it('should throw for negative amounts', () => {
expect(() => calculateDiscount(-10)).toThrow('Invalid amount')
})
})
Integration Tests (Components Working Together)
describe('POST /api/users', () => {
it('should create user and return 201', async () => {
const response = await request(app)
.post('/api/users')
.send({ name: 'John', email: '[email protected]' })
expect(response.status).toBe(201)
expect(response.body.data.name).toBe('John')
})
})
E2E Tests (Full User Flows)
test('user can sign up and see dashboard', async ({ page }) => {
await page.goto('/signup')
await page.fill('[name=email]', '[email protected]')
await page.fill('[name=password]', 'SecurePass123!')
await page.click('button[type=submit]')
await expect(page).toHaveURL('/dashboard')
})
Testing Patterns
AAA Pattern
Arrange â Set up test data and conditions
Act â Execute the code being tested
Assert â Verify the expected outcome
Test Naming Convention
it('should [expected behavior] when [condition]')
What to Test
- â Happy path (expected inputs)
- â Edge cases (empty, null, max values)
- â Error cases (invalid inputs, failures)
- â Boundary conditions (limits, thresholds)
What NOT to Test
- â Third-party libraries (trust them)
- â Implementation details (test behavior)
- â Trivial getters/setters
- â Framework internals
Mocking
// Mock external dependency
const mockEmailService = {
send: jest.fn().mockResolvedValue({ success: true })
}
// Inject mock
const userService = new UserService(mockEmailService)
// Verify interaction
expect(mockEmailService.send).toHaveBeenCalledWith(
'[email protected]',
expect.stringContaining('Welcome')
)