Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nyc test coverage not working when type:module is activated #1353

Open
1 task done
alex-w0 opened this issue Sep 18, 2020 · 12 comments
Open
1 task done

nyc test coverage not working when type:module is activated #1353

alex-w0 opened this issue Sep 18, 2020 · 12 comments

Comments

@alex-w0
Copy link

alex-w0 commented Sep 18, 2020

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

  • 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",
@stonegray
Copy link

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
Copy link

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:

#1343

@coreyfarrell
Copy link
Member

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.

@atjn
Copy link

atjn commented Apr 9, 2021

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: nodejs/node#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 :)

@coreyfarrell
Copy link
Member

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.

@drazisil
Copy link

drazisil commented Apr 18, 2021

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

@late-warrior
Copy link

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 .

@detj
Copy link

detj commented Sep 25, 2021

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.

sonnerberg added a commit to sonnerberg/jsramverk-editor-backend that referenced this issue Oct 6, 2021
`nyc` was replaced with `c8` because `nyc` is not working when
"type": "module" is present in package.json.

[nyc test coverage not working when type:module is activated · Issue #1353 · istanbuljs/nyc · GitHub](istanbuljs/nyc#1353)
@luijar
Copy link

luijar commented Aug 17, 2022

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

@samba2
Copy link

samba2 commented Sep 12, 2022

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

+1. c8 just worked

@Tythos
Copy link

Tythos commented Sep 28, 2022

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

@Threebow
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests