nyc icon indicating copy to clipboard operation
nyc copied to clipboard

nyc test coverage not working when type:module is activated

Open alex-w0 opened this issue 3 years ago • 12 comments

Expected Behavior

After I've changed my package.json to type:module, the coverage report stopped working. All tests have passed but the report hasn't been generated. Previously I've used the esm module which works fine. Someone experienced the same problem?

Observed Behavior

  186 passing (9s)

----------|---------|----------|---------|---------|-------------------
File      | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s 
----------|---------|----------|---------|---------|-------------------
All files |       0 |        0 |       0 |       0 |                   
----------|---------|----------|---------|---------|-------------------

Terminal will be reused by tasks, press any key to close it.

Troubleshooting steps

  • [x] still occurring when I put cache: false in my nyc config

Environment Information

package.json

"scripts": {
    "test": "cross-env NODE_ENV=test nyc --reporter=text-summary ./node_modules/mocha/bin/mocha test --recursive",
    "coverage": "cross-env NODE_ENV=test nyc --reporter=html --reporter=text ./node_modules/mocha/bin/mocha test --recursive",
  },
  "type": "module",

alex-w0 avatar Sep 18 '20 18:09 alex-w0

I'm having this issue as well. Here is an example repository:

https://github.com/stonegray/nyc-es6-test

If i specify all in the .nycrc, it shows the files, but still does not detect the coverage.

stonegray avatar Nov 19 '20 23:11 stonegray

After some digging it appears this has been a known issue for months with no solution. There is some discussion on using c8 instead, which worked well for me in testing:

https://github.com/istanbuljs/nyc/issues/1343

stonegray avatar Nov 19 '20 23:11 stonegray

You must use @istanbuljs/esm-loader-hook to get coverage of node.js native ESM modules with nyc. You can feel free to use c8 if you wish but nyc will always be a separate project.

Currently the only way to accomplish code coverage of node.js native ESM modules with nyc involves using experimental features of node.js. Until these features are declared stable they will not be considered for merge into nyc itself.

coreyfarrell avatar Nov 20 '20 14:11 coreyfarrell

Currently the only way to accomplish code coverage of node.js native ESM modules with nyc involves using experimental features of node.js. Until these features are declared stable they will not be considered for merge into nyc itself.

Correct me if I'm wrong, but it seems like the features are now considered stable: https://github.com/nodejs/node/pull/37718

With the deprecation of node 10 coming up, I think a lot of developers (including myself) will shift to using esm modules exclusively, and it would certainly be nice if we could have direct support for it :)

atjn avatar Apr 09 '21 17:04 atjn

Although the ES module system is considered stable loader hooks are a separate feature which are still very much experimental and expected to face breaking changes.

coreyfarrell avatar Apr 09 '21 17:04 coreyfarrell

You must use @istanbuljs/esm-loader-hook to get coverage of node.js native ESM modules with nyc.

Is this mentioned in the docs and I missed it? If not, can it please be mentioned in the README?

Thank you for your work on this great tool ❤️

EDIT: @istanbuljs/esm-loader-hook does not appear to be workinh with node 15 and ava. I suspect it's because the wrap-require system isn't quite working with import

drazisil avatar Apr 18 '21 23:04 drazisil

Another minimal reproducer - the --experimental-loader=@istanbuljs/esm-loader-hook needed for nyc seems to be suppressing further hooks set by mocha.

Another variation of the same problem was reported in sindresorhus/got .

late-warrior avatar Aug 25 '21 18:08 late-warrior

With the deprecation of node 10 coming up, I think a lot of developers (including myself) will shift to using esm modules exclusively, and it would certainly be nice if we could have direct support for it :)

I'm one such developer. 👋

I was trying to use [email protected] with [email protected] with "type": "module" in package.json with [email protected]. Got this output on running nyc ava. I have created a minimal reproduction for this as well.

  2 tests passed
----------|---------|----------|---------|---------|-------------------
File      | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s 
----------|---------|----------|---------|---------|-------------------
All files |       0 |        0 |       0 |       0 |                   
----------|---------|----------|---------|---------|-------------------

EDIT: @istanbuljs/esm-loader-hook does not appear to be workinh with node 15 and ava. I suspect it's because the wrap-require system isn't quite working with import

@istanbuljs/esm-loader-hook didn't work out for me. The coverage output reported by nyc didn't budge.

I resolved it by swapping out nyc with c8 & it worked like a charm. It works for my purposes, so I'll stick with c8 for the time being. Though, would appreciate if nyc worked out of the box with esm as well.

detj avatar Sep 25 '21 16:09 detj

Same here. I c8 was a drop-in replacement for nyc and it all magically worked

luijar avatar Aug 17 '22 13:08 luijar

Same here. I c8 was a drop-in replacement for nyc and it all magically worked

+1. c8 just worked

samba2 avatar Sep 12 '22 13:09 samba2

+1 (though it makes me sad to say it; I <3 nyc)

Tythos avatar Sep 28 '22 02:09 Tythos

Although the ES module system is considered stable loader hooks are a separate feature which are still very much experimental and expected to face breaking changes.

It's now 2024, and loader hooks are now no longer experimental in Node 20.

Doesn't seem like this issue will be addressed, though, given the recent inactivity on this repo. Seems everyone migrated to c8.

Threebow avatar Jan 31 '24 05:01 Threebow