码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2020-05-01 20:34:20      阅读:73      评论:0      收藏:0      [点我收藏+]

标签:led   tin   sts   mock   style   you   after   col   rac   

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)

 

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

标签:led   tin   sts   mock   style   you   after   col   rac   

原文地址:https://www.cnblogs.com/Answer1215/p/12814567.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!