From b489cb31528e78b73cc0497687384e6be102d7d0 Mon Sep 17 00:00:00 2001 From: Stev_Wang <304865932@qq.com> Date: Tue, 3 Nov 2026 16:17:21 +0800 Subject: [PATCH] =?UTF-8?q?test(api):=20=E4=BF=AE=E5=A4=8D=E9=9B=86?= =?UTF-8?q?=E6=88=90=E6=B5=8B=E8=AF=95=E5=AF=B9=E6=A0=B9=E9=83=A8=E9=97=A8?= =?UTF-8?q?=E9=A1=BA=E5=BA=8F=E4=B8=8E=E6=96=87=E4=BB=B6=E5=B9=B6=E8=A1=8C?= =?UTF-8?q?=E7=9A=84=E4=BE=9D=E8=B5=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 背景: - pnpm ci:local 的 test:int 在本地真实 PG 上失败,与业务代码无关,是测试自身不稳定: 1) identity 用例断言部门树第一项根部门必须有子级,但管理员可以新建无子级根部门 并排在最前(本地已有手工部门「111」,sortOrder=0),该断言不成立; 2) identity.int.spec 清理用 account startsWith('it.'),会误删 departments.int.spec 的 it.dep.* 夹具;四个 int 文件默认并行且共用同一个真实 PG,beforeAll 偶发失败。 改动: - identity.int.spec:部门树断言改为「存在带子级的根部门」,不再依赖根部门顺序; - vitest.int.config:fileParallelism: false,集成测试文件串行执行,并注释原因。 影响面: - 仅集成测试的断言与执行方式;业务代码、单测、构建不变; - test:int 改为串行,耗时略增。 回滚方式: - 回滚本提交即恢复原断言与并行执行。 验证证据: - pnpm --filter @lingxi/api test:int 连续 3 次:4 个文件 / 33 项全通过; - pnpm ci:local 第 1–8 步通过(第 10 步 e2e 为 A0 遗留问题,另行处理); - pnpm format:check / lint / typecheck / test 通过。 --- apps/api/src/modules/identity/identity.int.spec.ts | 4 +++- apps/api/vitest.int.config.ts | 3 +++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/apps/api/src/modules/identity/identity.int.spec.ts b/apps/api/src/modules/identity/identity.int.spec.ts index 1b90485..25b9474 100644 --- a/apps/api/src/modules/identity/identity.int.spec.ts +++ b/apps/api/src/modules/identity/identity.int.spec.ts @@ -318,7 +318,9 @@ describe('identity 集成(真实 PG / Redis)', () => { expect(tree.statusCode).toBe(200); const treeBody = tree.json<{ items: { name: string; children: unknown[] }[] }>(); expect(treeBody.items.length).toBeGreaterThanOrEqual(3); - expect(treeBody.items[0]?.children.length).toBeGreaterThan(0); + // 根部门按 sortOrder 排序,管理员可新建无子级的根部门并排在最前; + // 这里只校验树里存在带子级的根部门,不依赖当前第一项(本地手工数据会让测试失真)。 + expect(treeBody.items.some((item) => item.children.length > 0)).toBe(true); const roles = await created.app.inject({ method: 'GET', diff --git a/apps/api/vitest.int.config.ts b/apps/api/vitest.int.config.ts index fd8a853..aa3b085 100644 --- a/apps/api/vitest.int.config.ts +++ b/apps/api/vitest.int.config.ts @@ -6,5 +6,8 @@ export default defineConfig({ include: ['src/**/*.int.spec.ts'], testTimeout: 30_000, hookTimeout: 30_000, + // 集成测试共用本地真实 PG / Redis,且各文件会清理带前缀的夹具(如 it.* 与 it.dep.* 重叠)。 + // 文件级并行会让跨文件的夹具清理 / 数据断言互相干扰,因此串行执行。 + fileParallelism: false, }, });