Skip to content

Commit bc0ee1a

Browse files
committed
test_runner: fix coverage report when a directory is named file
The coverage tree traversal checked `tree[key].file` to detect file entries. When a directory named "file" contained a file also named "file", this check incorrectly matched the child entry instead of file metadata, causing a TypeError when accessing `.path`. Check for `.file?.path` instead to correctly identify file metadata. Fixes: #61080
1 parent 13073a9 commit bc0ee1a

File tree

4 files changed

+28
-1
lines changed

4 files changed

+28
-1
lines changed

lib/internal/test_runner/utils.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -551,7 +551,7 @@ function getCoverageReport(pad, summary, symbol, color, table) {
551551

552552
function printCoverageBodyTree(tree, depth = 0) {
553553
for (const key in tree) {
554-
if (tree[key].file) {
554+
if (tree[key].file?.path) {
555555
const file = tree[key].file;
556556
const fileName = ArrayPrototypePop(StringPrototypeSplit(file.path, sep));
557557

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
'use strict';
2+
module.exports.fn = function() {
3+
return 1;
4+
};
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
'use strict';
2+
const { fn } = require('./file/file');
3+
const test = require('node:test');
4+
5+
test('coverage with file/file directory structure', () => {
6+
fn();
7+
});

test/parallel/test-runner-coverage.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -550,3 +550,19 @@ test('correctly prints the coverage report of files contained in parent director
550550
assert(result.stdout.toString().includes(report));
551551
assert.strictEqual(result.status, 0);
552552
});
553+
554+
// Regression test for https://github.com/nodejs/node/issues/61080
555+
test('coverage with directory and file named "file"', skipIfNoInspector, () => {
556+
const fixture = fixtures.path('test-runner', 'coverage-file-name', 'test.js');
557+
const args = [
558+
'--experimental-test-coverage',
559+
'--test-reporter',
560+
'tap',
561+
fixture,
562+
];
563+
const result = spawnSync(process.execPath, args);
564+
565+
assert.strictEqual(result.stderr.toString(), '');
566+
assert.strictEqual(result.status, 0);
567+
assert(result.stdout.toString().includes('start of coverage report'));
568+
});

0 commit comments

Comments
 (0)