JEST Received 函数没有抛出,但是抛出了 HTTPError

JEST Received function did not throw, but HTTPError is thrown

我正在使用 JEST 和 Got 测试端点。我期待 403 Forbidden 错误。以下代码打印来自 catch 块的错误,并且由于相同的调用不会引发错误而失败。为什么?

    try {
        response = await api(`verify/${profile.auth.verifyToken}`, {method: 'POST'}).json();
    } catch (e) {
        console.log(e);
    }
    expect(async () => {
        response = await api(`verify/${profile.auth.verifyToken}`, {method: 'POST'}).json();
    }).toThrow();

输出:

console.log test/api.int.test.js:112
HTTPError: Response code 403 (Forbidden)
    at EventEmitter.<anonymous> (C:\dev\mezinamiridici\infrastructure\node_modules\got\dist\source\as-promise.js:118:31)
    at processTicksAndRejections (internal/process/task_queues.js:97:5) {
  name: 'HTTPError'
}


Error: expect(received).toThrow()
Received function did not throw

这个变体也不起作用:

expect(() => api(`verify/${profile.auth.verifyToken}`, {method: 'POST'})).toThrow();

顺便说一下,当 HTTPError 被抛出但没有被捕获时,没有堆栈跟踪,我也看不到错误是从哪里抛出的。如果还有其他错误,我会确切地看到是哪条测试线造成的。为什么?

expect(...).toThrow() 用于检查函数调用是否抛出错误。调用异步函数时,它永远不会抛出错误;相反,它 returns 一个 Promise 可能 最终被“拒绝”。尽管异步函数使用相同的 throw/catch 术语,但检测抛出错误所需的代码与检测被拒绝的 Promise 所需的代码不同。这就是为什么 Jest 需要一种不同的断言技术。

请尝试 expect(...).rejects.toThrow()

await expect(() => api(`verify/${profile.auth.verifyToken}`, {method: 'POST'}).json())
  .rejects.toThrow();

请注意,您必须 await 这个断言,因为 Jest 需要等到 Promise 结束才能看到它是已解决还是被拒绝。