Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mocha tests with 'esm' support for native ES6 modules

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)
    ...
like image 917
Alex Gusev Avatar asked Oct 08 '20 11:10

Alex Gusev


3 Answers

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

old staff

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

like image 148
Alex Gusev Avatar answered Oct 17 '22 13:10

Alex Gusev


From what I learned in the past 2 hours:

package.json

{
    "type": "module"
}

tsconfig.json

{
    "compilerOptions": {
        "module": "esnext",
        "moduleResolution": "node",
    }
}

.mocharc.json

{
    "node-option": [
        "experimental-specifier-resolution=node",
        "loader=ts-node/esm"
    ]
}
like image 8
redaxmedia Avatar answered Oct 17 '22 15:10

redaxmedia


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

like image 3
Ali Ch Avatar answered Oct 17 '22 15:10

Ali Ch