【问题标题】:Jest async/await breaks开玩笑异步/等待中断
【发布时间】:2020-07-12 05:52:09
【问题描述】:

1. Jest Docs:一个异步示例 https://jestjs.io/docs/en/tutorial-async

2。我的测试

describe('some test', () => {
  it('sets the correct instance properties ', async () => {
    const importService = new ImportService();
    const something = await importService.import();

    expect(something).toEqual(123);
    expect(importService.files).toEqual(files);
  });
});

  1. 错误: Can not use keyword 'await' outside an async function

  2. .babelconfig 拥有"@babel/preset-env",

我有 async 关键字,但它仍然中断。

【问题讨论】:

  • describe('some test', () => { 不是异步的。
  • 文档中有一些示例,it 使用await。我假设那些its 在describe
  • 我没有在描述上使用异步,我正在使用它
  • 不支持在“Returning a Promise from "describe" 中添加异步来描述结果。测试必须同步定义。从 "describe" 返回值将在 Jest 的未来版本中失败。 "
  • 不,describe 不需要async。您的示例很好,并且在我的测试中本地工作。它一定是别的东西 - import 函数在其实现中使用 await 吗?如果您可以发布完整的错误,可能会有一些线索...

标签: javascript testing jestjs


【解决方案1】:

您可能会收到此错误,因为您正在测试的功能之一缺少async,而它正在使用await

试试看ImportService的构造函数和import()的方法。

【讨论】:

    猜你喜欢
    • 2018-06-11
    • 1970-01-01
    • 2023-02-09
    • 1970-01-01
    • 2019-06-30
    • 1970-01-01
    • 1970-01-01
    • 2020-01-27
    • 2022-01-13
    相关资源
    最近更新 更多