[英]Why does Jest error without try/catch blocks for async test
I'm learning Jest and when I run this first example I get an error when running the test. 我正在学习Jest,运行第一个示例时,运行测试时出现错误。 If I wrap it in a try/catch block it works fine.
如果我将其包装在try / catch块中,则效果很好。 The docs show examples without try/catch.
该文档显示了没有try / catch的示例。 Why is the first example error?
为什么第一个示例错误?
ERRORS 错误
test('controller type error is correct', async () => {
expect(await pipe('string', podchain)).toThrow('VALIDATE PROPS: podchain must be an object.')
})
DOES NOT ERROR 不会出错
test('controller type error is correct', async () => {
try {
expect(await pipe('string', podchain)).toThrow('VALIDATE PROPS: podchain must be an object.')
} catch (e) {
console.log(e.message)
}
})
The .toThrow()
expectation is supposed to work on a function, and await pipe('string', podchain))
isn't a function that throws. .toThrow()
预期应该在函数上起作用,而await pipe('string', podchain))
不是抛出的函数。 In a normal async function, try-catch blocks are re-written to be .catch()
terms on the end of promises, but in jest that re-write doesn't work. 在一个普通的异步函数中,try-catch块在
.catch()
的末尾被.catch()
为.catch()
术语,但是开玩笑说重写不起作用。
I suggest using the Jest .rejects
helper: 我建议使用Jest
.rejects
助手:
await expect(pipe('string', podchain)).rejects.toThrow();
See https://jestjs.io/docs/en/expect#rejects 参见https://jestjs.io/docs/en/expect#rejects
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.