Javascript 为什么不是';以下是我的期望。。。TothRower在我的终端上记录了什么?

Javascript 为什么不是';以下是我的期望。。。TothRower在我的终端上记录了什么?,javascript,jestjs,Javascript,Jestjs,我有一个助手刚刚抛出了一个错误: export const checkPanoramaFormat = (panorama) => { if (!panorama.objectId) { throw new Error('panorama id is required') } } 这是我的测试: import { checkPanoramaFormat } from '@/common/helpers' describe('checkPanoramaFormat',

我有一个助手刚刚抛出了一个错误:

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没有告诉我任何事情:

为什么会出现这种情况以及如何修复它?

请尝试:

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

文档完美地展示了这一点:有些人将虚拟断言放在try/catch中,但我不喜欢这种方法。有点:
try{expect(fn).toThrow()}catch{expect(true).equals(true)}
@alex您的断言是错误的,您不希望返回正确的错误消息。