父史诗: docs/prd/epic-001-test-infrastructure.md
Ready for Development
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
# 运行所有测试
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 -- tests/e2e/specs/admin/dashboard.spec.ts
npm run test:e2e -- tests/e2e/specs/admin/users.spec.ts
npm run test:e2e -- 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 |
集成测试覆盖率: 28个测试用例 (Dashboard: 7, Login: 9, Users: 12) E2E测试场景: 33个测试用例 (Dashboard: 8, Login: 15, Users: 9, Settings: 1) 总体代码覆盖率: 6.71% (未达到目标60%阈值) 关键路径覆盖率: 部分覆盖 (需要验证完整工作流)
集成测试优势:
需要改进的方面:
hono/testing 的 testClient 进行类型安全的API集成测试E2E测试现状:
当前风险:
改进建议:
hono/testing 的 testClient 集成测试模式高优先级:
中优先级:
低优先级:
生产环境就绪: ⚠️ CONDITIONAL
监控建议:
有条件通过质量门禁 - 测试基础设施框架搭建完成,核心测试用例设计合理,但测试覆盖率严重不足。建议: