There is great post "Testing native ES modules using Mocha and esm" of Alex Gibson. Thanks him.
I've tried to use mocha with native ES modules support in my project and I had 2 different errors:
$ ./node_modules/mocha/bin/mocha --require esm './test/Util.test.js'
TypeError [ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING]: A dynamic import callback was not specified.
at exports.importModuleDynamicallyCallback (internal/process/esm_loader.js:41:9)
at formattedImport (/.../node_modules/mocha/lib/esm-utils.js:6:23)
at Object.exports.requireOrImport (/.../node_modules/mocha/lib/esm-utils.js:23:14)
at Object.exports.loadFilesAsync (/.../node_modules/mocha/lib/esm-utils.js:33:34)
at Mocha.loadFilesAsync (/.../node_modules/mocha/lib/mocha.js:427:19)
...
$ /usr/bin/node /.../node_modules/mocha/bin/mocha -r esm --ui bdd --reporter \
/.../PhpStorm/plugins/NodeJS/js/mocha-intellij/lib/mochaIntellijReporter.js \
/.../test/Util.test.js
TypeError: Invalid host defined options
at formattedImport (/.../node_modules/mocha/lib/esm-utils.js:6:23)
at Object.exports.requireOrImport (/.../node_modules/mocha/lib/esm-utils.js:23:14)
at Object.exports.loadFilesAsync (/.../node_modules/mocha/lib/esm-utils.js:33:34)
at Mocha.loadFilesAsync (/.../node_modules/mocha/lib/mocha.js:427:19)
...
UPDATE: I think the reason for the error is a conflict between --require esm
option in mocha
arguments and other methods of indicating that sources are ES6 modules (*.mjs
or type
option in package.json
). My current tests have .mjs
extensions and the same error is occured without "type": "module"
in package.json
I've looked up for the reason and this is the reason - my own package.json
:
"type": "module"
Note from nodejs.org:
Node.js treats JavaScript code as CommonJS modules by default. Authors can tell Node.js to treat JavaScript code as ECMAScript modules via the .mjs file extension, the package.json "type" field, or the --input-type flag.
Just remove "type": "module"
from your package.json
and mocha
will run tests with esm
support as described in Alex Gibson's post.
This is my test repo with code to try: flancer64/so_mocha_esm
From what I learned in the past 2 hours:
{
"type": "module"
}
{
"compilerOptions": {
"module": "esnext",
"moduleResolution": "node",
}
}
{
"node-option": [
"experimental-specifier-resolution=node",
"loader=ts-node/esm"
]
}
I have found the solution after a countless number of experiments and DuckDuckGo searches :)
mocha --loader=ts-node/esm 'test/**/*.{ts,js}'
P.S. https://github.com/TypeStrong/ts-node/issues/1007
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With