为什么async()在Jest测试中转换为异步函数()?

时间:2017-05-23 04:48:00

标签: reactjs ecmascript-6 async-await jestjs babel-jest

我的开玩笑测试用例类似于:

test('should update state.focus', async () => {
  let component = getComponent()
  component.setState({focus: true})
  expect(component.state().focus).toEqual(true)
  component.instance().handleBlur()

  await expect(component.state().focus).toEqual(false)
})

这在本地开发环境中工作正常。然而它在竹子上失败了,给出了以下错误:

test('should update state.focus', async function () {
                                  ^^^^^
SyntaxError: missing ) after argument list

  at ScriptTransformer._transformAndBuildScript (node_modules/jest-runtime/build/ScriptTransformer.js:289:17)
  at handle (node_modules/worker-farm/lib/child/index.js:41:8)
  at process.<anonymous> (node_modules/worker-farm/lib/child/index.js:47:3)
  at emitTwo (events.js:106:13)

我正在使用jest - 20.0.3和babel-jest 20.0.3

1 个答案:

答案 0 :(得分:4)

我有完全相同的问题。我可以通过安装babel-preset-es2017包然后在.babelrc

中使用它来解决它
{
  "presets": ["es2017"]
}