我正在学习玩笑,当我跑这第一个例子我运行测试时发生错误。如果我在一个try / catch块包装它,它工作正常。该文档显示没有try / catch语句的例子。为什么第一个例子中的错误?
错误
test('controller type error is correct', async () => {
expect(await pipe('string', podchain)).toThrow('VALIDATE PROPS: podchain must be an object.')
})
不对错误
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)
}
})
该.toThrow()
预期应该在功能工作,await pipe('string', podchain))
不是抛出一个功能。在一个正常的异步功能的try-catch块被重新编写,是对承诺的最后.catch()
条款,但开玩笑说重新写不起作用。
我建议使用玩笑.rejects
帮手:
await expect(pipe('string', podchain)).rejects.toThrow();