小编Sea*_*ean的帖子

你能编写期望toThrow的异步测试吗?

我正在写一个异步测试,希望async函数像这样抛出:

it("expects to have failed", async () => {
  let getBadResults = async () => {
    await failingAsyncTest()
  }
  expect(await getBadResults()).toThrow()
})
Run Code Online (Sandbox Code Playgroud)

但是,开玩笑只是失败而不是通过测试:

 FAIL  src/failing-test.spec.js
  ? expects to have failed

    Failed: I should fail!
Run Code Online (Sandbox Code Playgroud)

如果我重写测试看起来像这样:

expect(async () => {
  await failingAsyncTest()
}).toThrow()
Run Code Online (Sandbox Code Playgroud)

我收到此错误而不是通过测试:

expect(function).toThrow(undefined)

Expected the function to throw an error.
But it didn't throw anything.
Run Code Online (Sandbox Code Playgroud)

jestjs

32
推荐指数
7
解决办法
7662
查看次数

标签 统计

jestjs ×1