name: 测试模式 description: 用于Jest和Playwright的测试模式。在编写测试、设置测试夹具或验证RLS强制执行时使用。指导现有的测试约定。
测试模式技能
目的
指导一致和有效的测试。指导现有的测试模式并提供证据模板。
适用场景
- 编写单元或集成测试
- 使用RLS设置测试夹具
- 运行测试套件
- 打包测试证据
关键规则
❌ 禁止
// 直接Prisma调用绕过RLS
const user = await prisma.user.findUnique({ where: { user_id } });
// 共享测试状态导致不稳定测试
let sharedUser: User;
beforeAll(() => { sharedUser = createUser(); });
✅ 正确
// 使用RLS上下文助手
const user = await withSystemContext(prisma, "test", async (client) => {
return client.user.findUnique({ where: { user_id } });
});
// 隔离测试状态
beforeEach(() => {
const testUser = createTestUser();
});
// 唯一标识符
const userId = `user-${crypto.randomUUID()}`;
测试命令
yarn test:unit # 单元测试
yarn test:integration # 集成测试
yarn test:e2e # E2E测试 (Playwright)
yarn ci:validate # 完整验证
测试目录结构
__tests__/
├── unit/ # 快速、隔离的测试
├── integration/ # API和数据库测试
├── e2e/ # 端到端测试
└── setup.ts # 全局设置
证据模板
**测试执行证据**
**测试套件**: [unit/integration/e2e]
**文件变更**: [列出文件]
**测试结果:**
- 总测试数: [X]
- 通过: [X]
- 失败: [0]
**运行命令:**
```bash
yarn test:unit --coverage
## 参考
- **Jest配置**: `jest.config.js`
- **RLS上下文**: `lib/rls-context.ts`