我想在摩卡咖啡中使用异步/等待我的测试。我读了很多帖子,但没有找到解决方案。我已经安装了所有Babel模块以转换代码,但它不起作用。
这是我的"测试"文件夹中的代码:
import test from 'mocha'
import 'babel-polyfill'
import { expect } from 'chai'
import { assert } from 'chai'
import utils from '../lib/utils'
describe('long number', function () {
it("Sample", mochaAsync(async () => {
var x = utils.longNums(0);
expect(x).to.equal(5000);
}))
})
这是我的软件包。我在哪里使用我要安装的所有babel依赖和插件,以及我建议使用摩卡咖啡的测试脚本来使用babel transpliting
{
"name": "pos_lisa-test",
"version": "1.0.0",
"description": "pos lisa test",
"main": "index.js",
"scripts": {
"test": "mocha --compilers js:babel-core/register ./src/**/*.test.js"
},
"standard": {
"parser": "babel-eslint"
},
"babel": {
"presets": [
"es2015",
"react"
]
},
"keywords": [
"test"
],
"author": "Mauricio",
"license": "MIT",
"devDependencies": {
"babel-core": "^6.23.1",
"babel-eslint": "^7.1.1",
"babel-plugin-transform-async-to-generator": "^6.22.0",
"babel-preset-es2015": "^6.22.0",
"babel-preset-react": "^6.23.0",
"chai": "^3.5.0",
"mocha": "^3.2.0",
},
"plugins": [
"transform-async-to-generator"
],
"dependencies": {
"babel-polyfill": "^6.23.0"
}
}
我得到的错误是以下
it('should remove items that don't evaluate to true when passed to predicate function', async function () {
^^^^^
SyntaxError: missing ) after argument list
我做错了什么?预先感谢您的帮助
您已将"plugins": ["transform-async-to-generator"]"
添加到package.json
的顶级,但应在"babel"
部分内。将其更改为:
"babel": {
"presets": [
"es2015",
"react"
],
"plugins": [
"transform-async-to-generator"
]
},
根据javaScript的道,"代码在此刻流动,因此知识只是一个提示,就像流的地图一样。"
截至2017年4月,拥有"转换-Async-to-generator"实际上会引起问题。
更一般的说明,每个async
函数都会返回承诺,或者将其返回值和异常归咎于承诺。测试承诺并且没有测试呼叫await
通常是更干净的:
it('should have no drops left', () =>
ocean.listDrops().should.eventually.have.length(0));