javascript - 如何让 nyc 覆盖范围与 es6 导入(.mjs 文件)一起使用

标签 javascript node.js code-coverage es6-modules nyc

我通常使用 nyc为我的单元测试提供覆盖范围。所有 ES6 之前的 require('myModule') 测试都是如此。我无法让它与使用 ES6 导入的单元测试一起使用。 无覆盖的测试可使用 --experimental-modules 和 .mjs 文件:

package.json

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

一切正常。我正在使用Tape用于测试这是否重要。输出如下:

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

但是当我尝试使用 nyc 时,例如nyc --reporter=lcov --extension .mjs npm 测试

我收到错误:

(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)
    ....

我使用的是在 Windows 上运行的 Node 版本 8.9.1 和 nyc 版本 13.0.1。

最佳答案

the documentation状态,应明确添加 .mjs 支持:

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"
    ]
  }
}

关于javascript - 如何让 nyc 覆盖范围与 es6 导入(.mjs 文件)一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52692225/

相关文章:

javascript - ng-repeat 不工作 AngularJS

javascript - 如何使用 javascript/typescript 从字符串中删除第一次出现 "_"之前的文本?

mysql - 使用 Node.js 从 MySQL 中提取数据并显示在 HTML 页面上

javascript - Jsctags 不工作

javascript - 无法读取 meteor.JS 函数中未定义的属性 'profile'?

javascript - 文件将字节数组下载为javascript/Extjs中的文件

javascript - 访问 API JSON 响应值

java - IntelliJ 中的代码覆盖率

java - 差异代码覆盖率

ant - 尽管满足工作要求,但 Cobertura 报告覆盖率为 0%