简体   繁体   English

将jest.mock用于React组件时出现控制台错误

[英]Getting console errors when using jest.mock for React components

When I mock a react component like this jest.mock('react-slick', () => 'div'); 当我模拟这样的反应组件时jest.mock('react-slick', () => 'div'); I get console errors for unknown props passed to tag. 我收到传递给标记的未知道具的控制台错误。 I know these errors don't mean anything in this case, but it's quite annoying to see them in the output: 我知道这些错误在这种情况下不代表任何意义,但是在输出中看到它们很烦人:

控制台错误

Is there a way to disable these errors? 有办法禁用这些错误吗? Maybe just don't pass any props to the component when it is mocked? 也许在模拟时不将任何道具传递给组件?

You can just mock the console.error, like so: 您可以仅模拟console.error,如下所示:

console.error = jest.genMockFn()

Jest will still fail the tests regardless of the above. 无论以上哪种情况,Jest仍将使测试失败。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM