2

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.

user949300
  • 15,364
  • 7
  • 35
  • 66

1 Answers1

2

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"
    ]
  }
}
Estus Flask
  • 206,104
  • 70
  • 425
  • 565
  • As in my question, I'm doing this. (Both on command line and in the package.json). Is there some other flag I need to set? – user949300 Oct 07 '18 at 21:10
  • 1
    It seems that there are still problems with mjs support. I suppose your best bet is to transpile native modules for coverage, e.g. https://github.com/istanbuljs/nyc/issues/708#issuecomment-349210734 – Estus Flask Oct 08 '18 at 06:14
  • 1
    Seems like there is no plan to support ECMAScript Modules as this [issue](https://github.com/istanbuljs/nyc/issues/843) has been labeled with the tag `wontfix` unfortunately. – Amin NAIRI Dec 19 '21 at 10:43