forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-runner-coverage-source-map.js
More file actions
123 lines (108 loc) · 5.47 KB
/
test-runner-coverage-source-map.js
File metadata and controls
123 lines (108 loc) · 5.47 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
'use strict';
const common = require('../common');
const { pathToFileURL } = require('url');
const fixtures = require('../common/fixtures');
common.skipIfInspectorDisabled();
const { describe, it } = require('node:test');
function generateReport(report) {
report = ([
'# start of coverage report',
...report,
'# end of coverage report',
]).join('\n');
if (common.isWindows) {
report = report.replaceAll('/', '\\');
}
return report;
}
const flags = [
'--test', '--experimental-test-coverage', '--test-reporter', 'tap',
];
describe('Coverage with source maps', async () => {
await it('should work with source maps', async (t) => {
const report = generateReport([
'# --------------------------------------------------------------',
'# file | line % | branch % | funcs % | uncovered lines',
'# --------------------------------------------------------------',
'# a.test.ts | 53.85 | 100.00 | 100.00 | 8-13', // part of a bundle
'# b.test.ts | 55.56 | 100.00 | 100.00 | 1 7-9', // part of a bundle
'# index.test.js | 71.43 | 66.67 | 100.00 | 6-7', // no source map
'# stdin.test.ts | 57.14 | 100.00 | 100.00 | 4-6', // Source map without original file
'# --------------------------------------------------------------',
'# all files | 58.33 | 87.50 | 100.00 | ',
'# --------------------------------------------------------------',
]);
const spawned = await common.spawnPromisified(process.execPath, flags, {
cwd: fixtures.path('test-runner', 'coverage')
});
t.assert.strictEqual(spawned.stderr, '');
t.assert.ok(spawned.stdout.includes(report));
t.assert.strictEqual(spawned.code, 1);
});
it('accounts only mapped lines when --enable-source-maps is provided', async (t) => {
const report = generateReport([
'# --------------------------------------------------------------',
'# file | line % | branch % | funcs % | uncovered lines',
'# --------------------------------------------------------------',
'# a.test.ts | 100.00 | 100.00 | 100.00 | ', // part of a bundle
'# b.test.ts | 88.89 | 100.00 | 100.00 | 1', // part of a bundle
'# index.test.js | 71.43 | 66.67 | 100.00 | 6-7', // no source map
'# stdin.test.ts | 100.00 | 100.00 | 100.00 | ', // Source map without original file
'# --------------------------------------------------------------',
'# all files | 91.67 | 87.50 | 100.00 | ',
'# --------------------------------------------------------------',
]);
const spawned = await common.spawnPromisified(process.execPath, [
'--test', '--experimental-test-coverage', '--enable-source-maps', '--test-reporter', 'tap',
], {
cwd: fixtures.path('test-runner', 'coverage')
});
t.assert.strictEqual(spawned.stderr, '');
t.assert.ok(spawned.stdout.includes(report));
t.assert.strictEqual(spawned.code, 1);
});
await it('properly accounts for line endings in source maps', async (t) => {
const report = generateReport([
'# ------------------------------------------------------------------',
'# file | line % | branch % | funcs % | uncovered lines',
'# ------------------------------------------------------------------',
'# test | | | | ',
'# fixtures | | | | ',
'# test-runner | | | | ',
'# source-maps | | | | ',
'# line-lengths | | | | ',
'# index.ts | 100.00 | 100.00 | 100.00 | ',
'# ------------------------------------------------------------------',
'# all files | 100.00 | 100.00 | 100.00 | ',
'# ------------------------------------------------------------------',
]);
const spawned = await common.spawnPromisified(process.execPath, [
...flags,
fixtures.path('test-runner', 'source-maps', 'line-lengths', 'index.js'),
]);
t.assert.strictEqual(spawned.stderr, '');
t.assert.ok(spawned.stdout.includes(report));
t.assert.strictEqual(spawned.code, 0);
});
await it('should throw when a source map is missing a source file', async (t) => {
const file = fixtures.path('test-runner', 'source-maps', 'missing-sources', 'index.js');
const missing = fixtures.path('test-runner', 'source-maps', 'missing-sources', 'nonexistent.js');
const spawned = await common.spawnPromisified(process.execPath, [...flags, file]);
const error = `Cannot find '${pathToFileURL(missing)}' imported from the source map for '${pathToFileURL(file)}'`;
t.assert.strictEqual(spawned.stderr, '');
t.assert.ok(spawned.stdout.includes(error));
t.assert.strictEqual(spawned.code, 1);
});
for (const [file, message] of [
[fixtures.path('test-runner', 'source-maps', 'invalid-json', 'index.js'), 'is not valid JSON'],
[fixtures.path('test-runner', 'source-maps', 'missing-map.js'), 'does not exist'],
]) {
await it(`should throw when a source map ${message}`, async (t) => {
const spawned = await common.spawnPromisified(process.execPath, [...flags, file]);
const error = `The source map for '${pathToFileURL(file)}' does not exist or is corrupt`;
t.assert.strictEqual(spawned.stderr, '');
t.assert.ok(spawned.stdout.includes(error));
t.assert.strictEqual(spawned.code, 1);
});
}
}).then(common.mustCall());