Skip to content

--experimental-test-coverage throws TypeError if directory is named file #61080

Description

@joshuaavalon

Version

24.12.0

Platform

Microsoft Windows NT 10.0.22631.0 x64

Subsystem

test_runner

What steps will reproduce the bug?

Run test node test with --experimental-test-coverage on source code with a directory named file.

Steps to Reproduce

  1. Create src/file/index.js
exportfunctiona(){console.log("!");return1;}
  1. Create src/file/__tests__/index.spec.js
import{describe,it}from"node:test";importassertfrom"node:assert/strict";import{a}from"../index.js"describe("a",asyncfunction(){it("test",asyncfunction(){constresult=a();assert.equal(result,1);})});
  1. npm run test with node --experimental-test-coverage --test \"./src/**/__tests__/*.{spec,test}.js\".

Findings

functionprintCoverageBodyTree(tree,depth=0){
for(constkeyintree){
if(tree[key].file){
constfile=tree[key].file;

In printCoverageBodyTree, a directory named file seems to conflict with internal variable named file. Causing it to treat the directory as a file.

How often does it reproduce? Is there a required condition?

100%

What is the expected behavior? Why is that the expected behavior?

Generate test coverage normally.

What do you see instead?

TypeError: String.prototype.split called on null or undefined
at split (<anonymous>)
at printCoverageBodyTree (node:internal/test_runner/utils:536:44)
at printCoverageBodyTree (node:internal/test_runner/utils:565:9)
at getCoverageReport (node:internal/test_runner/utils:585:3)
at #handleEvent (node:internal/test_runner/reporter/spec:102:16)
at SpecReporter._transform (node:internal/test_runner/reporter/spec:112:37)
at Transform._write (node:internal/streams/transform:171:8)
at writeOrBuffer (node:internal/streams/writable:570:12)
at _write (node:internal/streams/writable:499:10)
at Writable.write (node:internal/streams/writable:508:10)
Emitted 'error' event on Readable instance at:
at emitErrorNT (node:internal/streams/destroy:170:8)
at emitErrorCloseNT (node:internal/streams/destroy:129:3)
at process.processTicksAndRejections (node:internal/process/task_queues:90:21)

Additional information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions