mochajs / mocha

☕️ simple, flexible, fun javascript test framework for node.js & the browser
https://mochajs.org
MIT License
22.55k stars 3.01k forks source link

Mocha does not work with typescript when `compilerOptions.module` set to `esnext` #4919

Closed ryanzec closed 1 year ago

ryanzec commented 2 years ago

Prerequisites

Description

When attempting to use Mocha with TypeScript that has compilerOptions.module set to esnext, mocha fails to work with the following error:

TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" for /Users/ryanzec/repositories/monorepo-template/packages/utils/tests/drag-drop.spec.ts
    at new NodeError (node:internal/errors:372:5)
    at Object.getFileProtocolModuleFormat [as file:] (node:internal/modules/esm/get_format:76:11)
    at defaultGetFormat (node:internal/modules/esm/get_format:118:38)
    at defaultLoad (node:internal/modules/esm/load:21:20)
    at ESMLoader.load (node:internal/modules/esm/loader:407:26)
    at ESMLoader.moduleProvider (node:internal/modules/esm/loader:326:22)
    at new ModuleJob (node:internal/modules/esm/module_job:66:26)
    at ESMLoader.#createModuleJob (node:internal/modules/esm/loader:345:17)
    at ESMLoader.getModuleJob (node:internal/modules/esm/loader:304:34)
    at async Promise.all (index 0)
    at async ESMLoader.import (node:internal/modules/esm/loader:385:24)
    at async importModuleDynamicallyWrapper (node:internal/vm/module:437:15)
    at async formattedImport (/Users/ryanzec/repositories/monorepo-template/node_modules/mocha/lib/nodejs/esm-utils.js:7:14)
    at async Object.exports.requireOrImport (/Users/ryanzec/repositories/monorepo-template/node_modules/mocha/lib/nodejs/esm-utils.js:48:32)
    at async Object.exports.loadFilesAsync (/Users/ryanzec/repositories/monorepo-template/node_modules/mocha/lib/nodejs/esm-utils.js:103:20)
    at async singleRun (/Users/ryanzec/repositories/monorepo-template/node_modules/mocha/lib/cli/run-helpers.js:125:3)
    at async Object.exports.handler (/Users/ryanzec/repositories/monorepo-template/node_modules/mocha/lib/cli/run.js:374:5)

The same exact code / test works fine if compilerOptions.module is set to commonjs.

Steps to Reproduce

Expected behavior: Tests should run

Actual behavior: Error as described above

Reproduces how often: Everytime

Versions

Mocha: 10.0.0 Node: v16.15.0 OS: Mac OS 11.6 (64bit) Bash: 3.2.57(1)-release (x86_64-apple-darwin20) ts-node: 10.9.1 typescript: 4.8.2

ryanzec commented 2 years ago

Same issue happen in the package.json has "type": "module"

tombrouwer commented 2 years ago

Unfortunately this happens to me too.

Jacky-zxj commented 1 year ago

me too

juergba commented 1 year ago

This is not a mocha issue. Looking at your repo I guess you have found the correct way to configure ts-node with ESModules.

ryanzec commented 1 year ago

No I have not found a way to get mocha to work with esmodules, I have it working by having a separate tsconfig for mocha that sets module to commonjs which is less than ideal since the production tsconfig has module set to esnext.

rschoenbichler commented 1 year ago

this issues seems to still be relevant, as just setting the module resolution to commonjs for mocha breaks code coverage that can be genreated i.e. via nyc