Skip to content

Commit f44115f

Browse files
committed
test_runner: ignore unmapped lines for coverage
1 parent deb5eff commit f44115f

1 file changed

Lines changed: 27 additions & 1 deletion

File tree

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,
@@ -57,12 +58,21 @@ class CoverageLine {
5758
}
5859

5960
class TestCoverage {
60-
constructor(coverageDirectory, originalCoverageDirectory, workingDirectory, excludeGlobs, includeGlobs, thresholds) {
61+
constructor(
62+
coverageDirectory,
63+
originalCoverageDirectory,
64+
workingDirectory,
65+
excludeGlobs,
66+
includeGlobs,
67+
sourceMaps,
68+
thresholds,
69+
) {
6170
this.coverageDirectory = coverageDirectory;
6271
this.originalCoverageDirectory = originalCoverageDirectory;
6372
this.workingDirectory = workingDirectory;
6473
this.excludeGlobs = excludeGlobs;
6574
this.includeGlobs = includeGlobs;
75+
this.sourceMaps = sourceMaps;
6676
this.thresholds = thresholds;
6777
}
6878

@@ -366,7 +376,13 @@ class TestCoverage {
366376
this.getLines(data.sources[j], data.sourcesContent[j]);
367377
}
368378
}
379+
369380
const sourceMap = new SourceMap(data, { __proto__: null, lineLengths });
381+
const linesToCover = new SafeSet();
382+
383+
for (let i = 0; i < sourceMap[kMappings].length; i++) {
384+
linesToCover.add(sourceMap[kMappings][i][3] + 1);
385+
}
370386

371387
for (let j = 0; j < functions.length; ++j) {
372388
const { ranges, functionName, isBlockCoverage } = functions[j];
@@ -415,6 +431,15 @@ class TestCoverage {
415431
// No mappable ranges. Skip the function.
416432
continue;
417433
}
434+
435+
if (this.sourceMaps) {
436+
ArrayPrototypeForEach(this.getLines(newUrl), (mappedLine) => {
437+
if (!linesToCover.has(mappedLine.line)) {
438+
mappedLine.ignore = true;
439+
}
440+
});
441+
}
442+
418443
const newScript = newResult.get(newUrl) ?? { __proto__: null, url: newUrl, functions: [] };
419444
ArrayPrototypePush(newScript.functions, { __proto__: null, functionName, ranges: newRanges, isBlockCoverage });
420445
newResult.set(newUrl, newScript);
@@ -516,6 +541,7 @@ function setupCoverage(options) {
516541
options.cwd,
517542
options.coverageExcludeGlobs,
518543
options.coverageIncludeGlobs,
544+
options.sourceMaps,
519545
{
520546
__proto__: null,
521547
line: options.lineCoverage,

0 commit comments

Comments
 (0)