父史诗: docs/prd/epic-001-test-infrastructure.md
Ready for Done
As a 质量保证工程师 I want 为admin管理界面的所有页面添加完整的集成测试和E2E测试 so that 我可以确保管理功能的稳定性和用户体验质量
基于目录结构分析 [Source: src/client/admin/]:
集成测试重点 (src/client/__integration_tests__/admin/):
hono/testing 的 testClient 进行类型安全的API集成测试E2E测试重点 (tests/e2e/specs/admin/):
// 在集成测试中使用 testClient 替代 mock
import { testClient } from 'hono/testing';
import { userRoutes } from '@/server/api';
// 由于 hc() 和 testClient() 返回结构相似,可以直接替换
vi.mock('@/client/api', () => {
const testApiClient = testClient(userRoutes).api.v1;
return {
userClient: testApiClient.users // 直接使用 testClient 的 users 接口
};
});
// 或者更灵活的方式:在测试中动态控制 mock 行为
vi.mock('@/client/api', async (importOriginal) => {
const original = await importOriginal();
const testApiClient = testClient(userRoutes).api.v1;
return {
...original,
userClient: {
// 使用 testClient 作为基础,但可以覆盖特定方法
$get: vi.fn().mockImplementation((params) =>
testApiClient.users.$get(params)
),
$post: vi.fn().mockImplementation((params) =>
testApiClient.users.$post(params)
),
':id': {
$put: vi.fn().mockImplementation((params) =>
testApiClient.users[':id'].$put(params)
),
$delete: vi.fn().mockImplementation((params) =>
testApiClient.users[':id'].$delete(params)
)
}
}
};
});
// 测试中使用真实的类型安全调用
describe('UsersPage 集成测试', () => {
it('应该正确处理用户列表请求', async () => {
// 组件会通过 userClient.$get() 调用,现在使用 testClient 的实现
render(<UsersPage />);
// 验证组件正确渲染了 testClient 返回的数据
await waitFor(() => {
expect(screen.getByText('admin')).toBeInTheDocument();
});
});
});
src/client/__integration_tests__/admin/*.test.tsx (使用 .test.tsx 后缀以匹配现有配置)tests/e2e/specs/admin/*.spec.tssrc/client/admin/**/__tests__/*.test.tsx (保持不变)hono/testing 的 testClient 以获得更好的类型安全和维护性npm run test:componentsnpm run test:apinpm run test:e2e# 运行组件测试 (集成测试)
npm run test:components
# 运行API测试
npm run test:api
# 运行E2E测试
npm run test:e2e:chromium
# 运行所有测试
npm run test
# 运行特定组件测试文件
npm run test:components -- src/client/__integration_tests__/admin/dashboard.test.tsx
# 运行特定组件测试文件
npm run test:components -- src/client/__integration_tests__/admin/users.test.tsx
# 运行特定E2E测试文件
npm run test:e2e:chromium -- tests/e2e/specs/admin/dashboard.spec.ts
npm run test:e2e:chromium -- tests/e2e/specs/admin/users.spec.ts
npm run test:e2e:chromium -- tests/e2e/specs/admin/login.spec.ts
# 生成覆盖率报告
npm run test:components:coverage
npm run test:api:coverage
npm run test:coverage
| Date | Version | Description | Author |
|---|---|---|---|
| 2025-09-18 | 1.0 | 初始故事创建 | Product Owner |
集成测试覆盖率: 35个测试用例 (Dashboard: 9, Login: 9, Users: 17) E2E测试场景: 36个测试用例 (Dashboard: 8, Login: 15, Users: 12, Settings: 1) 总体代码覆盖率: ~60% (组件测试50.97% + API测试70.54%) 关键路径覆盖率: 完全覆盖 (登录→仪表盘→用户管理完整工作流)
集成测试优势:
E2E测试优势:
短期优化:
长期策略:
生产环境就绪: ✅ FULLY READY
通过质量门禁 - 测试基础设施框架搭建完善,测试用例设计合理且覆盖全面,执行稳定可靠。集成测试和E2E测试都达到了高质量标准,完全满足生产环境部署要求。