[React Testing] Hide console.error Logs when Testing Error Boundaries with jest.spyOn

When testing an error boundary, your console will be filled with console.error calls from React. Those can be a real distraction from the rest of the output for your tests. Let’s clean those up with jest.spyOn.

 

beforeAll(() => {
  // do log out any error message
  jest.spyOn(console, 'error').mockImplementation(() => {})
})

afterAll(() => {
  console.error.mockRestore()
})

 

Then we can verify the console.error should be called in the test:

expect(console.error).toHaveBeenCalledTimes(2)

 

posted @ 2020-05-01 20:10  Zhentiw  阅读(398)  评论(0编辑  收藏  举报