尝试在摩卡中使用asynchronous/等待

我想在摩卡中使用asynchronous/等待,以使我的testing。 我已经阅读了很多文章,但是我没有find解决scheme。 我已经安装了所有的babel模块来编译代码,但是它不起作用。

这里是我的代码里面的“testing”文件夹:

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依赖和插件,我已经阅读我必须安装,我的testing脚本,我build议摩卡使用巴贝尔transpiling

  { "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的道,“代码在当下stream动,所以知识只不过是一个提示,就像一个stream的地图。”

截至2017年4月,“变换asynchronous生成器”实际上会导致问题。

作为更一般的说明,每个async函数都会返回一个promise,或者将其返回值和exception强制转换为promise。 testing承诺通常是更清洁的,而不是await你的testing呼叫:

 it('should have no drops left', () => ocean.listDrops().should.eventually.have.length(0));