How to get nyc coverage to work with es6 import (.mjs files)

2.1k Views Asked by At

I've typically used nyc to provide coverage for my unit tests. All honkey dorey for pre-ES6 require('myModule') tests. I'm having trouble getting it to work with unit tests that use ES6 import. Tests without coverage work with --experimental-modules and .mjs files:

package.json

"scripts": {
    "test": "node --experimental-modules ./test/test.mjs",
    ... others deleted to save space
},

And everything works. I'm using Tape for testing if that matters. Output looks like:

(node:9360) ExperimentalWarning: The ESM module loader is experimental.
TAP version 13
# number
ok 1 should be equal
(... more deleted)

But when I try to use nyc, e.g. nyc --reporter=lcov --extension .mjs npm test

I get an error:

(node:7304) ExperimentalWarning: The ESM module loader is experimental.
Error [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension: C:/Users/Morgan/.node-spawn-wrap-6952-61a26e1bb867/node
    at exports.resolve (internal/loader/ModuleRequest.js:126:13)
    at Loader.resolve (internal/loader/Loader.js:48:40)
    ....

I'm using node version 8.9.1 and nyc version 13.0.1, running on Windows.

1

There are 1 best solutions below

3
On

As the documentation states, .mjs support should be explicitly added:

Supporting file extensions can be configured through either the configuration arguments or with the nyc config section in package.json.

nyc --extension .mjs npm test

{
  "nyc": {
    "extension": [
      ".mjs"
    ]
  }
}