| 版本 | 日期 | 描述 | 作者 |
|---|---|---|---|
| 2.7 | 2025-11-09 | 更新为monorepo测试架构,清理重复测试文件 | James |
| 2.6 | 2025-10-15 | 完成遗留测试文件迁移到统一的tests目录结构 | Winston |
| 2.5 | 2025-10-14 | 更新测试文件位置到统一的tests目录结构 | Claude |
| 2.4 | 2025-09-20 | 更新测试策略与主架构文档版本一致 | Winston |
本文档定义了D8D Starter项目的完整测试策略,基于monorepo架构和现有的测试基础设施。测试策略遵循测试金字塔模型,确保代码质量、功能稳定性和系统可靠性。
项目已重构为monorepo结构,测试架构相应调整为:
packages/server/tests/unit/**/*.test.ts (服务器单元测试)web/tests/unit/**/*.test.{ts,tsx} (Web组件单元测试)packages/server/tests/integration/**/*.test.ts (服务器集成测试)web/tests/integration/**/*.test.{ts,tsx} (Web集成测试)web/tests/e2e/**/*.test.{ts,tsx}// vitest.config.ts - 开发环境配置
export default defineConfig({
test: {
projects: [
// Node.js 环境项目 - 后端测试
{
test: {
include: [
'tests/unit/server/**/*.test.{ts,js}',
'tests/integration/server/**/*.test.{ts,js}'
],
// ... 其他配置
}
},
// Happy DOM 环境项目 - 前端组件测试
{
test: {
include: [
'tests/unit/client/**/*.test.{ts,js,tsx,jsx}',
'tests/integration/client/**/*.test.{ts,js,tsx,jsx}'
],
// ... 其他配置
}
}
]
}
});
# GitHub Actions 测试配置 (Monorepo架构)
name: Test Pipeline
jobs:
server-tests:
runs-on: ubuntu-latest
services:
postgres:
image: postgres:17
env:
POSTGRES_PASSWORD: test_password
POSTGRES_DB: test_d8dai
steps:
- run: cd packages/server && pnpm test:unit
- run: cd packages/server && pnpm test:integration
web-integration-tests:
runs-on: ubuntu-latest
services:
postgres:
image: postgres:17
env:
POSTGRES_PASSWORD: test_password
POSTGRES_DB: test_d8dai
steps:
- run: cd web && pnpm test:integration
web-component-tests:
runs-on: ubuntu-latest
steps:
- run: cd web && pnpm test:components
web-e2e-tests:
runs-on: ubuntu-latest
steps:
- run: cd web && pnpm test:e2e:chromium
| 测试类型 | 最低要求 | 目标要求 | 关键模块要求 |
|---|---|---|---|
| 单元测试 | 70% | 80% | 90% |
| 集成测试 | 50% | 60% | 70% |
| E2E测试 | 关键流程100% | 主要流程80% | - |
// 测试数据工厂模式
export function createTestUser(overrides = {}): User {
return {
id: 1,
username: 'testuser',
email: 'test@example.com',
createdAt: new Date(),
...overrides
};
}
// 使用示例
const adminUser = createTestUser({ role: 'admin' });
const inactiveUser = createTestUser({ active: false });
# 运行所有测试
pnpm test
# 运行单元测试
pnpm test:unit
# 运行集成测试
pnpm test:integration
# 生成覆盖率报告
pnpm test:coverage
# 运行所有测试
pnpm test
# 运行单元测试
pnpm test:unit
# 运行集成测试
pnpm test:integration
# 运行组件测试
pnpm test:components
# 运行E2E测试
pnpm test:e2e:chromium
# 生成覆盖率报告
pnpm test:coverage
// 良好的测试示例
describe('UserService', () => {
describe('createUser()', () => {
it('应该创建新用户并返回用户对象', async () => {
// Arrange
const userData = { username: 'testuser', email: 'test@example.com' };
// Act
const result = await userService.createUser(userData);
// Assert
expect(result).toHaveProperty('id');
expect(result.username).toBe('testuser');
expect(result.email).toBe('test@example.com');
});
it('应该拒绝重复的用户名', async () => {
// Arrange
const existingUser = await createTestUser({ username: 'existing' });
// Act & Assert
await expect(
userService.createUser({ username: 'existing', email: 'new@example.com' })
).rejects.toThrow('用户名已存在');
});
});
});
[module].test.ts 或 [module].integration.test.ts| 日期 | 版本 | 描述 |
|---|---|---|
| 2025-11-09 | 2.7 | 更新为monorepo测试架构,清理重复测试文件 |
| 2025-10-15 | 2.6 | 完成遗留测试文件迁移到统一的tests目录结构 |
| 2025-10-14 | 2.5 | 重构测试文件结构,统一到tests目录 |
| 2025-09-20 | 2.4 | 更新版本与主架构文档一致 |
| 2025-09-19 | 1.0 | 初始版本,基于现有测试基础设施 |
文档状态: 正式版 下次评审: 2025-12-19