Skip to content

Commit 61e7ae0

Browse files
MoLowmarco-ippolito
authored andcommittedMay 3, 2024
test_runner: use source maps when reporting coverage
PR-URL: #52060 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Chemi Atlow <chemi@atlow.co.il>
1 parent 5f1e7a0 commit 61e7ae0

File tree

11 files changed

+300
-118
lines changed

11 files changed

+300
-118
lines changed
 

‎lib/internal/source_map/source_map.js

+8
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ const {
7373
ArrayPrototypeSort,
7474
ObjectPrototypeHasOwnProperty,
7575
StringPrototypeCharAt,
76+
Symbol,
7677
} = primordials;
7778

7879
const { validateObject } = require('internal/validators');
@@ -83,6 +84,8 @@ const VLQ_BASE_SHIFT = 5;
8384
const VLQ_BASE_MASK = (1 << 5) - 1;
8485
const VLQ_CONTINUATION_MASK = 1 << 5;
8586

87+
const kMappings = Symbol('kMappings');
88+
8689
class StringCharIterator {
8790
/**
8891
* @constructor
@@ -153,6 +156,10 @@ class SourceMap {
153156
return cloneSourceMapV3(this.#payload);
154157
}
155158

159+
get [kMappings]() {
160+
return this.#mappings;
161+
}
162+
156163
/**
157164
* @return {number[] | undefined} line lengths of generated source code
158165
*/
@@ -382,5 +389,6 @@ function compareSourceMapEntry(entry1, entry2) {
382389
}
383390

384391
module.exports = {
392+
kMappings,
385393
SourceMap,
386394
};

‎lib/internal/test_runner/coverage.js

+199-118
Large diffs are not rendered by default.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
The files in the directory are generated by the
2+
following commands:
3+
4+
```sh
5+
npx esbuild a.test.ts --sourcemap --outdir=. --out-extension:.js=.mjs --sources-content=false --minify --bundle --platform=node --format=esm
6+
echo "import { test } from 'node:test';
7+
test('ok', () => {});
8+
9+
function uncovered() {
10+
return 'uncovered';
11+
}
12+
" | npx esbuild --sourcemap --sourcefile=stdin.test.ts --sources-content=true --bundle --platform=node --outfile="stdin.test.js"
13+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import{test as o}from"node:test";import{strictEqual as r}from"node:assert";function e(){r(1,2)}o("fails",()=>{e()});
2+
//# sourceMappingURL=a.test.mjs.map
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"version": 3,
3+
"sources": ["a.test.ts", "b.test.ts"],
4+
"mappings": "AAAA,OAAS,QAAAA,MAAY,YCArB,OAAS,eAAAC,MAAmB,cAErB,SAASC,GAAU,CACxBD,EAAY,EAAG,CAAC,CAClB,CDDAE,EAAK,QAAS,IAAM,CAClBC,EAAQ,CACV,CAAC",
5+
"names": ["test", "strictEqual", "covered", "test", "covered"]
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { test } from 'node:test';
2+
import { covered } from './b.test';
3+
4+
test('fails', () => {
5+
covered();
6+
});
7+
8+
function uncovered() {
9+
return 'uncovered';
10+
}
11+
if (false) {
12+
uncovered();
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { strictEqual } from 'node:assert';
2+
3+
export function covered() {
4+
strictEqual(1, 2);
5+
}
6+
7+
export function uncovered() {
8+
return 'uncovered';
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
'use strict';
2+
3+
const test = require('node:test');
4+
test('no soucre map', () => {});
5+
if (false) {
6+
console.log('this does not execute');
7+
}

‎test/fixtures/test-runner/coverage/stdin.test.js

+5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎test/fixtures/test-runner/coverage/stdin.test.js.map

+7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎test/parallel/test-runner-coverage.js

+31
Original file line numberDiff line numberDiff line change
@@ -242,3 +242,34 @@ test('coverage reports on lines, functions, and branches', skipIfNoInspector, as
242242
});
243243
});
244244
});
245+
246+
test('coverage with source maps', skipIfNoInspector, () => {
247+
let report = [
248+
'# start of coverage report',
249+
'# --------------------------------------------------------------',
250+
'# file | line % | branch % | funcs % | uncovered lines',
251+
'# --------------------------------------------------------------',
252+
'# a.test.ts | 53.85 | 100.00 | 100.00 | 8-13', // part of a bundle
253+
'# b.test.ts | 55.56 | 100.00 | 100.00 | 1 7-9', // part of a bundle
254+
'# index.test.js | 71.43 | 66.67 | 100.00 | 6-7', // no source map
255+
'# stdin.test.ts | 57.14 | 100.00 | 100.00 | 4-6', // Source map without original file
256+
'# --------------------------------------------------------------',
257+
'# all files | 58.33 | 87.50 | 100.00 |',
258+
'# --------------------------------------------------------------',
259+
'# end of coverage report',
260+
].join('\n');
261+
262+
if (common.isWindows) {
263+
report = report.replaceAll('/', '\\');
264+
}
265+
266+
const fixture = fixtures.path('test-runner', 'coverage');
267+
const args = [
268+
'--test', '--experimental-test-coverage', '--test-reporter', 'tap',
269+
];
270+
const result = spawnSync(process.execPath, args, { cwd: fixture });
271+
272+
assert.strictEqual(result.stderr.toString(), '');
273+
assert(result.stdout.toString().includes(report));
274+
assert.strictEqual(result.status, 1);
275+
});

0 commit comments

Comments
 (0)
Please sign in to comment.