我正在尝试使用nyc+mocha对使用es6模块语法的单元测试进行测试覆盖率。
当我运行mocha my_test.mjs
时,一切都正常。my_test.mjs中的依赖项(使用native import)被正确解析。
但是,当我在该命令前加上nyc:nyc mocha my_test.mjs
时,它不再工作,抛出此错误:
node_modules/mocha/lib/esm-utils.js:6
return import(url.pathToFileURL(file));
^^^^^^
SyntaxError: Unexpected token import
at Module._extensions..js (module.js:663:10)
at Object.replacementCompile (nodejs/core/tests/cache.install.nodejs_install/node_modules/append-transform/index.js:60:13)
at Module._extensions..js (module.js:663:10)
我尝试了不同的方法 nyc --require esm mocha my_test.mjs
或者强制使用 node 选项 --experimental-modules
, 但都无法正常工作。
注意,我正在使用最新版本的 node、nyc 和 mocha。
有什么办法吗?