我正在编写一个测试来断言如果提供一个道具而不是另一个道具,组件会抛出错误。
测试本身通过了,但控制台仍然抱怨未捕获的错误并打印整个堆栈跟踪。有什么办法可以让 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");
});
Run Code Online (Sandbox Code Playgroud)
错误本身在这里抛出:
if(props.showCancel && !props.onCancel) {
throw new Error("If `showCancel` is true, you must provide an `onCancel` method");
}
Run Code Online (Sandbox Code Playgroud)