我通常使用 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/