Skip to content

Commit

Permalink
test_runner: require --enable-source-maps for source map coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
RedYetiDev committed Sep 21, 2024
1 parent 8b8fc53 commit 2b633da
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 15 deletions.
25 changes: 12 additions & 13 deletions lib/internal/test_runner/coverage.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,19 @@ class CoverageLine {
}

class TestCoverage {
constructor(coverageDirectory, originalCoverageDirectory, workingDirectory, excludeGlobs, includeGlobs, thresholds) {
constructor(coverageDirectory, originalCoverageDirectory, workingDirectory, options) {
this.coverageDirectory = coverageDirectory;
this.originalCoverageDirectory = originalCoverageDirectory;
this.workingDirectory = workingDirectory;
this.excludeGlobs = excludeGlobs;
this.includeGlobs = includeGlobs;
this.thresholds = thresholds;
this.excludeGlobs = options.coverageExcludeGlobs;
this.includeGlobs = options.coverageIncludeGlobs;
this.enableSourceMaps = options.enableSourceMaps;
this.thresholds = {
__proto__: null,
line: options.lineCoverage,
branch: options.branchCoverage,
function: options.functionCoverage,
};
}

#sourceLines = new SafeMap();
Expand Down Expand Up @@ -335,7 +341,7 @@ class TestCoverage {
mapCoverageWithSourceMap(coverage) {
const { result } = coverage;
const sourceMapCache = coverage['source-map-cache'];
if (!sourceMapCache) {
if (!this.enableSourceMaps || !sourceMapCache) {
return result;
}
const newResult = new SafeMap();
Expand Down Expand Up @@ -505,14 +511,7 @@ function setupCoverage(options) {
coverageDirectory,
originalCoverageDirectory,
cwd,
options.coverageExcludeGlobs,
options.coverageIncludeGlobs,
{
__proto__: null,
line: options.lineCoverage,
branch: options.branchCoverage,
function: options.functionCoverage,
},
options,
);
}

Expand Down
2 changes: 2 additions & 0 deletions lib/internal/test_runner/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ function parseCommandLine() {
const forceExit = getOptionValue('--test-force-exit');
const sourceMaps = getOptionValue('--enable-source-maps');
const updateSnapshots = getOptionValue('--test-update-snapshots');
const enableSourceMaps = getOptionValue('--enable-source-maps');
const watch = getOptionValue('--watch');
const isChildProcess = process.env.NODE_TEST_CONTEXT === 'child';
const isChildProcessV8 = process.env.NODE_TEST_CONTEXT === 'child-v8';
Expand Down Expand Up @@ -311,6 +312,7 @@ function parseCommandLine() {
coverageExcludeGlobs,
coverageIncludeGlobs,
destinations,
enableSourceMaps,
forceExit,
isolation,
branchCoverage,
Expand Down
41 changes: 39 additions & 2 deletions test/parallel/test-runner-coverage.js
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,37 @@ test('coverage reports on lines, functions, and branches', skipIfNoInspector, as
});
});

test('coverage without --enable-source-map ignores sourcemaps', skipIfNoInspector, () => {
let report = [
'# start of coverage report',
'# --------------------------------------------------------------',
'# file | line % | branch % | funcs % | uncovered lines',
'# --------------------------------------------------------------',
'# a.test.mjs | 100.00 | 100.00 | 100.00 | ',
'# index.test.js | 71.43 | 66.67 | 100.00 | 6-7',
'# stdin.test.js | 100.00 | 100.00 | 100.00 | ',
'# --------------------------------------------------------------',
'# all files | 85.71 | 87.50 | 100.00 | ',
'# --------------------------------------------------------------',
'# end of coverage report',
].join('\n');

if (common.isWindows) {
report = report.replaceAll('/', '\\');
}

const fixture = fixtures.path('test-runner', 'coverage');
const args = [
'--test',
'--experimental-test-coverage',
'--test-reporter', 'tap',
];
const result = spawnSync(process.execPath, args, { cwd: fixture });
assert.strictEqual(result.stderr.toString(), '');
assert(result.stdout.toString().includes(report));
assert.strictEqual(result.status, 1);
});

test('coverage with source maps', skipIfNoInspector, () => {
let report = [
'# start of coverage report',
Expand All @@ -311,7 +342,10 @@ test('coverage with source maps', skipIfNoInspector, () => {

const fixture = fixtures.path('test-runner', 'coverage');
const args = [
'--test', '--experimental-test-coverage', '--test-reporter', 'tap',
'--enable-source-maps',
'--test',
'--experimental-test-coverage',
'--test-reporter', 'tap',
];
const result = spawnSync(process.execPath, args, { cwd: fixture });

Expand Down Expand Up @@ -489,7 +523,10 @@ test('coverage with included and excluded files', skipIfNoInspector, () => {
test('properly accounts for line endings in source maps', skipIfNoInspector, () => {
const fixture = fixtures.path('test-runner', 'source-map-line-lengths', 'index.js');
const args = [
'--test', '--experimental-test-coverage', '--test-reporter', 'tap',
'--enable-source-maps',
'--test',
'--experimental-test-coverage',
'--test-reporter', 'tap',
fixture,
];
const report = [
Expand Down

0 comments on commit 2b633da

Please sign in to comment.