[React Testing] Hide console.error Logs when Testing Error Boundaries with jest.spyOn
Posted answer1215
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[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)
以上是关于[React Testing] Hide console.error Logs when Testing Error Boundaries with jest.spyOn的主要内容,如果未能解决你的问题,请参考以下文章
[React Testing] Error State with React Testing Library, findBy*
react-testing-library - 屏幕与渲染查询
[React Testing] Test your Custom Hook Module with react-hooks-testing-library
使用 `react-testing-library` 和 `cypress` 有啥区别?