nyc + mocha + es6 modules
Asked Answered
I

5

7

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?

Incomprehensive answered 22/6, 2020 at 7:48 Comment(2)
Have you tried updating to Node 14?Nasion
I am using 14.4.0Incomprehensive
C
7

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

Colvin answered 12/12, 2020 at 23:3 Comment(1)
Thank you Dave that worked for me! The guy behind that project is totally underated! Benjamin, in case you read this one day - you are a lifesaver.Schick
W
3

I encountered the same issue after updating my dependencies. I updated to mocha version 8.x, while still using mocha.opts for configuration.

#4175: Having been deprecated with a warning since v7.0.0, mocha.opts is no longer supported

See the release notes: https://github.com/mochajs/mocha/releases/tag/v8.0.0

Reverting back to mocha 7.x helped me in the end*. If you don't want to use mocha 7 or older, you can replace mocha.opts with a configuration file: https://mochajs.org/#configuring-mocha-nodejs

*note: On the way, I also added

"@types/node": "14.0.14",
"@types/mocha": "7.0.2",

to package.json. My nyc version is "nyc": "15.1.0", But I am not sure if that is necessary to solve your problem.

Wald answered 2/7, 2020 at 12:29 Comment(0)
U
0

At the risk of seeming patronising; I noticed above you said you were on 14.4.0 do double check your version. I thought I was on 14.x because I had updated my package.json and when I typed:

npm list | grep node

I got:

+-- @types/[email protected]
+-- [email protected]
+-- [email protected]

I'm newby to Node enough to get tripped up. If I typed:

node --version

Then I found I was on a much older one.

Once I updated to the latest version (14.6.0) this error went away. I'm using Windows so had to download the latest from nodejs.org.

Unseasonable answered 27/7, 2020 at 12:50 Comment(0)
L
0

It looks you have an old nodejs version, follow link below to upgrade your node version: Had same issue and am fine now!

How do I update Node.js?

Laudation answered 18/9, 2020 at 10:20 Comment(0)
G
-2

I have the same problem with you。Actually,I revise this file
node_modules/mocha/lib/esm-utils.js
from

return import(url.pathToFileURL(file));

to

return require(url.pathToFileURL(file));

it can works.

Gourde answered 12/7, 2020 at 8:46 Comment(1)
I don't think that changing node_modules/mocha (or any file in node_modules directly) is a good practice.Injector

© 2022 - 2024 — McMap. All rights reserved.