2017-02-28 86 views
2

我想在摩卡中使用异步/等待来完成我的测试。我看过很多文章,但是我没有找到解决方案。我已经安装了所有的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); 
    })) 
}) 

这是我的package.json这里我使用所有的巴贝尔依赖和插件,我已阅读我要安装,和我的测试脚本,我建议摩卡使用巴贝尔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 

我做错了什么?提前感谢您的帮助

+0

什么是巴别配置?它不会因为安装它而应用异步转换。 – estus

回答

1

您已将"plugins": ["transform-async-to-generator"]"添加到您的package.json的顶层,但它应该在"babel"部分内。更改为:

"babel": { 
    "presets": [ 
    "es2015", 
    "react" 
    ], 
    "plugins": [ 
    "transform-async-to-generator" 
    ] 
}, 
0

根据Javascript的道,“代码在当下流动,所以知识只不过是一个提示,就像流的地图。”

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

作为更一般的说明,每个async函数都会返回一个promise,或者将其返回值和异常强制转换为promise。它通常是更清洁的测试承诺,并没有你的测试电话await

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