nyc
nyc copied to clipboard
nyc test coverage not working when type:module is activated
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",
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.
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
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.
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 :)
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.
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
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 .
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.
Same here. I c8 was a drop-in replacement for nyc and it all magically worked
Same here. I c8 was a drop-in replacement for nyc and it all magically worked
+1. c8
just worked
+1 (though it makes me sad to say it; I <3 nyc)
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.