-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathcoverageProcessor.test.ts
More file actions
310 lines (254 loc) · 10.1 KB
/
Copy pathcoverageProcessor.test.ts
File metadata and controls
310 lines (254 loc) · 10.1 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
import test from "ava";
import {
extractLineCoverage,
filterScriptUrl,
takeAndProcessSnapshot,
V8CoverageData,
} from "./coverageProcessor";
import fs from "fs";
import path from "path";
import os from "os";
// --- filterScriptUrl ---
test("filterScriptUrl: accepts user source files", (t) => {
t.is(filterScriptUrl("file:///project/src/app.js", "/project"), "/project/src/app.js");
});
test("filterScriptUrl: rejects non-file URLs", (t) => {
t.is(filterScriptUrl("node:internal/modules", "/project"), null);
t.is(filterScriptUrl("", "/project"), null);
});
test("filterScriptUrl: rejects node_modules", (t) => {
t.is(filterScriptUrl("file:///project/node_modules/express/index.js", "/project"), null);
});
test("filterScriptUrl: rejects files outside sourceRoot", (t) => {
t.is(filterScriptUrl("file:///other/project/app.js", "/project"), null);
});
test("filterScriptUrl: rejects prefix collisions (e.g., /app vs /application)", (t) => {
t.is(filterScriptUrl("file:///application/src/app.js", "/app"), null);
});
test("filterScriptUrl: accepts files when sourceRoot is / (Docker root)", (t) => {
t.is(filterScriptUrl("file:///app/src/server.js", "/"), "/app/src/server.js");
});
test("filterScriptUrl: handles URL-encoded paths (spaces)", (t) => {
t.is(
filterScriptUrl("file:///my%20project/src/app.js", "/my project"),
"/my project/src/app.js"
);
});
// --- takeAndProcessSnapshot ---
test("takeAndProcessSnapshot: skips coverage files without user scripts", async (t) => {
const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "coverage-test-"));
const coverageDir = path.join(tmpDir, "coverage");
fs.mkdirSync(coverageDir);
try {
// Create coverage for node_modules only
const v8Data: V8CoverageData = {
result: [
{
url: "file:///some/path/node_modules/pkg/index.js",
functions: [
{
functionName: "",
ranges: [{ startOffset: 0, endOffset: 10, count: 1 }],
isBlockCoverage: false,
},
],
},
],
};
const coverageFile = path.join(coverageDir, "coverage-1.json");
fs.writeFileSync(coverageFile, JSON.stringify(v8Data));
const result = await takeAndProcessSnapshot(coverageDir, tmpDir, false);
// Should skip files without user scripts
t.deepEqual(result, {});
// File should be cleaned up
const remainingFiles = fs.readdirSync(coverageDir);
t.is(remainingFiles.length, 0);
} finally {
fs.rmSync(tmpDir, { recursive: true, force: true });
}
});
// --- Babel plugin selection and parsing tests ---
test("Babel plugin selection: TypeScript files get typescript and decorators-legacy plugins", (t) => {
const scriptPath = "/project/src/service.ts";
const isTypeScript = /\.(ts|tsx|mts|cts)$/.test(scriptPath);
const isTSX = /\.tsx$/.test(scriptPath);
const babelPlugins: string[] = isTypeScript
? ["typescript", "decorators-legacy", ...(isTSX ? ["jsx"] : [])]
: ["decorators-legacy"];
t.deepEqual(babelPlugins, ["typescript", "decorators-legacy"]);
});
test("Babel plugin selection: TSX files get typescript, decorators-legacy, and jsx plugins", (t) => {
const scriptPath = "/project/src/Component.tsx";
const isTypeScript = /\.(ts|tsx|mts|cts)$/.test(scriptPath);
const isTSX = /\.tsx$/.test(scriptPath);
const babelPlugins: string[] = isTypeScript
? ["typescript", "decorators-legacy", ...(isTSX ? ["jsx"] : [])]
: ["decorators-legacy"];
t.deepEqual(babelPlugins, ["typescript", "decorators-legacy", "jsx"]);
});
test("Babel plugin selection: .mts files get typescript and decorators-legacy plugins", (t) => {
const scriptPath = "/project/src/module.mts";
const isTypeScript = /\.(ts|tsx|mts|cts)$/.test(scriptPath);
const isTSX = /\.tsx$/.test(scriptPath);
const babelPlugins: string[] = isTypeScript
? ["typescript", "decorators-legacy", ...(isTSX ? ["jsx"] : [])]
: ["decorators-legacy"];
t.deepEqual(babelPlugins, ["typescript", "decorators-legacy"]);
});
test("Babel plugin selection: .cts files get typescript and decorators-legacy plugins", (t) => {
const scriptPath = "/project/src/commonjs.cts";
const isTypeScript = /\.(ts|tsx|mts|cts)$/.test(scriptPath);
const isTSX = /\.tsx$/.test(scriptPath);
const babelPlugins: string[] = isTypeScript
? ["typescript", "decorators-legacy", ...(isTSX ? ["jsx"] : [])]
: ["decorators-legacy"];
t.deepEqual(babelPlugins, ["typescript", "decorators-legacy"]);
});
test("Babel plugin selection: JavaScript files get only decorators-legacy plugin", (t) => {
const scriptPath = "/project/src/service.js";
const isTypeScript = /\.(ts|tsx|mts|cts)$/.test(scriptPath);
const isTSX = /\.tsx$/.test(scriptPath);
const babelPlugins: string[] = isTypeScript
? ["typescript", "decorators-legacy", ...(isTSX ? ["jsx"] : [])]
: ["decorators-legacy"];
t.deepEqual(babelPlugins, ["decorators-legacy"]);
});
test("Babel plugin selection: .mjs files get only decorators-legacy plugin", (t) => {
const scriptPath = "/project/src/module.mjs";
const isTypeScript = /\.(ts|tsx|mts|cts)$/.test(scriptPath);
const isTSX = /\.tsx$/.test(scriptPath);
const babelPlugins: string[] = isTypeScript
? ["typescript", "decorators-legacy", ...(isTSX ? ["jsx"] : [])]
: ["decorators-legacy"];
t.deepEqual(babelPlugins, ["decorators-legacy"]);
});
test("Babel plugin selection: .cjs files get only decorators-legacy plugin", (t) => {
const scriptPath = "/project/src/commonjs.cjs";
const isTypeScript = /\.(ts|tsx|mts|cts)$/.test(scriptPath);
const isTSX = /\.tsx$/.test(scriptPath);
const babelPlugins: string[] = isTypeScript
? ["typescript", "decorators-legacy", ...(isTSX ? ["jsx"] : [])]
: ["decorators-legacy"];
t.deepEqual(babelPlugins, ["decorators-legacy"]);
});
// --- Babel parser integration tests ---
test("Babel parser: can parse TypeScript with parameter decorators (decorators-legacy)", (t) => {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const babelParser = require("@babel/parser");
const tsCode = `
class Service {
constructor(@inject('repo') private repo: any) {}
}
`;
// Should parse without error using decorators-legacy
const ast = babelParser.parse(tsCode, {
sourceType: "module",
plugins: ["typescript", "decorators-legacy"],
});
t.truthy(ast);
t.is(ast.type, "File");
});
test("Babel parser: can parse TSX with JSX syntax", (t) => {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const babelParser = require("@babel/parser");
const tsxCode = `
const Component = () => <div>Hello</div>;
export { Component };
`;
// Should parse without error using typescript + jsx + decorators-legacy
const ast = babelParser.parse(tsxCode, {
sourceType: "module",
plugins: ["typescript", "decorators-legacy", "jsx"],
});
t.truthy(ast);
t.is(ast.type, "File");
});
test("Babel parser: can parse JavaScript with class decorators", (t) => {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const babelParser = require("@babel/parser");
const jsCode = `
@decorator
class Example {}
`;
// Should parse without error using decorators-legacy
const ast = babelParser.parse(jsCode, {
sourceType: "module",
plugins: ["decorators-legacy"],
});
t.truthy(ast);
t.is(ast.type, "File");
});
// --- extractLineCoverage ---
test("extractLineCoverage: expands multi-line statements to all lines", (t) => {
// res.json({ ... }) spanning lines 14-20, covered
const statementMap = {
"0": { start: { line: 14, column: 2 }, end: { line: 20, column: 4 } },
};
const statementCounts = { "0": 1 };
const lines = extractLineCoverage(statementMap, statementCounts);
t.is(lines["14"], 1);
t.is(lines["15"], 1);
t.is(lines["16"], 1);
t.is(lines["17"], 1);
t.is(lines["18"], 1);
t.is(lines["19"], 1);
t.is(lines["20"], 1);
});
test("extractLineCoverage: single-line statement works", (t) => {
const statementMap = {
"0": { start: { line: 5, column: 0 }, end: { line: 5, column: 30 } },
};
const statementCounts = { "0": 3 };
const lines = extractLineCoverage(statementMap, statementCounts);
t.is(lines["5"], 3);
t.false("4" in lines);
t.false("6" in lines);
});
test("extractLineCoverage: inner uncovered statement overrides outer covered statement", (t) => {
// Simulates: try { ... } catch (error) { res.status(500).json({...}) }
// Outer try-catch block (lines 25-40) is covered (count=1)
// Inner catch body (lines 36-39) is uncovered (count=0)
const statementMap = {
"0": { start: { line: 25, column: 0 }, end: { line: 40, column: 1 } },
"1": { start: { line: 26, column: 4 }, end: { line: 27, column: 5 } },
"2": { start: { line: 36, column: 4 }, end: { line: 39, column: 5 } },
};
const statementCounts = {
"0": 1, // try-catch executed
"1": 1, // try body executed
"2": 0, // catch body NOT executed
};
const lines = extractLineCoverage(statementMap, statementCounts);
// Try body lines should be covered
t.is(lines["26"], 1);
t.is(lines["27"], 1);
// Catch body lines should be uncovered (inner statement overrides outer)
t.is(lines["36"], 0);
t.is(lines["37"], 0);
t.is(lines["38"], 0);
t.is(lines["39"], 0);
// Outer try-catch structural lines are covered
t.is(lines["25"], 1);
t.is(lines["40"], 1);
});
test("extractLineCoverage: same-size statements use max count (covered wins)", (t) => {
// Two single-line statements on the same line: if (x) foo(); else bar();
// foo() was called (count=1), bar() was not (count=0)
const statementMap = {
"0": { start: { line: 10, column: 0 }, end: { line: 10, column: 40 } },
"1": { start: { line: 10, column: 7 }, end: { line: 10, column: 20 } },
"2": { start: { line: 10, column: 26 }, end: { line: 10, column: 40 } },
};
const statementCounts = {
"0": 1, // if statement executed
"1": 1, // foo() called
"2": 0, // bar() not called
};
const lines = extractLineCoverage(statementMap, statementCounts);
// Line 10 should be covered because at least one statement on it was executed
t.is(lines["10"], 1);
});
test("extractLineCoverage: handles empty statement map", (t) => {
const lines = extractLineCoverage({}, {});
t.deepEqual(lines, {});
});