expect(...).toThrow()
is for checking if an error is thrown from a function call. When calling an async function, it never throws an error; rather it returns a Promise
which may eventually become “rejected.” Although async functions use the same throw
/catch
terminology, the code required to detect a thrown error differs from what’s required to detect a rejected Promise. This is why Jest needs a different assertion technique.
Try expect(...).rejects.toThrow()
instead:
await expect(api(`verify/${profile.auth.verifyToken}`, {method: 'POST'}).json())
.rejects.toThrow();
Notice you have to await
this assertion because Jest needs to wait until the Promise
finalizes before seeing whether it resolved or rejected.