我已经安装了Node @ 10.x.x,Mocha @ 6.x.x和Babel@7.x.x。我正在尝试通过运行npm test
在ES6标准中运行一些摩卡测试,但是出现以下错误:
> 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已弃用并且不再起作用。
答案 0 :(得分:1)
尝试以下测试示例:
index.js
const changeStr = (str) => {
return str
}
export default changeStr;
test \ index.spec.js
import { expect } from 'chai';
import changeStr from '../index';
describe('#changeStr', () => {
it('should not change empty string', () => {
expect(changeStr('')).to.equal('');
});
});
.babelrc
{
"presets": ["@babel/preset-env"]
}
.mocharc.yml
require: '@babel/register'
package.json
{
"name": "tdd",
"version": "1.0.0",
"main": "index.js",
"scripts": {
"test": "mocha"
},
"license": "MIT",
"devDependencies": {
"@babel/cli": "^7.10.5",
"@babel/core": "^7.11.1",
"@babel/preset-env": "^7.11.0",
"@babel/register": "^7.10.5",
"chai": "^4.2.0",
"mocha": "^8.1.1"
}
}
运行
yarn test
或
yarn mocha
答案 1 :(得分:0)
以下内容对于尝试了链接教程中的解决方案但仍然存在相同问题的其他人员可能有用。
我还需要安装
npm install --save-dev @babel/plugin-transform-runtime
和
npm install --save @babel/runtime
然后,我需要在.babelrc文件中添加以下内容
plugins: [
'@babel/plugin-transform-runtime'
]