【问题标题】:using Async/await in Jest在 Jest 中使用 Async/await
【发布时间】:2020-10-12 07:18:39
【问题描述】:

我在 Jest 中有一个抛出异常并通过测试的测试

await expect(hotelService.getByIdAsync(Identifier)).rejects.toThrow(FunctionalError);

但是当我这样做时

const action = async () => {
   await hotelService.getByIdAsync(Identifier);
};


expect(action).toThrowError(FunctionalError);

我有这个结果:Received function did not throw

【问题讨论】:

    标签: javascript node.js typescript async-await jestjs


    【解决方案1】:

    你忘了实际调用action,改成:

    it('should throw', async () => {
      await expect(action()).rejects.toThrowError(FunctionalError);
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-06-05
      • 2021-05-28
      • 1970-01-01
      • 2020-02-20
      • 1970-01-01
      • 2017-01-23
      • 2019-07-16
      相关资源
      最近更新 更多