Skip to content

Commit 1b30f7f

Browse files
avivkellertargos
authored andcommitted
test: move coverage source map tests to new file
PR-URL: #55123 Reviewed-By: Moshe Atlow <moshe@atlow.co.il> Reviewed-By: Chemi Atlow <chemi@atlow.co.il>
1 parent f89664d commit 1b30f7f

File tree

7 files changed

+83
-74
lines changed

7 files changed

+83
-74
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
'use strict';
2+
const common = require('../common');
3+
const { pathToFileURL } = require('url');
4+
const fixtures = require('../common/fixtures');
5+
common.skipIfInspectorDisabled();
6+
7+
const { describe, it } = require('node:test');
8+
9+
function generateReport(report) {
10+
report = ([
11+
'# start of coverage report',
12+
...report,
13+
'# end of coverage report',
14+
]).join('\n');
15+
if (common.isWindows) {
16+
report = report.replaceAll('/', '\\');
17+
}
18+
return report;
19+
}
20+
21+
const flags = [
22+
'--test', '--experimental-test-coverage', '--test-reporter', 'tap',
23+
];
24+
25+
describe('Coverage with source maps', async () => {
26+
await it('should work with source maps', async (t) => {
27+
const report = generateReport([
28+
'# --------------------------------------------------------------',
29+
'# file | line % | branch % | funcs % | uncovered lines',
30+
'# --------------------------------------------------------------',
31+
'# a.test.ts | 53.85 | 100.00 | 100.00 | 8-13', // part of a bundle
32+
'# b.test.ts | 55.56 | 100.00 | 100.00 | 1 7-9', // part of a bundle
33+
'# index.test.js | 71.43 | 66.67 | 100.00 | 6-7', // no source map
34+
'# stdin.test.ts | 57.14 | 100.00 | 100.00 | 4-6', // Source map without original file
35+
'# --------------------------------------------------------------',
36+
'# all files | 58.33 | 87.50 | 100.00 | ',
37+
'# --------------------------------------------------------------',
38+
]);
39+
40+
const spawned = await common.spawnPromisified(process.execPath, flags, {
41+
cwd: fixtures.path('test-runner', 'coverage')
42+
});
43+
t.assert.strictEqual(spawned.stderr, '');
44+
t.assert.ok(spawned.stdout.includes(report));
45+
t.assert.strictEqual(spawned.code, 1);
46+
});
47+
48+
await it('properly accounts for line endings in source maps', async (t) => {
49+
const report = generateReport([
50+
'# ------------------------------------------------------------------',
51+
'# file | line % | branch % | funcs % | uncovered lines',
52+
'# ------------------------------------------------------------------',
53+
'# test | | | | ',
54+
'# fixtures | | | | ',
55+
'# test-runner | | | | ',
56+
'# source-maps | | | | ',
57+
'# line-lengths | | | | ',
58+
'# index.ts | 100.00 | 100.00 | 100.00 | ',
59+
'# ------------------------------------------------------------------',
60+
'# all files | 100.00 | 100.00 | 100.00 | ',
61+
'# ------------------------------------------------------------------',
62+
]);
63+
64+
const spawned = await common.spawnPromisified(process.execPath, [
65+
...flags,
66+
fixtures.path('test-runner', 'source-maps', 'line-lengths', 'index.js'),
67+
]);
68+
t.assert.strictEqual(spawned.stderr, '');
69+
t.assert.ok(spawned.stdout.includes(report));
70+
t.assert.strictEqual(spawned.code, 0);
71+
});
72+
73+
await it('should throw when a source map is missing a source file', async (t) => {
74+
const file = fixtures.path('test-runner', 'source-maps', 'missing-sources', 'index.js');
75+
const missing = fixtures.path('test-runner', 'source-maps', 'missing-sources', 'nonexistent.js');
76+
const spawned = await common.spawnPromisified(process.execPath, [...flags, file]);
77+
78+
const error = `Cannot find '${pathToFileURL(missing)}' imported from the source map for '${pathToFileURL(file)}'`;
79+
t.assert.strictEqual(spawned.stderr, '');
80+
t.assert.ok(spawned.stdout.includes(error));
81+
t.assert.strictEqual(spawned.code, 1);
82+
});
83+
}).then(common.mustCall());

test/parallel/test-runner-coverage.js

-74
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ const { readdirSync } = require('node:fs');
66
const { test } = require('node:test');
77
const fixtures = require('../common/fixtures');
88
const tmpdir = require('../common/tmpdir');
9-
const { pathToFileURL } = require('node:url');
109
const skipIfNoInspector = {
1110
skip: !process.features.inspector ? 'inspector disabled' : false
1211
};
@@ -290,51 +289,6 @@ test('coverage reports on lines, functions, and branches', skipIfNoInspector, as
290289
});
291290
});
292291

293-
test('coverage with source maps', skipIfNoInspector, () => {
294-
let report = [
295-
'# start of coverage report',
296-
'# --------------------------------------------------------------',
297-
'# file | line % | branch % | funcs % | uncovered lines',
298-
'# --------------------------------------------------------------',
299-
'# a.test.ts | 53.85 | 100.00 | 100.00 | 8-13', // part of a bundle
300-
'# b.test.ts | 55.56 | 100.00 | 100.00 | 1 7-9', // part of a bundle
301-
'# index.test.js | 71.43 | 66.67 | 100.00 | 6-7', // no source map
302-
'# stdin.test.ts | 57.14 | 100.00 | 100.00 | 4-6', // Source map without original file
303-
'# --------------------------------------------------------------',
304-
'# all files | 58.33 | 87.50 | 100.00 | ',
305-
'# --------------------------------------------------------------',
306-
'# end of coverage report',
307-
].join('\n');
308-
309-
if (common.isWindows) {
310-
report = report.replaceAll('/', '\\');
311-
}
312-
313-
const fixture = fixtures.path('test-runner', 'coverage');
314-
const args = [
315-
'--test', '--experimental-test-coverage', '--test-reporter', 'tap',
316-
];
317-
const result = spawnSync(process.execPath, args, { cwd: fixture });
318-
319-
assert.strictEqual(result.stderr.toString(), '');
320-
assert(result.stdout.toString().includes(report));
321-
assert.strictEqual(result.status, 1);
322-
});
323-
324-
test('coverage with source maps missing sources', skipIfNoInspector, () => {
325-
const file = fixtures.path('test-runner', 'source-map-missing-sources', 'index.js');
326-
const missing = fixtures.path('test-runner', 'source-map-missing-sources', 'nonexistent.js');
327-
const result = spawnSync(process.execPath, [
328-
'--test',
329-
'--experimental-test-coverage',
330-
file,
331-
]);
332-
333-
const error = `Cannot find '${pathToFileURL(missing)}' imported from the source map for '${pathToFileURL(file)}'`;
334-
assert(result.stdout.toString().includes(error));
335-
assert.strictEqual(result.status, 1);
336-
});
337-
338292
test('coverage with ESM hook - source irrelevant', skipIfNoInspector, () => {
339293
let report = [
340294
'# start of coverage report',
@@ -501,34 +455,6 @@ test('coverage with included and excluded files', skipIfNoInspector, () => {
501455
assert(!findCoverageFileForPid(result.pid));
502456
});
503457

504-
test('properly accounts for line endings in source maps', skipIfNoInspector, () => {
505-
const fixture = fixtures.path('test-runner', 'source-map-line-lengths', 'index.js');
506-
const args = [
507-
'--test', '--experimental-test-coverage', '--test-reporter', 'tap',
508-
fixture,
509-
];
510-
const report = [
511-
'# start of coverage report',
512-
'# ----------------------------------------------------------------------------',
513-
'# file | line % | branch % | funcs % | uncovered lines',
514-
'# ----------------------------------------------------------------------------',
515-
'# test | | | | ',
516-
'# fixtures | | | | ',
517-
'# test-runner | | | | ',
518-
'# source-map-line-lengths | | | | ',
519-
'# index.ts | 100.00 | 100.00 | 100.00 | ',
520-
'# ----------------------------------------------------------------------------',
521-
'# all files | 100.00 | 100.00 | 100.00 | ',
522-
'# ----------------------------------------------------------------------------',
523-
'# end of coverage report',
524-
].join('\n');
525-
526-
const result = spawnSync(process.execPath, args);
527-
assert.strictEqual(result.stderr.toString(), '');
528-
assert(result.stdout.toString().includes(report));
529-
assert.strictEqual(result.status, 0);
530-
});
531-
532458
test('correctly prints the coverage report of files contained in parent directories', skipIfNoInspector, () => {
533459
let report = [
534460
'# start of coverage report',

0 commit comments

Comments
 (0)