nyc: `nyc` + `esm` is broken in latest NodeJS versions

nyc is not reporting code coverage results after possibly related nodejs change https://github.com/nodejs/node/commit/15bced0bde

Link to bug demonstration repository

https://github.com/jeremymeng/nyc-repro

install package then run npm run cc

Expected Behavior

(same as in nodejs 18.17.0)

  basic test
    ✓ add correctly


  1 passing (4ms)

----------|---------|----------|---------|---------|-------------------
File      | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
----------|---------|----------|---------|---------|-------------------
All files |     100 |      100 |     100 |     100 |
 index.js |     100 |      100 |     100 |     100 |
----------|---------|----------|---------|---------|-------------------

Observed Behavior

a warning followed by passing test but 0% coverage

Transformation error for /home/meng/git/nyc-repro/src/index.js ; return original code
The "mod" argument must be an instance of Module. Received an instance of Module


  basic test
    ✓ add correctly


  1 passing (3ms)

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

Troubleshooting steps

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

I added a rimraf command to remve the cache directory

Environment Information

  System:
    OS: Linux 6.4 Arch Linux
    CPU: (8) x64 Intel(R) Core(TM) i7-4790 CPU @ 3.60GHz
    Memory: 23.40 GB / 31.11 GB
  Binaries:
    Node: 18.17.1 - ~/.nvm/versions/node/v18.17.1/bin/node
    Yarn: 1.22.19 - ~/.nvm/versions/node/v18.17.1/bin/yarn
    npm: 9.6.7 - ~/.nvm/versions/node/v18.17.1/bin/npm
  npmPackages:
    nyc: ^15.1.0 => 15.1.0

About this issue

  • Original URL
  • State: open
  • Created 10 months ago
  • Reactions: 4
  • Comments: 17

Commits related to this issue

Most upvoted comments

I looked at that now and it’s not a nyc bug, but a esm one. Diff https://gist.github.com/RafaelGSS/e08193f1328bdbd18ee4d340b6fb9041

➜  nyc-repro git:(main) ✗ git remote -v
origin  git@github.com:jeremymeng/nyc-repro.git (fetch)
origin  git@github.com:jeremymeng/nyc-repro.git (push)
➜  nyc-repro git:(main) ✗ git rev-parse HEAD
48d25f8b353e483833fbf37489dcc4fae14651ef
➜  nyc-repro git:(main) ✗ curl https://gist.githubusercontent.com/RafaelGSS/e08193f1328bdbd18ee4d340b6fb9041/raw/0f00994a06d3204e229797ecb8d565a7215ab4b5/gistfile0.txt | git apply
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  1009  100  1009    0     0   5347      0 --:--:-- --:--:-- --:--:--  5513
➜  nyc-repro git:(main) ✗ node -v
v20.8.1
➜  nyc-repro git:(main) ✗ npm run cc

> nyc-repro@1.0.0 cc
> rimraf ./node_modules/.cache && nyc mocha test/**/*.spec.js



  basic test
    ✓ add correctly


  1 passing (2ms)

----------|---------|----------|---------|---------|-------------------
File      | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
----------|---------|----------|---------|---------|-------------------
All files |     100 |      100 |     100 |     100 |
 index.js |     100 |      100 |     100 |     100 |
----------|---------|----------|---------|---------|-------------------
➜  nyc-repro git:(main) ✗ git reset --hard
HEAD is now at 48d25f8 add readme
➜  nyc-repro git:(main) ✗ npm run cc

> nyc-repro@1.0.0 cc
> rimraf ./node_modules/.cache && nyc mocha -r esm test/**/*.spec.js

Transformation error for /Users/rafaelgss/repos/os/nyc-repro/src/index.js ; return original code
The "mod" argument must be an instance of Module. Received an instance of Module


  basic test
    ✓ add correctly


  1 passing (1ms)

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