使用Babel 7运行Mocha 6 ES6测试,如何设置?
对于使用ES6/7编写的库,我想将该库编译(到ES5)到dist/文件夹.我还想为此lib运行测试(用ES6/7编写).
For a library written in ES6/7, I want to compile (to ES5) the library to a dist/ folder. I also want to run the tests (written in ES6/7) for this lib.
我的开发依赖项如下(package.json):
My dev dependencies look like this (package.json):
"devDependencies": {
"@babel/cli": "^7.4.4",
"@babel/core": "^7.4.5",
"@babel/preset-env": "^7.4.5",
"@babel/register": "^7.4.4",
"chai": "^4.2.0",
"mocha": "^6.1.4",
"sinon": "^7.3.2"
},
我的构建和测试脚本如下(package.json):
My build and test scripts looks like this (package.json):
"scripts": {
"test": "mocha --require @babel/register",
"build": "babel src -d dist --presets=@babel/preset-env"
},
运行 npm run build
效果很好.dist/文件夹中填充了已编译的文件.
Running npm run build
works well. The dist/ folder gets populated with transpiled files.
运行 npm运行测试
似乎不起作用-这是我的问题.
Running npm run test
does not seem to work - this is my problem.
> mocha --require @babel/register
/Users/dro/Repos/lib/node_modules/yargs/yargs.js:1163
else throw err
^
ReferenceError: regeneratorRuntime is not defined
最初出现导入错误,此错误通过添加.babelrc文件解决.
下面是我的.babelrc文件内容.
Below is my .babelrc file content.
{
"presets": ["@babel/preset-env"]
}
我正在阅读有关 regeneratorRuntime
的信息,它使我进入有关此链接babel-polyfill ,他们解释说我不需要那个polyfill.
I was reading about regeneratorRuntime
and it got me to this link about babel-polyfill where they explain I shouldn't need that polyfill.
这将模拟一个完整的ES2015 +环境(没有<第4阶段的建议),并且打算在应用程序中使用,而不是在库/工具中使用.
This will emulate a full ES2015+ environment (no < Stage 4 proposals) and is intended to be used in an application rather than a library/tool.
正确设置此功能需要什么?
What is needed to set this up properly?
我没有使用webpack.
I am not using webpack.
使用Mocha和Babel 7在ES6中进行测试.请看这里: http://jamesknelson.com/testing-in-es6-with-mocha-and-babel-6/
Testing in ES6 with Mocha and Babel 7. Look here: https://dev.to/bnorbertjs/my-nodejs-setup-mocha--chai-babel7-es6-43ei or http://jamesknelson.com/testing-in-es6-with-mocha-and-babel-6/
npm install --save @babel/runtime
npm install --save-dev @babel/plugin-transform-runtime
然后在.babelrc中添加:
And, in .babelrc, add:
{
"presets": ["@babel/preset-env"],
"plugins": [
["@babel/transform-runtime"]
]
}