[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)

猜你喜欢

转载自www.cnblogs.com/Answer1215/p/12814567.html
今日推荐