Skip to content

🐛 Bug: original require() errors are masked with an unrelated ERR_UNKNOWN_FILE_EXTENSION #5393

Open
@lkho

Description

@lkho

Bug Report Checklist

  • I have read and agree to Mocha's Code of Conduct and Contributing Guidelines
  • I have searched for related issues and issues with the faq label, but none matched my issue.
  • I have 'smoke tested' the code to be tested by running it outside the real test suite to get a better sense of whether the problem is in the code under test, my usage of Mocha, or Mocha itself.
  • I want to provide a PR to resolve this (but after community discussion first)

Expected

in 11.7.0, if require() fails, the error is thrown and output to console. e.g.:

 $ mocha --require ts-node/register --require source-map-support/register 'a.ts'
 
  Exception during run: .../a.ts:61:66 - error TS2345: Argument of type '{ address: string; }' is not assignable to parameter of type ...
 
 $ mocha --version
 11.7.0

Actual

after this change #5384 (comment)
the err is masked and returned an ERR_UNKNOWN_FILE_EXTENSION which is misleading.

 $ mocha --require ts-node/register --require source-map-support/register 'a.ts'
 
  Exception during run: TypeError: Unknown file extension ".ts" for /.../a.ts
     at Object.getFileProtocolModuleFormat [as file:] (node:internal/modules/esm/get_format:219:9)
     at defaultGetFormat (node:internal/modules/esm/get_format:245:36)
     at defaultLoad (node:internal/modules/esm/load:120:22)
     at async ModuleLoader.loadAndTranslate (node:internal/modules/esm/loader:580:32)
     at async ModuleJob._link (node:internal/modules/esm/module_job:116:19) {
   code: 'ERR_UNKNOWN_FILE_EXTENSION'
 }
 
 $ mocha --version
 11.7.1

Minimal, Reproducible Example

  1. install ts-node and require it along with the mocha command i.e. mocha --require ts-node/register abc.ts
  2. try to run with a giblish ts file. e.g.
bla@#&#_@$

what ever contents in it causing a ts not compilable will output the same ERR_UNKNOWN_FILE_EXTENSION error

Versions

11.7.1 not work
11.7.0 works

Additional Info

I think this is open to discussion whether this should be classified as bug or not, but I just want to point out the breaking behavioral change in this patch version release which might potentially affect people who rely on the error output for debugging purpose or experiencing surprises suddenly.

Metadata

Metadata

Assignees

No one assigned

    Labels

    status: accepting prsMocha can use your help with this one!type: buga defect, confirmed by a maintainer

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions