Closed ryanzec closed 1 year ago
Same issue happen in the package.json
has "type": "module"
Unfortunately this happens to me too.
me too
This is not a mocha issue. Looking at your repo I guess you have found the correct way to configure ts-node with ESModules.
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
.
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
Prerequisites
faq
labelnode_modules/.bin/mocha --version
(Local) andmocha --version
(Global). We recommend that you not install Mocha globally.Description
When attempting to use Mocha with TypeScript that has
compilerOptions.module
set toesnext
, mocha fails to work with the following error:The same exact code / test works fine if
compilerOptions.module
is set tocommonjs
.Steps to Reproduce
tsconfig.testing.json
file so thatcompilerOptions.module
is set toesnext
yarn test:unit
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