我有一个助手只会抛出错误:
export const checkPanoramaFormat = (panorama) => {
if (!panorama.objectId) {
throw new Error('panorama id is required')
}
}
这是我的测试:
import {
checkPanoramaFormat
} from '@/common/helpers'
describe('checkPanoramaFormat', () => {
const panorama = { anotherProp: '1' }
it('creates clone', () => {
expect(checkPanoramaFormat(panorama)).toThrowError('hshshs')
})
})
我希望终端能够告诉我预期的内容和我得到的内容。但我一无所获。事实上,Jest并没有告诉我任何事情:
为什么会这样以及如何解决?
答案 0 :(得分:5)
尝试改为:
expect(() => {
checkPanoramaFormat(panorama)
}).toThrow('hshshs')
如果您立即将函数作为expect
参数调用,它将在断言之前抛出异常。它不在try/catch
块内。您应该将函数处理程序传递给expect
,以便仅在断言时调用。我将它括在箭头函数中,但它可以被称为其他形式,如:
expect(myFunc) // no arguments
expect(myFunc.bind(this, arg1, arg2)) // using .bind to set arguments for later calling