Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test_runner: fix coverage report when --enable-source-maps is provided #55228

Merged
merged 3 commits into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion lib/internal/test_runner/coverage.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';
const {
ArrayFrom,
ArrayPrototypeForEach,
ArrayPrototypeMap,
ArrayPrototypePush,
JSONParse,
Expand Down Expand Up @@ -57,12 +58,21 @@ class CoverageLine {
}

class TestCoverage {
constructor(coverageDirectory, originalCoverageDirectory, workingDirectory, excludeGlobs, includeGlobs, thresholds) {
constructor(
coverageDirectory,
originalCoverageDirectory,
workingDirectory,
excludeGlobs,
includeGlobs,
sourceMaps,
thresholds,
) {
this.coverageDirectory = coverageDirectory;
this.originalCoverageDirectory = originalCoverageDirectory;
this.workingDirectory = workingDirectory;
this.excludeGlobs = excludeGlobs;
this.includeGlobs = includeGlobs;
this.sourceMaps = sourceMaps;
this.thresholds = thresholds;
}

Expand Down Expand Up @@ -366,7 +376,13 @@ class TestCoverage {
this.getLines(data.sources[j], data.sourcesContent[j]);
}
}

const sourceMap = new SourceMap(data, { __proto__: null, lineLengths });
const linesToCover = new SafeSet();

for (let i = 0; i < sourceMap[kMappings].length; i++) {
linesToCover.add(sourceMap[kMappings][i][3] + 1);
}

for (let j = 0; j < functions.length; ++j) {
const { ranges, functionName, isBlockCoverage } = functions[j];
Expand Down Expand Up @@ -415,6 +431,15 @@ class TestCoverage {
// No mappable ranges. Skip the function.
continue;
}

if (this.sourceMaps) {
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);
Expand Down Expand Up @@ -516,6 +541,7 @@ function setupCoverage(options) {
options.cwd,
options.coverageExcludeGlobs,
options.coverageIncludeGlobs,
options.sourceMaps,
{
__proto__: null,
line: options.lineCoverage,
Expand Down
26 changes: 26 additions & 0 deletions test/parallel/test-runner-coverage-source-map.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,31 @@ describe('Coverage with source maps', async () => {
t.assert.strictEqual(spawned.code, 1);
});

it('accounts only mapped lines when --enable-source-maps is provided', async (t) => {
const report = generateReport([
'# --------------------------------------------------------------',
'# file | line % | branch % | funcs % | uncovered lines',
'# --------------------------------------------------------------',
'# 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 | 100.00 | 100.00 | 100.00 | ', // Source map without original file
geeksilva97 marked this conversation as resolved.
Show resolved Hide resolved
'# --------------------------------------------------------------',
'# all files | 91.67 | 87.50 | 100.00 | ',
'# --------------------------------------------------------------',
]);

const spawned = await common.spawnPromisified(process.execPath, [
'--test', '--experimental-test-coverage', '--enable-source-maps', '--test-reporter', 'tap',
], {
cwd: fixtures.path('test-runner', 'coverage')
});

t.assert.strictEqual(spawned.stderr, '');
t.assert.ok(spawned.stdout.includes(report));
t.assert.strictEqual(spawned.code, 1);
});

await it('properly accounts for line endings in source maps', async (t) => {
const report = generateReport([
'# ------------------------------------------------------------------',
Expand Down Expand Up @@ -89,6 +114,7 @@ describe('Coverage with source maps', async () => {
const spawned = await common.spawnPromisified(process.execPath, [...flags, file]);

const error = `The source map for '${pathToFileURL(file)}' does not exist or is corrupt`;

t.assert.strictEqual(spawned.stderr, '');
t.assert.ok(spawned.stdout.includes(error));
t.assert.strictEqual(spawned.code, 1);
Expand Down
Loading