Skip to content

Commit 67517de

Browse files
committed
test_runner: ignore unmapped lines for coverage
1 parent 98788da commit 67517de

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

lib/internal/test_runner/coverage.js

Lines changed: 27 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,21 @@ class CoverageLine {
5657
}
5758

5859
class TestCoverage {
59-
constructor(coverageDirectory, originalCoverageDirectory, workingDirectory, excludeGlobs, includeGlobs, thresholds) {
60+
constructor(
61+
coverageDirectory,
62+
originalCoverageDirectory,
63+
workingDirectory,
64+
excludeGlobs,
65+
includeGlobs,
66+
sourceMaps,
67+
thresholds,
68+
) {
6069
this.coverageDirectory = coverageDirectory;
6170
this.originalCoverageDirectory = originalCoverageDirectory;
6271
this.workingDirectory = workingDirectory;
6372
this.excludeGlobs = excludeGlobs;
6473
this.includeGlobs = includeGlobs;
74+
this.sourceMaps = sourceMaps;
6575
this.thresholds = thresholds;
6676
}
6777

@@ -364,7 +374,13 @@ class TestCoverage {
364374
this.getLines(data.sources[j], data.sourcesContent[j]);
365375
}
366376
}
377+
367378
const sourceMap = new SourceMap(data, { __proto__: null, lineLengths });
379+
const linesToCover = new SafeSet();
380+
381+
for (let i = 0; i < sourceMap[kMappings].length; i++) {
382+
linesToCover.add(sourceMap[kMappings][i][3] + 1);
383+
}
368384

369385
for (let j = 0; j < functions.length; ++j) {
370386
const { ranges, functionName, isBlockCoverage } = functions[j];
@@ -413,6 +429,15 @@ class TestCoverage {
413429
// No mappable ranges. Skip the function.
414430
continue;
415431
}
432+
433+
if (this.sourceMaps) {
434+
ArrayPrototypeForEach(this.getLines(newUrl), (mappedLine) => {
435+
if (!linesToCover.has(mappedLine.line)) {
436+
mappedLine.ignore = true;
437+
}
438+
});
439+
}
440+
416441
const newScript = newResult.get(newUrl) ?? { __proto__: null, url: newUrl, functions: [] };
417442
ArrayPrototypePush(newScript.functions, { __proto__: null, functionName, ranges: newRanges, isBlockCoverage });
418443
newResult.set(newUrl, newScript);
@@ -514,6 +539,7 @@ function setupCoverage(options) {
514539
options.cwd,
515540
options.coverageExcludeGlobs,
516541
options.coverageIncludeGlobs,
542+
options.sourceMaps,
517543
{
518544
__proto__: null,
519545
line: options.lineCoverage,

0 commit comments

Comments
 (0)