测试异步函数没有抛出的错误

时间:2021-01-19 14:12:05

标签: node.js jestjs

我正在尝试测试未抛出的错误,并且我在测试中完成了以下操作:

await expect(() => execute(1, 1)).rejects.not.toThrow()

execute 抛出错误时,测试失败,正如我希望的那样。但是,当 execute 成功解析时,我得到以下测试失败:

Received promise resolved instead of rejected
Resolved to value: undefined

我以前曾将 Jasmine 用于此类事情,它很高兴地执行 expect(() => codeThrowingError).not.toThrow(),但 Jest 的语法似乎有点受折磨,我不知道如何正确处理。

1 个答案:

答案 0 :(得分:0)

非常适合https://stackoverflow.com/users/3001761/jonrsharpe关于执行await expect(execute(1, 1)).resolves.toEqual(expect.anything())的回复