我正在编写一个测试来断言如果提供了一个 prop 而没有提供另一个 prop,则组件会抛出错误。
测试本身通过了,但控制台仍然抱怨未捕获的错误并打印整个堆栈跟踪。有没有办法让 Jest 停止打印此信息,因为它会污染测试运行程序并使其看起来像是失败了。
作为参考,这是我的测试:
it("throws an error if showCancel is set to true, but no onCancel method is provided", () => {
// Assert that an error is thrown
expect(() => mount(<DropTarget showCancel={ true }/>)).toThrowError("If `showCancel` is true, you must provide an `onCancel` method");
});
错误本身在这里抛出:
if(props.showCancel && !props.onCancel) {
throw new Error("If `showCancel` is true, you must provide an `onCancel` method");
}
我找到了我的问题的一行答案here https://github.com/facebook/react/issues/11098.
Adding spyOn(console, "error");
(在预期出现错误的测试中)禁止记录错误。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)