[英]Why isn't the following expect … toThrowError logging anything in my terminal?
I have a helper that just throws an error: 我有一个助手,只会抛出一个错误:
export const checkPanoramaFormat = (panorama) => {
if (!panorama.objectId) {
throw new Error('panorama id is required')
}
}
This is my test: 这是我的测试:
import {
checkPanoramaFormat
} from '@/common/helpers'
describe('checkPanoramaFormat', () => {
const panorama = { anotherProp: '1' }
it('creates clone', () => {
expect(checkPanoramaFormat(panorama)).toThrowError('hshshs')
})
})
I expected the terminal to show me what was expected and what I got. 我希望终端能告诉我期望和得到的东西。 But I got nothing. 但是我什么都没有。 In fact, Jest isn't telling me anything: 实际上,Jest并没有告诉我任何事情:
Why is this and how to fix it? 为什么会这样以及如何解决?
Try instead: 请尝试:
expect(() => {
checkPanoramaFormat(panorama)
}).toThrow('hshshs')
If you call the function immediately as the expect
argument, it will throw the exception before the assertion. 如果立即将函数作为expect
参数调用,它将在断言之前引发异常。 It is not inside a try/catch
block. 它不在try/catch
块中。 You should instead pass a function handler to expect
to be called only on assertion. 相反,您应该传递函数处理程序以expect
仅在断言时被调用。 I enclosed it in an arrow function, but it can be called other forms such as: 我将其包含在箭头函数中,但是可以将其称为其他形式,例如:
expect(myFunc) // no arguments
expect(myFunc.bind(this, arg1, arg2)) // using .bind to set arguments for later calling
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.