diff --git a/lib/internal/test_runner/coverage.js b/lib/internal/test_runner/coverage.js index 1b5ba7912dcb7d..a0aaa225f8e39c 100644 --- a/lib/internal/test_runner/coverage.js +++ b/lib/internal/test_runner/coverage.js @@ -1,6 +1,7 @@ 'use strict'; const { ArrayFrom, + ArrayPrototypeForEach, ArrayPrototypeMap, ArrayPrototypePush, JSONParse, @@ -365,6 +366,12 @@ class TestCoverage { } } const sourceMap = new SourceMap(data, { __proto__: null, lineLengths }); + const linesToCover = new SafeSet(); + + for (let i = 0; i < sourceMap[kMappings].length; i++) { + const { 3: originalLine } = sourceMap[kMappings][i]; + linesToCover.add(originalLine + 1); + } for (let j = 0; j < functions.length; ++j) { const { ranges, functionName, isBlockCoverage } = functions[j]; @@ -413,6 +420,13 @@ class TestCoverage { // No mappable ranges. Skip the function. continue; } + + ArrayPrototypeForEach(this.getLines(newUrl), (mappedLine) => { + if (!linesToCover.has(mappedLine.line)) { + mappedLine.ignore = true; + } + }); + const newScript = newResult.get(newUrl) ?? { __proto__: null, url: newUrl, functions: [] }; ArrayPrototypePush(newScript.functions, { __proto__: null, functionName, ranges: newRanges, isBlockCoverage }); newResult.set(newUrl, newScript); diff --git a/test/parallel/test-runner-coverage.js b/test/parallel/test-runner-coverage.js index 77e0afadbdff95..a39bdea493d4df 100644 --- a/test/parallel/test-runner-coverage.js +++ b/test/parallel/test-runner-coverage.js @@ -296,12 +296,12 @@ test('coverage with source maps', skipIfNoInspector, () => { '# --------------------------------------------------------------', '# 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 + '# 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 | 57.14 | 100.00 | 100.00 | 4-6', // Source map without original file + '# stdin.test.ts | 100.00 | 100.00 | 100.00 | ', // Source map with original file '# --------------------------------------------------------------', - '# all files | 58.33 | 87.50 | 100.00 | ', + '# all files | 91.67 | 87.50 | 100.00 | ', '# --------------------------------------------------------------', '# end of coverage report', ].join('\n');