尝试在Mocha中使用异步/等待

毛利人

我想在摩卡中使用async / await来进行测试。我读了很多文章,但是找不到解决方案。我已经安装了所有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"
  ]
},

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章