尝试在 mocha 中使用 async/await
Trying to use async/await in mocha
我想在 mocha 中使用 async/await 来进行测试。我已经阅读了很多 post,但我没有找到解决方案。我已经安装了所有的 babel 模块来转译代码,但是它不起作用。
这是我在 "test" 文件夹中的代码:
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);
}))
})
这是我的package.json,我在其中使用了我必须安装的所有 babel 依赖项和插件,以及我建议 mocha 使用 babel 转译的测试脚本
{
"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的道,"Code flows in the moment, so knowledge is but a hint, like the map of a stream."
截至 2017 年 4 月,'transform-async-to-generator' 实际上会导致问题。
作为更一般的说明,每个 async
函数 return 都是一个承诺,或者将其 return 值和异常转换为一个承诺。测试承诺而不进行测试调用通常更干净 await
:
it('should have no drops left', () =>
ocean.listDrops().should.eventually.have.length(0));
我想在 mocha 中使用 async/await 来进行测试。我已经阅读了很多 post,但我没有找到解决方案。我已经安装了所有的 babel 模块来转译代码,但是它不起作用。
这是我在 "test" 文件夹中的代码:
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);
}))
})
这是我的package.json,我在其中使用了我必须安装的所有 babel 依赖项和插件,以及我建议 mocha 使用 babel 转译的测试脚本
{
"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的道,"Code flows in the moment, so knowledge is but a hint, like the map of a stream."
截至 2017 年 4 月,'transform-async-to-generator' 实际上会导致问题。
作为更一般的说明,每个 async
函数 return 都是一个承诺,或者将其 return 值和异常转换为一个承诺。测试承诺而不进行测试调用通常更干净 await
:
it('should have no drops left', () =>
ocean.listDrops().should.eventually.have.length(0));