affected-tests.spec.js
5.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
const fs = require("node:fs");
const path = require("node:path");
const fixtureRoot = path.resolve(__dirname, "__fixtures__/vue2-app");
const builderPath = path.resolve(__dirname, "build-knowledge-graph.js");
const affectedPath = path.resolve(__dirname, "affected-tests.js");
function loadModules() {
expect(fs.existsSync(builderPath)).toBe(true);
expect(fs.existsSync(affectedPath)).toBe(true);
return {
...require(builderPath),
...require(affectedPath),
};
}
describe("affected 测试规划", () => {
it("业务域文件应选择图谱覆盖测试", () => {
const { buildKnowledgeGraph, planAffectedTests } = loadModules();
const graph = buildKnowledgeGraph({ rootDir: fixtureRoot });
const result = planAffectedTests({
rootDir: fixtureRoot,
graph,
changedFiles: ["src/api/goods.js"],
});
expect(result.mode).toBe("targeted");
expect(result.domains).toContain("903");
expect(result.testFiles).toContain("tests/goods-list.spec.js");
});
it.each([
["package.json", "global-infrastructure"],
["src/router/index.js", "global-infrastructure"],
["src/components/SharedCard.vue", "global-infrastructure"],
])("共享基础设施 %s 应升级全量", (changedFile, reason) => {
const { buildKnowledgeGraph, planAffectedTests } = loadModules();
const graph = buildKnowledgeGraph({ rootDir: fixtureRoot });
const result = planAffectedTests({
rootDir: fixtureRoot,
graph,
changedFiles: [changedFile],
});
expect(result.mode).toBe("full");
expect(result.reasons).toContain(reason);
});
it("纯知识文档改动应只运行知识检查", () => {
const { buildKnowledgeGraph, planAffectedTests } = loadModules();
const graph = buildKnowledgeGraph({ rootDir: fixtureRoot });
const result = planAffectedTests({
rootDir: fixtureRoot,
graph,
changedFiles: [".ai/docs/knowledge/903-product.md"],
});
expect(result.mode).toBe("knowledge-only");
expect(result.commands).toEqual([
"npm run knowledge:test",
"npm run knowledge:check",
]);
});
it("删除运行时代码应升级全量", () => {
const { buildKnowledgeGraph, planAffectedTests } = loadModules();
const graph = buildKnowledgeGraph({ rootDir: fixtureRoot });
const result = planAffectedTests({
rootDir: fixtureRoot,
graph,
changedFiles: ["src/views/goods/removed.vue"],
deletedFiles: ["src/views/goods/removed.vue"],
});
expect(result.mode).toBe("full");
expect(result.reasons).toContain("deleted-runtime-file");
});
it("jest 标准依赖证据应补充新文件的候选测试", () => {
const { buildKnowledgeGraph, planAffectedTests } = loadModules();
const graph = buildKnowledgeGraph({ rootDir: fixtureRoot });
const result = planAffectedTests({
rootDir: fixtureRoot,
graph,
changedFiles: ["src/views/goods/new.vue"],
standardTestFiles: ["tests/goods-list.spec.js"],
});
expect(result.mode).toBe("targeted");
expect(result.domains).toContain("903");
expect(result.testFiles).toEqual(["tests/goods-list.spec.js"]);
});
it("overlay 发现变更文件有 unresolved 时应升级全量", () => {
const { buildKnowledgeGraph, planAffectedTests } = loadModules();
const graph = buildKnowledgeGraph({ rootDir: fixtureRoot });
const result = planAffectedTests({
rootDir: fixtureRoot,
graph,
changedFiles: ["src/views/goods/list.vue"],
});
expect(result.mode).toBe("full");
expect(result.reasons).toContain("unresolved-change");
});
it("运行时代码重命名应升级全量", () => {
const { buildKnowledgeGraph, planAffectedTests } = loadModules();
const graph = buildKnowledgeGraph({ rootDir: fixtureRoot });
const result = planAffectedTests({
rootDir: fixtureRoot,
graph,
changedFiles: ["src/views/goods/detail.vue", "src/views/goods/detail-new.vue"],
renamedFiles: ["src/views/goods/detail.vue", "src/views/goods/detail-new.vue"],
});
expect(result.mode).toBe("full");
expect(result.reasons).toContain("renamed-runtime-file");
});
it("三个业务域受影响时应升级全量", () => {
const { buildKnowledgeGraph, planAffectedTests } = loadModules();
const graph = buildKnowledgeGraph({ rootDir: fixtureRoot });
graph.nodes.push(
{ id: "module:src/domain904.js", type: "module", name: "domain904", path: "src/domain904.js", domainIds: ["904"] },
{ id: "module:src/domain912.js", type: "module", name: "domain912", path: "src/domain912.js", domainIds: ["912"] },
);
const result = planAffectedTests({
rootDir: fixtureRoot,
graph,
changedFiles: ["src/api/goods.js", "src/domain904.js", "src/domain912.js"],
});
expect(result.mode).toBe("full");
expect(result.reasons).toContain("cross-domain-change");
});
it("只修改测试文件时应直接运行该测试", () => {
const { buildKnowledgeGraph, planAffectedTests } = loadModules();
const graph = buildKnowledgeGraph({ rootDir: fixtureRoot });
const result = planAffectedTests({
rootDir: fixtureRoot,
graph,
changedFiles: ["tests/goods-list.spec.js"],
});
expect(result.mode).toBe("targeted");
expect(result.reasons).toEqual(["changed-tests-only"]);
expect(result.testFiles).toEqual(["tests/goods-list.spec.js"]);
});
});