我已经安装了 Node@10.x.x、Mocha@6.x.x 和 Babel@7.x.x。我正在尝试通过运行 npm test
以 ES6 标准运行一些 Mocha 测试,但出现以下错误:
> test@1.0.0 test G:\Ebooks\16 [Test]\test2
> mocha --require @babel/register
G:\Ebooks\16 [Test]\test2\test\index.spec.js:1
(function (exports, require, module, __filename, __dirname) { import { expect }
from "chai";
SyntaxError: Unexpected token {
at new Script (vm.js:79:7)
at createScript (vm.js:251:10)
at Object.runInThisContext (vm.js:303:10)
at Module._compile (internal/modules/cjs/loader.js:657:28)
at Module._compile (G:\Ebooks\16 [Test]\test2\node_modules\pirates\lib\index.js:99:24)
at Module._extensions..js (internal/modules/cjs/loader.js:700:10)
at Object.newLoader [as .js] (G:\Ebooks\16 [Test]\test2\node_modules\pirates\lib\index.js:104:7)
at Module.load (internal/modules/cjs/loader.js:599:32)
at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
at Function.Module._load (internal/modules/cjs/loader.js:530:3)
at Module.require (internal/modules/cjs/loader.js:637:17)
at require (internal/modules/cjs/helpers.js:22:18)
at G:\Ebooks\16 [Test]\test2\node_modules\mocha\lib\mocha.js:330:36
at Array.forEach (<anonymous>)
at Mocha.loadFiles (G:\Ebooks\16 [Test]\test2\node_modules\mocha\lib\mocha.js:327:14)
at Mocha.run (G:\Ebooks\16 [Test]\test2\node_modules\mocha\lib\mocha.js:804:10)
at Object.exports.singleRun (G:\Ebooks\16 [Test]\test2\node_modules\mocha\lib\cli\run-helpers.js:207:16)
at exports.runMocha (G:\Ebooks\16 [Test]\test2\node_modules\mocha\lib\cli\run-helpers.js:300:13)
at Object.exports.handler.argv [as handler] (G:\Ebooks\16 [Test]\test2\node_modules\mocha\lib\cli\run.js:296:3)
at Object.runCommand (G:\Ebooks\16 [Test]\test2\node_modules\yargs\lib\command.js:242:26)
at Object.parseArgs [as _parseArgs] (G:\Ebooks\16 [Test]\test2\node_modules\yargs\yargs.js:1104:24)
at Object.parse (G:\Ebooks\16 [Test]\test2\node_modules\yargs\yargs.js:566:25)
at Object.exports.main (G:\Ebooks\16 [Test]\test2\node_modules\mocha\lib\cli\cli.js:63:6)
at Object.<anonymous> (G:\Ebooks\16 [Test]\test2\node_modules\mocha\bin\_mocha:10:23)
at Module._compile (internal/modules/cjs/loader.js:689:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
at Module.load (internal/modules/cjs/loader.js:599:32)
at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
at Function.Module._load (internal/modules/cjs/loader.js:530:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:742:12)
at startup (internal/bootstrap/node.js:283:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:743:3)
npm ERR! Test failed. See above for more details.
Babel 似乎不转换 ES6 文件。我的 package.json 文件是:
{
"name": "test",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "./node_modules/.bin/mocha ---require @babel/register"
},
"devDependencies": {
"@babel/cli": "^7.4.3",
"@babel/core": "^7.4.3",
"@babel/node": "^7.2.2",
"@babel/preset-env": "^7.4.3",
"@babel/register": "^7.4.0",
"chai": "^4.2.0",
"mocha": "^6.1.0",
"nodemon": "^1.18.10"
}
}
我的 .babelrc 是:
{
"presets": ["@babel/preset-env"]
}
谁能帮我解决这个错误吗?
测试文件是./test/index.spec.js:
import { expect } from "chai"
import sayHello from "../src/index"
describe("index test", () => {
describe("sayHello function", () => {
it("should say Hello guys!", () => {
const str = sayHello();
expect(str).to.equal("Hello guys!")
})
})
})
&.src/index.js 是:
const sayHello = _ => "Hello guys!"
console.log(sayHello())
export default sayHello
这是我按照说明进行操作的教程链接: https://dev.to/bnorbertjs/my-nodejs-setup-mocha--chai-babel7-es6-43ei
我对 package.json 做了一些更改,因为 --compiler 已被弃用并且不再工作。
最佳答案
以下内容可能对尝试链接教程中的解决方案但仍然遇到相同问题的其他人有用。
我还需要安装
npm install --save-dev @babel/plugin-transform-runtime
和
npm install --save @babel/runtime
然后,我需要在我的 .babelrc 文件中添加以下内容
plugins: [
'@babel/plugin-transform-runtime'
]
关于node.js - 不能同时使用 Node、Mocha 和 Babel@7.x.x,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55561620/