小编Jor*_*vey的帖子

如何静默地对抛出的错误进行 Jest 测试

我正在编写一个测试来断言如果提供一个道具而不是另一个道具,组件会抛出错误。

测试本身通过了,但控制台仍然抱怨未捕获的错误并打印整个堆栈跟踪。有什么办法可以让 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)

testing unit-testing typescript reactjs jestjs

6
推荐指数
1
解决办法
2090
查看次数

标签 统计

jestjs ×1

reactjs ×1

testing ×1

typescript ×1

unit-testing ×1