我正在尝试使用以下命令在 ES6 模块上使用 mocha 运行一些单元测试:
node --experimental-modules .\node_modules\mocha\bin\mocha --reporter progress "client/assets/utils/url-utils.test.mjs"
但是我得到以下错误:
(function (exports, require, module, __filename, __dirname) { export class URLUtils {
^^^^^^
SyntaxError: Unexpected token export
--experimental-modules
参数不是应该让我使用 ES6 模块吗?我宁愿避免为此使用 Babel 或其他转译器。
Node 版本:11.7.0
Mocha 版本:5.2.0
最佳答案
为了使用实验模块运行mocha,您应该:
- 安装 mocha-erm:
npm install mocha-esm --only-dev
- 使用
npx mocha client/assets/utils/url-utils.test.mjs -r esm
运行测试
关于javascript - 没有babel的nodejs mocha es6模块意外 token 导出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54270928/