Skip to content

Commit c8bd13f

Browse files
committed
test_runner: ignore unmapped lines for coverage
1 parent d24c731 commit c8bd13f

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

lib/internal/test_runner/coverage.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22
const {
33
ArrayFrom,
4+
ArrayPrototypeForEach,
45
ArrayPrototypeMap,
56
ArrayPrototypePush,
67
JSONParse,
@@ -56,12 +57,13 @@ class CoverageLine {
5657
}
5758

5859
class TestCoverage {
59-
constructor(coverageDirectory, originalCoverageDirectory, workingDirectory, excludeGlobs, includeGlobs, thresholds) {
60+
constructor(coverageDirectory, originalCoverageDirectory, workingDirectory, excludeGlobs, includeGlobs, sourceMaps, thresholds) {
6061
this.coverageDirectory = coverageDirectory;
6162
this.originalCoverageDirectory = originalCoverageDirectory;
6263
this.workingDirectory = workingDirectory;
6364
this.excludeGlobs = excludeGlobs;
6465
this.includeGlobs = includeGlobs;
66+
this.sourceMaps = sourceMaps;
6567
this.thresholds = thresholds;
6668
}
6769

@@ -364,7 +366,13 @@ class TestCoverage {
364366
this.getLines(data.sources[j], data.sourcesContent[j]);
365367
}
366368
}
369+
367370
const sourceMap = new SourceMap(data, { __proto__: null, lineLengths });
371+
const linesToCover = new SafeSet();
372+
373+
for (let i = 0; i < sourceMap[kMappings].length; i++) {
374+
linesToCover.add(sourceMap[kMappings][i][3] + 1);
375+
}
368376

369377
for (let j = 0; j < functions.length; ++j) {
370378
const { ranges, functionName, isBlockCoverage } = functions[j];
@@ -413,6 +421,15 @@ class TestCoverage {
413421
// No mappable ranges. Skip the function.
414422
continue;
415423
}
424+
425+
if (this.sourceMaps) {
426+
ArrayPrototypeForEach(this.getLines(newUrl), (mappedLine) => {
427+
if (!linesToCover.has(mappedLine.line)) {
428+
mappedLine.ignore = true;
429+
}
430+
});
431+
}
432+
416433
const newScript = newResult.get(newUrl) ?? { __proto__: null, url: newUrl, functions: [] };
417434
ArrayPrototypePush(newScript.functions, { __proto__: null, functionName, ranges: newRanges, isBlockCoverage });
418435
newResult.set(newUrl, newScript);
@@ -515,6 +532,7 @@ function setupCoverage(options) {
515532
cwd,
516533
options.coverageExcludeGlobs,
517534
options.coverageIncludeGlobs,
535+
options.sourceMaps,
518536
{
519537
__proto__: null,
520538
line: options.lineCoverage,

0 commit comments

Comments
 (0)