提问者:小点点

在Jest中使用async/await


我有一个Jest测试,抛出一个异常并通过测试

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

但当我这么做的时候

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


expect(action).toThrowError(FunctionalError);

我有这样的结果:接收到的函数没有抛出


共1个答案

匿名用户

您忘记实际调用操作,请将其更改为:

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