我正在写一个async测试,期望async函数抛出像这样:
it("expects to have failed", async () => {
let getBadResults = async () => {
await failingAsyncTest()
}
expect(await getBadResults()).toThrow()
})
但玩笑只是失败而不是通过测试:
FAIL src/failing-test.spec.js
● expects to have failed
Failed: I should fail!
如果我重写这个测试,看起来像这样:
expect(async () => {
await failingAsyncTest()
}).toThrow()
我得到这个错误,而不是通过测试:
expect(function).toThrow(undefined)
Expected the function to throw an error.
But it didn't throw anything.
await expect(async () => {
await someAsyncFunction(someParams);
}).rejects.toThrowError("Some error message");
我们必须将代码包装在函数中以捕获错误。在这里,我们期望someAsyncFunction抛出的错误消息应该等于“一些错误消息”。我们也可以调用异常处理程序
await expect(async () => {
await someAsyncFunction(someParams);
}).rejects.toThrowError(new InvalidArgumentError("Some error message"));
阅读更多信息https://jestjs.io/docs/expect#tothrowerror
如果你想测试一个async函数不抛出:
it('async function does not throw', async () => {
await expect(hopefullyDoesntThrow()).resolves.not.toThrow();
});
不管返回的值是什么,上面的测试都将通过,即使是未定义的。
请记住,如果一个异步函数抛出一个错误,它实际上是作为一个承诺拒绝返回节点,而不是一个错误(这就是为什么如果你没有try/catch块,你会得到一个UnhandledPromiseRejectionWarning,与一个错误略有不同)。所以,就像其他人说的,这就是你使用either的原因:
.reject和.resolve方法,或者a
在测试中尝试/捕获块。
参考:
https://jestjs.io/docs/asynchronous#asyncawait
如果你想在测试用例中使用try/catch方法,你可以像下面这样做。
test("some test case name with success", async () => {
let response = null;
let failure = null;
// Before calling the method, make sure someAsyncFunction should be succeeded
try {
response = await someAsyncFunction();
} catch(err) {
error = err;
}
expect(response).toEqual(SOME_MOCK_RESPONSE)
expect(error).toBeNull();
})
test("some test case name with failure", async () => {
let response = null;
let error = null;
// Before calling the method, make sure someAsyncFunction should throw some error by mocking in proper way
try {
response = await someAsyncFunction();
} catch(err) {
error = err;
}
expect(response).toBeNull();
expect(error).toEqual(YOUR_MOCK_ERROR)
})
编辑:
由于我给出的解决方案没有利用内置的笑话测试抛出功能的优势,请遵循@Lisandro https://stackoverflow.com/a/47887098/8988448建议的其他解决方案
it('should test async errors', async () => {
await expect(failingAsyncTest())
.rejects
.toThrow('I should fail');
});