最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How can I display a custom error message when a Jest snapshot test fails? - Stack Overflow

programmeradmin0浏览0评论

I have a simple Jest snapshot test, and upon failure, I would like to print a custom error message to remind the developer that if they intentionally failed the test, they need to ensure they update a related constant that's used in the page/component. Here is my test:

describe('My Page', () => {
  it('matches the snapshot', () => {
    const { asFragment } = render(<MyComponent />)

    expect(asFragment()).toMatchSnapshot()
  })
})

I've tried to wrap the expect in a try/catch, however, the test fails immediately at toMatchSnapshot() and the catch block is never reached:

try {
  expect(asFragment()).toMatchSnapshot()
} catch (error) {
  error.message +=
    '\nNOTE: The snapshot for the page has changed. If this is intentional, please remember to update the MY_CONSTANT constant accordingly.'
  throw error
}

Is there any way to print a custom error message when a Jest snapshot test fails?

发布评论

评论列表(0)

  1. 暂无评论