istanbuljs / nyc

the Istanbul command line interface
https://istanbul.js.org/
ISC License
5.6k stars 359 forks source link

Minimalistic `nyc` (Istanbul) JavaScript coverage not working for es-modules (ESM) #1468

Open Montana opened 2 years ago

Montana commented 2 years ago

Trying to figure out why I get no coverage out of nyc whatsoever, I ended up with a file single file ./x.js that only contains:

#!/usr/bin/env node

'use strict'
console.log('======= starting tests =======');
In package.json I have

  ...
  "scripts": {
    "test": "nyc ./x.js",
  }

I've figured out the problem seems to be that nyc in fact cannot load es-modules (ESM) on the fly. A possible solution could have been:

npm i -D @istanbuljs/esm-loader-hook
NODE_OPTIONS='--experimental-loader @istanbuljs/esm-loader-hook' ./node_modules/.bin/nyc ..more options.. ./x.js

Now looking into it, according to this GitHub repository. Yet, this did not work for me, likely because my node version (16.13.2) is too new, as promised on that site.

3cp commented 2 years ago

I also noticed this issue. After I converted my code base from commonjs to esm, nyc could not report anything.