Skip to content

Commit

Permalink
test_runner: require --enable-source-maps for sourcemap coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
RedYetiDev committed Oct 11, 2024
1 parent aeadaba commit 2cc8c10
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 26 deletions.
48 changes: 22 additions & 26 deletions lib/internal/test_runner/coverage.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,10 @@ class CoverageLine {
}

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

#sourceLines = new SafeMap();
Expand Down Expand Up @@ -136,7 +133,7 @@ class TestCoverage {
const coverage = this.getCoverageFromDirectory();
const coverageSummary = {
__proto__: null,
workingDirectory: this.workingDirectory,
workingDirectory: this.globalOptions.cwd,
files: [],
totals: {
__proto__: null,
Expand All @@ -150,7 +147,12 @@ class TestCoverage {
coveredBranchPercent: 0,
coveredFunctionPercent: 0,
},
thresholds: this.thresholds,
thresholds: {
__proto__: null,
line: this.globalOptions.lineCoverage,
branch: this.globalOptions.branchCoverage,
function: this.globalOptions.functionCoverage,
},
};

if (!coverage) {
Expand Down Expand Up @@ -341,7 +343,7 @@ class TestCoverage {
mapCoverageWithSourceMap(coverage) {
const { result } = coverage;
const sourceMapCache = coverage['source-map-cache'];
if (!sourceMapCache) {
if (!this.globalOptions.sourceMaps || !sourceMapCache) {
return result;
}
const newResult = new SafeMap();
Expand Down Expand Up @@ -455,21 +457,23 @@ class TestCoverage {
if (!StringPrototypeStartsWith(url, 'file:')) return true;

const absolutePath = fileURLToPath(url);
const relativePath = relative(this.workingDirectory, absolutePath);
const relativePath = relative(this.globalOptions.cwd, absolutePath);

// This check filters out files that match the exclude globs.
if (this.excludeGlobs?.length > 0) {
for (let i = 0; i < this.excludeGlobs.length; ++i) {
if (matchesGlob(relativePath, this.excludeGlobs[i]) ||
matchesGlob(absolutePath, this.excludeGlobs[i])) return true;
const excludeGlobs = this.globalOptions.coverageExcludeGlobs;
if (excludeGlobs?.length > 0) {
for (let i = 0; i < excludeGlobs.length; ++i) {
if (matchesGlob(relativePath, excludeGlobs[i]) ||
matchesGlob(absolutePath, excludeGlobs[i])) return true;
}
}

// This check filters out files that do not match the include globs.
if (this.includeGlobs?.length > 0) {
for (let i = 0; i < this.includeGlobs.length; ++i) {
if (matchesGlob(relativePath, this.includeGlobs[i]) ||
matchesGlob(absolutePath, this.includeGlobs[i])) return false;
const includeGlobs = this.globalOptions.coverageIncludeGlobs;
if (includeGlobs?.length > 0) {
for (let i = 0; i < includeGlobs.length; ++i) {
if (matchesGlob(relativePath, includeGlobs[i]) ||
matchesGlob(absolutePath, includeGlobs[i])) return false;
}
return true;
}
Expand Down Expand Up @@ -511,15 +515,7 @@ function setupCoverage(options) {
return new TestCoverage(
coverageDirectory,
originalCoverageDirectory,
options.cwd,
options.coverageExcludeGlobs,
options.coverageIncludeGlobs,
{
__proto__: null,
line: options.lineCoverage,
branch: options.branchCoverage,
function: options.functionCoverage,
},
options,
);
}

Expand Down
23 changes: 23 additions & 0 deletions test/parallel/test-runner-coverage-source-map.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ function generateReport(report) {
}

const flags = [
'--enable-source-maps',
'--test', '--experimental-test-coverage', '--test-reporter', 'tap',
];

Expand All @@ -40,6 +41,28 @@ describe('Coverage with source maps', async () => {
const spawned = await common.spawnPromisified(process.execPath, flags, {
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('should only work with --enable-source-maps', async (t) => {
const report = generateReport([
'# --------------------------------------------------------------',
'# 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 | ',
'# --------------------------------------------------------------',
]);

const spawned = await common.spawnPromisified(process.execPath, flags.slice(1), {
cwd: fixtures.path('test-runner', 'coverage')
});
t.assert.strictEqual(spawned.stderr, '');
t.assert.ok(spawned.stdout.includes(report));
t.assert.strictEqual(spawned.code, 1);
Expand Down

0 comments on commit 2cc8c10

Please sign in to comment.