🚀 Feature: if loading a file as CJS and ESM both fail, display both errors instead of just ESM #5235
Description
Feature Request 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 want to provide a PR to resolve this
Overview
Ever since chai
v5 dropped support for CJS, we get errors like this when we accidentally install chai 5+:
$ mocha
Exception during run: TypeError: Unknown file extension ".ts" for /Users/user/src/battalion/storage-site-sim/src/__tests__/SeededRNG.test.ts
at Object.getFileProtocolModuleFormat [as file:] (node:internal/modules/esm/get_format:160:9)
at defaultGetFormat (node:internal/modules/esm/get_format:203:36)
at defaultLoad (node:internal/modules/esm/load:141:22)
at ModuleLoader.load (node:internal/modules/esm/loader:409:7)
at ModuleLoader.moduleProvider (node:internal/modules/esm/loader:291:45)
at link (node:internal/modules/esm/module_job:76:21) {
code: 'ERR_UNKNOWN_FILE_EXTENSION'
}
This threw us for a loop at first, because normally our tests work fine with @babel/register
handling .ts
files.
What's happening is first Mocha tries to load our file as CJS, which make it as far as require('chai')
which fails with ERROR_REQUIRE_ESM
. Then Mocha tries to load our file as ESM instead, which triggers the above error.
Suggested Solution
If Mocha would print out the error from loading our file as CJS in addition to the above error, we would have seen what caused the problem more quickly.
Alternatives
I've thought about adding code to our toolchain to check for chai v5+ and explicitly warn...but this same issue could occur with any package we're using in tests that drops CJS support
I guess another solution would be a mocharc option to force Mocha to only try one of CJS or ESM, not both.
I can't just put type: "commonjs"
in my package.json
because I made my toolchain run tests in CJS mode with @babel/register
, and then in ESM mode with babel-register-esm
, since I'm transpiling my TypeScript source to both CJS and ESM for publishing.
Additional Info
No response
Activity