This skill is intended to ensure that all code development follows TDD principles and has comprehensive test coverage.
When to Enable
- Writing new features or new 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 the tests pass.
2. Coverage Requirements
- Minimum 80% coverage (unit tests + integration tests + E2E tests)
- Cover all edge cases
- Test error scenarios
- Verify boundary conditions
3. Test Types
Unit Tests
- Individual functions and utilities
- Component logic
- Pure functions
- Helpers and utilities
Integration Tests
- API endpoints
- Database operations
- Interactions between services
- External API calls
E2E Tests (Playwright)
- Critical user flows
- Complete workflows
- Browser automation
- UI interactions
TDD Workflow Steps
Step 1: Write User Journeys
作为 [角色],我想 [动作],以便 [获益]
示例:
作为一名用户,我想进行语义化的市场搜索,
以便即使没有精确的关键词也能找到相关的市场。
Step 2: Generate Test Cases
Create comprehensive test cases for each user journey:
describe('Semantic Search', () => {
it('returns relevant markets for query', async () => {
// 测试实现
})
it('handles empty query gracefully', async () => {
// 测试边缘情况
})
it('falls back to substring search when Redis unavailable', async () => {
// 测试回退行为
})
it('sorts results by similarity score', async () => {
// 测试排序逻辑
})
})
Step 3: Run Tests (Expected to Fail)
npm test
# 测试应该失败 - 因为我们尚未实现功能
Step 4: Implement Code
Write the minimum amount of code to make the tests pass:
// 由测试引导的实现
export async function searchMarkets(query: string) {
// 此处为实现逻辑
}
Step 5: Run Tests Again
npm test
# 测试现在应该通过
Step 6: Refactor
Improve code quality while keeping tests passing:
- Eliminate duplication
- Improve naming
- Optimize performance
- Enhance readability
Step 7: Verify Coverage
npm run test:coverage
# 验证是否达到了 80% 以上的覆盖率
Testing Patterns
Unit Test Pattern (Jest/Vitest)
import { render, screen, fireEvent } from '@testing-library/react'
import { Button } from './Button'
describe('Button Component', () => {
it('renders with correct text', () => {
render(<Button>Click me</Button>)
expect(screen.getByText('Click me')).toBeInTheDocument()
})
it('calls onClick when clicked', () => {
const handleClick = jest.fn()
render(<Button onClick={handleClick}>Click</Button>)
fireEvent.click(screen.getByRole('button'))
expect(handleClick).toHaveBeenCalledTimes(1)
})
it('is disabled when disabled prop is true', () => {
render(<Button disabled>Click</Button>)
expect(screen.getByRole('button')).toBeDisabled()
})
})
API Integration Test Pattern
import { NextRequest } from 'next/server'
import { GET } from './route'
describe('GET /api/markets', () => {
it('returns markets successfully', async () => {
const request = new NextRequest('http://localhost/api/markets')
const response = await GET(request)
const data = await response.json()
expect(response.status).toBe(200)
expect(data.success).toBe(true)
expect(Array.isArray(data.data)).toBe(true)
})
it('validates query parameters', async () => {
const request = new NextRequest('http://localhost/api/markets?limit=invalid')
const response = await GET(request)
expect(response.status).toBe(400)
})
it('handles database errors gracefully', async () => {
// 模拟数据库故障
const request = new NextRequest('http://localhost/api/markets')
// 测试错误处理
})
})
E2E Test Pattern (Playwright)
import { test, expect } from '@playwright/test'
test('user can search and filter markets', async ({ page }) => {
// 导航到市场页面
await page.goto('/')
await page.click('a[href="/markets"]')
// 验证页面已加载
await expect(page.locator('h1')).toContainText('Markets')
// 搜索市场
await page.fill('input[placeholder="Search markets"]', 'election')
// 等待防抖处理和结果返回
await page.waitForTimeout(600)
// 验证搜索结果已显示
const results = page.locator('[data-testid="market-card"]')
await expect(results).toHaveCount(5, { timeout: 5000 })
// 验证结果包含搜索词
const firstResult = results.first()
await expect(firstResult).toContainText('election', { ignoreCase: true })
// 按状态过滤
await page.click('button:has-text("Active")')
// 验证过滤后的结果
await expect(results).toHaveCount(3)
})
test('user can create a new market', async ({ page }) => {
// 先登录
await page.goto('/creator-dashboard')
// 填写市场创建表单
await page.fill('input[name="name"]', 'Test Market')
await page.fill('textarea[name="description"]', 'Test description')
await page.fill('input[name="endDate"]', '2025-12-31')
// 提交表单
await page.click('button[type="submit"]')
// 验证成功消息
await expect(page.locator('text=Market created successfully')).toBeVisible()
// 验证重定向到市场详情页
await expect(page).toHaveURL(/\/markets\/test-market/)
})
Test File Organization
src/
├── components/
│ ├── Button/
│ │ ├── Button.tsx
│ │ ├── Button.test.tsx # 单元测试
│ │ └── Button.stories.tsx # Storybook
│ └── MarketCard/
│ ├── MarketCard.tsx
│ └── MarketCard.test.tsx
├── app/
│ └── api/
│ └── markets/
│ ├── route.ts
│ └── route.test.ts # 集成测试
└── e2e/
├── markets.spec.ts # E2E 测试
├── trading.spec.ts
└── auth.spec.ts
Mocking External Services
Supabase Mocking
jest.mock('@/lib/supabase', () => ({
supabase: {
from: jest.fn(() => ({
select: jest.fn(() => ({
eq: jest.fn(() => Promise.resolve({
data: [{ id: 1, name: 'Test Market' }],
error: null
}))
}))
}))
}
}))
Redis Mocking
jest.mock('@/lib/redis', () => ({
searchMarketsByVector: jest.fn(() => Promise.resolve([
{ slug: 'test-market', similarity_score: 0.95 }
])),
checkRedisHealth: jest.fn(() => Promise.resolve({ connected: true }))
}))
OpenAI Mocking
jest.mock('@/lib/openai', () => ({
generateEmbedding: jest.fn(() => Promise.resolve(
new Array(1536).fill(0.1) // 模拟 1536 维向量嵌入
))
}))
Test Coverage Verification
Run Coverage Report
npm run test:coverage
Coverage Threshold
{
"jest": {
"coverageThresholds": {
"global": {
"branches": 80,
"functions": 80,
"lines": 80,
"statements": 80
}
}
}
}
Common Testing Mistakes to Avoid
❌ Wrong Practice: Testing Implementation Details
// 不要测试内部状态
expect(component.state.count).toBe(5)
✅ Correct Practice: Test User-Visible Behavior
// 测试用户看到的内容
expect(screen.getByText('Count: 5')).toBeInTheDocument()
❌ Wrong Practice: Using Fragile Selectors
// 极易因样式更改而失败
await page.click('.css-class-xyz')
✅ Correct Practice: Use Semantic Selectors
// 对更改具有弹性
await page.click('button:has-text("Submit")')
await page.click('[data-testid="submit-button"]')
❌ Wrong Practice: Lack of Isolation Between Tests
// 测试相互依赖
test('creates user', () => { /* ... */ })
test('updates same user', () => { /* 依赖于前一个测试的结果 */ })
✅ Correct Practice: Independent Tests
// 每个测试都设置自己的数据
test('creates user', () => {
const user = createTestUser()
// 测试逻辑
})
test('updates user', () => {
const user = createTestUser()
// 更新逻辑
})
Continuous Testing
Watch Mode During Development
npm test -- --watch
# 文件更改时自动运行测试
Pre-commit hooks
# 在每次提交前运行
npm test && npm run lint
CI/CD integration
# GitHub Actions
- name: Run Tests
run: npm test -- --coverage
- name: Upload Coverage
uses: codecov/codecov-action@v3
Best practices
- Write tests first - Always follow TDD
- One assertion per test - Focus on a single behavior
- Descriptive test names - Explain what is being tested
- Arrange-Act-Assert - Clear test structure
- Mock external dependencies - Isolate unit tests
- Test edge cases - null, undefined, empty values, oversized values
- Test error paths - Not just "Happy paths"
- Keep tests fast - Each unit test < 50ms
- Clean up after tests - Avoid side effects
- Review coverage reports - Identify missing gaps
Success metrics
- Achieve over 80% code coverage
- All tests pass (shown as green)
- No skipped or disabled tests
- Fast test execution (unit tests < 30s)
- E2E tests cover key user flows
- Tests can catch bugs before they reach production
Remember: Tests are not optional. They are the safety net that gives you the confidence to refactor, develop quickly, and ensure production reliability.