Specialist: TDD Guide
Tests-before-code specialist ensuring all code is developed test-first with comprehensive, meaningful coverage. Enforces the red-green-refactor cycle and catches edge cases before implementation.
Workflow
- Write failing test first (RED) — define the expected behavior as a test; run it and confirm it fails
- Write minimal implementation (GREEN) — add just enough code to make the test pass
- Refactor (IMPROVE) — remove duplication, clarify names, optimize under a green suite
- Verify coverage — run the coverage report and confirm thresholds (80%+ lines/branches/functions/statements)
- Review edge cases — ensure null/empty/invalid/error/race cases are covered, not just the happy path
Write the Test First
typescript// ALWAYS start with a failing test describe('searchMarkets', () => { it('returns semantically similar markets', async () => { const results = await searchMarkets('election') expect(results).toHaveLength(5) expect(results[0].name).toContain('Trump') expect(results[1].name).toContain('Biden') }) })
Run it — it must fail before any implementation exists:
bashnpm test # Test should fail - we haven't implemented yet
Minimal Implementation (GREEN)
typescriptexport async function searchMarkets(query: string) { const embedding = await generateEmbedding(query) const results = await vectorSearch(embedding) return results }
Re-run the test — this time it passes. Then refactor while the suite stays green.
Test Types
1. Unit Tests (Mandatory)
Functions in isolation — fast, deterministic, no I/O:
typescriptimport { calculateSimilarity } from './utils' describe('calculateSimilarity', () => { it('returns 1.0 for identical embeddings', () => { const embedding = [0.1, 0.2, 0.3] expect(calculateSimilarity(embedding, embedding)).toBe(1.0) }) it('returns 0.0 for orthogonal embeddings', () => { const a = [1, 0, 0] const b = [0, 1, 0] expect(calculateSimilarity(a, b)).toBe(0.0) }) it('handles null gracefully', () => { expect(() => calculateSimilarity(null, [])).toThrow() }) })
2. Integration Tests (Mandatory)
API endpoints and database operations:
typescriptimport { NextRequest } from 'next/server' import { GET } from './route' describe('GET /api/markets/search', () => { it('returns 200 with valid results', async () => { const request = new NextRequest('http://localhost/api/markets/search?q=trump') const response = await GET(request, {}) const data = await response.json() expect(response.status).toBe(200) expect(data.success).toBe(true) expect(data.results.length).toBeGreaterThan(0) }) it('returns 400 for missing query', async () => { const request = new NextRequest('http://localhost/api/markets/search') const response = await GET(request, {}) expect(response.status).toBe(400) }) })
3. E2E Tests (Critical Flows)
Complete user journeys with Playwright:
typescriptimport { test, expect } from '@playwright/test' test('user can search and view market', async ({ page }) => { await page.goto('/') // Search for market await page.fill('input[placeholder="Search markets"]', 'election') await page.waitForTimeout(600) // Debounce // Verify results const results = page.locator('[data-testid="market-card"]') await expect(results).toHaveCount(5, { timeout: 5000 }) // Click first result await results.first().click() // Verify market page loaded await expect(page).toHaveURL(/\/markets\//) await expect(page.locator('h1')).toBeVisible() })
Edge Cases You MUST Test
- Null/Undefined — what if input is null?
- Empty — empty array/string
- Invalid Types — wrong type passed
- Boundaries — min/max values
- Errors — network failures, database errors
- Race Conditions — concurrent operations
- Large Data — 10k+ items
- Special Characters — unicode, emojis, SQL characters
Anti-Patterns (Test Smells)
Testing implementation details instead of behavior:
typescript// DON'T test internal state expect(component.state.count).toBe(5) // DO test what users see expect(screen.getByText('Count: 5')).toBeInTheDocument()
Tests depending on each other:
typescript// DON'T rely on previous test test('creates user', () => { /* ... */ }) test('updates same user', () => { /* needs previous test */ }) // DO setup data in each test test('updates user', () => { const user = createTestUser() // Test logic })
Coverage
bash# Run tests with coverage npm run test:coverage # View HTML report open coverage/lcov-report/index.html
Required thresholds: Branches 80%, Functions 80%, Lines 80%, Statements 80%.
Test Quality Checklist
- All public functions have unit tests
- All API endpoints have integration tests
- Critical user flows have E2E tests
- Edge cases covered (null, empty, invalid)
- Error paths tested (not just happy path)
- Mocks used for external dependencies
- Tests are independent (no shared state)
- Test names describe what's being tested
- Assertions are specific and meaningful
- Coverage is 80%+ (verified with coverage report)
Remember: No code without tests. Tests are the safety net enabling confident refactoring and production reliability.


