Test-Driven Development Workflow
This skill ensures all code development follows TDD principles with comprehensive test coverage.
When to Activate
- Writing new features or functionality
- Fixing bugs or issues
- Refactoring existing code
- Adding API endpoints
- Creating new components
Core Principles
1. Tests BEFORE Code
ALWAYS write tests first, then implement code to make tests pass.
2. Coverage Requirements
- Minimum 80% coverage (unit + integration + E2E)
- All edge cases covered
- Error scenarios tested
- Boundary conditions verified
3. Test Types
Unit Tests
- Individual functions and utilities
- Component logic
- Pure functions
- Helpers and utilities
Integration Tests
- API endpoints
- Database operations
- Service interactions
- External API calls
E2E Tests (Playwright)
- Critical user flows
- Complete workflows
- Browser automation
- UI interactions
4. Git Checkpoints
- If the repository is under Git, create a checkpoint commit after each TDD stage
- Do not squash or rewrite these checkpoint commits until the workflow is complete
- Each checkpoint commit message must describe the stage and the exact evidence captured
- Count only commits created on the current active branch for the current task
- Do not treat commits from other branches, earlier unrelated work, or distant branch history as valid checkpoint evidence
- Before treating a checkpoint as satisfied, verify that the commit is reachable from the current
HEAD on the active branch and belongs to the current task sequence
- The preferred compact workflow is:
- one commit for failing test added and RED validated
- one commit for minimal fix applied and GREEN validated
- one optional commit for refactor complete
- Separate evidence-only commits are not required if the test commit clearly corresponds to RED and the fix commit clearly corresponds to GREEN
TDD Workflow Steps
Step 1: Write User Journeys
As a [role], I want to [action], so that [benefit]
Example:
As a user, I want to search for markets semantically,
so that I can find relevant markets even without exact keywords.
Step 2: Generate Test Cases
For each user journey, create comprehensive test cases:
typescript
1describe('Semantic Search', () => {
2 it('returns relevant markets for query', async () => {
3 // Test implementation
4 })
5
6 it('handles empty query gracefully', async () => {
7 // Test edge case
8 })
9
10 it('falls back to substring search when Redis unavailable', async () => {
11 // Test fallback behavior
12 })
13
14 it('sorts results by similarity score', async () => {
15 // Test sorting logic
16 })
17})
Step 3: Run Tests (They Should Fail)
bash
1npm test
2# Tests should fail - we haven't implemented yet
This step is mandatory and is the RED gate for all production changes.
Before modifying business logic or other production code, you must verify a valid RED state via one of these paths:
- Runtime RED:
- The relevant test target compiles successfully
- The new or changed test is actually executed
- The result is RED
- Compile-time RED:
- The new test newly instantiates, references, or exercises the buggy code path
- The compile failure is itself the intended RED signal
- In either case, the failure is caused by the intended business-logic bug, undefined behavior, or missing implementation
- The failure is not caused only by unrelated syntax errors, broken test setup, missing dependencies, or unrelated regressions
A test that was only written but not compiled and executed does not count as RED.
Do not edit production code until this RED state is confirmed.
If the repository is under Git, create a checkpoint commit immediately after this stage is validated.
Recommended commit message format:
test: add reproducer for <feature or bug>
- This commit may also serve as the RED validation checkpoint if the reproducer was compiled and executed and failed for the intended reason
- Verify that this checkpoint commit is on the current active branch before continuing
Step 4: Implement Code
Write minimal code to make tests pass:
typescript
1// Implementation guided by tests
2export async function searchMarkets(query: string) {
3 // Implementation here
4}
If the repository is under Git, stage the minimal fix now but defer the checkpoint commit until GREEN is validated in Step 5.
Step 5: Run Tests Again
bash
1npm test
2# Tests should now pass
Rerun the same relevant test target after the fix and confirm the previously failing test is now GREEN.
Only after a valid GREEN result may you proceed to refactor.
If the repository is under Git, create a checkpoint commit immediately after GREEN is validated.
Recommended commit message format:
fix: <feature or bug>
- The fix commit may also serve as the GREEN validation checkpoint if the same relevant test target was rerun and passed
- Verify that this checkpoint commit is on the current active branch before continuing
Step 6: Refactor
Improve code quality while keeping tests green:
- Remove duplication
- Improve naming
- Optimize performance
- Enhance readability
If the repository is under Git, create a checkpoint commit immediately after refactoring is complete and tests remain green.
Recommended commit message format:
refactor: clean up after <feature or bug> implementation
- Verify that this checkpoint commit is on the current active branch before considering the TDD cycle complete
Step 7: Verify Coverage
bash
1npm run test:coverage
2# Verify 80%+ coverage achieved
Testing Patterns
Unit Test Pattern (Jest/Vitest)
typescript
1import { render, screen, fireEvent } from '@testing-library/react'
2import { Button } from './Button'
3
4describe('Button Component', () => {
5 it('renders with correct text', () => {
6 render(<Button>Click me</Button>)
7 expect(screen.getByText('Click me')).toBeInTheDocument()
8 })
9
10 it('calls onClick when clicked', () => {
11 const handleClick = jest.fn()
12 render(<Button onClick={handleClick}>Click</Button>)
13
14 fireEvent.click(screen.getByRole('button'))
15
16 expect(handleClick).toHaveBeenCalledTimes(1)
17 })
18
19 it('is disabled when disabled prop is true', () => {
20 render(<Button disabled>Click</Button>)
21 expect(screen.getByRole('button')).toBeDisabled()
22 })
23})
API Integration Test Pattern
typescript
1import { NextRequest } from 'next/server'
2import { GET } from './route'
3
4describe('GET /api/markets', () => {
5 it('returns markets successfully', async () => {
6 const request = new NextRequest('http://localhost/api/markets')
7 const response = await GET(request)
8 const data = await response.json()
9
10 expect(response.status).toBe(200)
11 expect(data.success).toBe(true)
12 expect(Array.isArray(data.data)).toBe(true)
13 })
14
15 it('validates query parameters', async () => {
16 const request = new NextRequest('http://localhost/api/markets?limit=invalid')
17 const response = await GET(request)
18
19 expect(response.status).toBe(400)
20 })
21
22 it('handles database errors gracefully', async () => {
23 // Mock database failure
24 const request = new NextRequest('http://localhost/api/markets')
25 // Test error handling
26 })
27})
E2E Test Pattern (Playwright)
typescript
1import { test, expect } from '@playwright/test'
2
3test('user can search and filter markets', async ({ page }) => {
4 // Navigate to markets page
5 await page.goto('/')
6 await page.click('a[href="/markets"]')
7
8 // Verify page loaded
9 await expect(page.locator('h1')).toContainText('Markets')
10
11 // Search for markets
12 await page.fill('input[placeholder="Search markets"]', 'election')
13
14 // Wait for debounce and results
15 await page.waitForTimeout(600)
16
17 // Verify search results displayed
18 const results = page.locator('[data-testid="market-card"]')
19 await expect(results).toHaveCount(5, { timeout: 5000 })
20
21 // Verify results contain search term
22 const firstResult = results.first()
23 await expect(firstResult).toContainText('election', { ignoreCase: true })
24
25 // Filter by status
26 await page.click('button:has-text("Active")')
27
28 // Verify filtered results
29 await expect(results).toHaveCount(3)
30})
31
32test('user can create a new market', async ({ page }) => {
33 // Login first
34 await page.goto('/creator-dashboard')
35
36 // Fill market creation form
37 await page.fill('input[name="name"]', 'Test Market')
38 await page.fill('textarea[name="description"]', 'Test description')
39 await page.fill('input[name="endDate"]', '2025-12-31')
40
41 // Submit form
42 await page.click('button[type="submit"]')
43
44 // Verify success message
45 await expect(page.locator('text=Market created successfully')).toBeVisible()
46
47 // Verify redirect to market page
48 await expect(page).toHaveURL(/\/markets\/test-market/)
49})
Test File Organization
src/
├── components/
│ ├── Button/
│ │ ├── Button.tsx
│ │ ├── Button.test.tsx # Unit tests
│ │ └── Button.stories.tsx # Storybook
│ └── MarketCard/
│ ├── MarketCard.tsx
│ └── MarketCard.test.tsx
├── app/
│ └── api/
│ └── markets/
│ ├── route.ts
│ └── route.test.ts # Integration tests
└── e2e/
├── markets.spec.ts # E2E tests
├── trading.spec.ts
└── auth.spec.ts
Mocking External Services
Supabase Mock
typescript
1jest.mock('@/lib/supabase', () => ({
2 supabase: {
3 from: jest.fn(() => ({
4 select: jest.fn(() => ({
5 eq: jest.fn(() => Promise.resolve({
6 data: [{ id: 1, name: 'Test Market' }],
7 error: null
8 }))
9 }))
10 }))
11 }
12}))
Redis Mock
typescript
1jest.mock('@/lib/redis', () => ({
2 searchMarketsByVector: jest.fn(() => Promise.resolve([
3 { slug: 'test-market', similarity_score: 0.95 }
4 ])),
5 checkRedisHealth: jest.fn(() => Promise.resolve({ connected: true }))
6}))
OpenAI Mock
typescript
1jest.mock('@/lib/openai', () => ({
2 generateEmbedding: jest.fn(() => Promise.resolve(
3 new Array(1536).fill(0.1) // Mock 1536-dim embedding
4 ))
5}))
Test Coverage Verification
Run Coverage Report
bash
1npm run test:coverage
Coverage Thresholds
json
1{
2 "jest": {
3 "coverageThresholds": {
4 "global": {
5 "branches": 80,
6 "functions": 80,
7 "lines": 80,
8 "statements": 80
9 }
10 }
11 }
12}
Common Testing Mistakes to Avoid
FAIL: WRONG: Testing Implementation Details
typescript
1// Don't test internal state
2expect(component.state.count).toBe(5)
PASS: CORRECT: Test User-Visible Behavior
typescript
1// Test what users see
2expect(screen.getByText('Count: 5')).toBeInTheDocument()
FAIL: WRONG: Brittle Selectors
typescript
1// Breaks easily
2await page.click('.css-class-xyz')
PASS: CORRECT: Semantic Selectors
typescript
1// Resilient to changes
2await page.click('button:has-text("Submit")')
3await page.click('[data-testid="submit-button"]')
FAIL: WRONG: No Test Isolation
typescript
1// Tests depend on each other
2test('creates user', () => { /* ... */ })
3test('updates same user', () => { /* depends on previous test */ })
PASS: CORRECT: Independent Tests
typescript
1// Each test sets up its own data
2test('creates user', () => {
3 const user = createTestUser()
4 // Test logic
5})
6
7test('updates user', () => {
8 const user = createTestUser()
9 // Update logic
10})
Continuous Testing
Watch Mode During Development
bash
1npm test -- --watch
2# Tests run automatically on file changes
Pre-Commit Hook
bash
1# Runs before every commit
2npm test && npm run lint
CI/CD Integration
yaml
1# GitHub Actions
2- name: Run Tests
3 run: npm test -- --coverage
4- name: Upload Coverage
5 uses: codecov/codecov-action@v3
Best Practices
- Write Tests First - Always TDD
- One Assert Per Test - Focus on single behavior
- Descriptive Test Names - Explain what's tested
- Arrange-Act-Assert - Clear test structure
- Mock External Dependencies - Isolate unit tests
- Test Edge Cases - Null, undefined, empty, large
- Test Error Paths - Not just happy paths
- Keep Tests Fast - Unit tests < 50ms each
- Clean Up After Tests - No side effects
- Review Coverage Reports - Identify gaps
Success Metrics
- 80%+ code coverage achieved
- All tests passing (green)
- No skipped or disabled tests
- Fast test execution (< 30s for unit tests)
- E2E tests cover critical user flows
- Tests catch bugs before production
Remember: Tests are not optional. They are the safety net that enables confident refactoring, rapid development, and production reliability.