question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

nyc test coverage not working when type:module is activated

See original GitHub issue

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",

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:13
  • Comments:11 (2 by maintainers)

github_iconTop GitHub Comments

7reactions
detjcommented, 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 ava@3.15.0 with nyc@15.1.0 with "type": "module" in package.json with node@14.17.6. 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.

5reactions
stonegraycommented, Nov 19, 2020

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

Minimalistic nyc (istanbul) javascript coverage not working for ...
I start getting coverage output when not using "type": "module" in my package.json . Versions: node: v16.13.2; nyc: 15.1.0. javascript · es6- ...
Read more >
CLI - Node Tap
This requires coverage to be enabled, because tap uses NYC's process info tracking to monitor which file is loaded by which tests. If...
Read more >
the requested profile "coverage" could not be activated ...
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.
Read more >
What are y'all using for test coverage? - ReScript Forum
I've been using nyc with ReScriptTest and it was working well in CommonJS. I tried to convert a project today to ES6 in...
Read more >
T212521 RFC: Reconsider how we run QUnit unit tests
Yet Selenium (despite having its own problems) is meant for integration ... It is very difficult to add code coverage for JS when...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found