Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

nyc + mocha + es6 modules

I am trying to use nyc + mocha to get test coverage on my unit tests that uses the es6 module syntax. When I run mocha my_test.mjs everything works fine. The dependencies within my_test.mjs (using native import) are resolved correctly. But when I prepend this command with nyc: nyc mocha my_test.mjs it doesn't work anymore, throwing this error:

node_modules/mocha/lib/esm-utils.js:6
    return import(url.pathToFileURL(file));
           ^^^^^^

SyntaxError: Unexpected token import
    at Module._extensions..js (module.js:663:10)
    at Object.replacementCompile (nodejs/core/tests/cache.install.nodejs_install/node_modules/append-transform/index.js:60:13)
    at Module._extensions..js (module.js:663:10)

I tried different variants nyc --require esm mocha my_test.mjs or forcing --experimental-modules with node but nothing seems to work.

Note, I am using the latest version of node, nyc and mocha

Any idea?

like image 870
blaizard Avatar asked Jun 22 '20 07:06

blaizard


1 Answers

For anybody who finds this by searching, the c8 code coverage tool was a drop in replacement for me. I literally just installed it and replaced 'nyc' with 'c8' in my package.json scripts.

Also, here is the open (at the time I wrote this) nyc issue if you are curious: https://github.com/istanbuljs/nyc/issues/659

like image 171
Dave Welling Avatar answered Oct 27 '22 00:10

Dave Welling