jestjs - 如何测试应该抛出错误的组件?

标签 jestjs enzyme

我的组件抛出错误(应该是)

function ProblemChild() {
    throw new Error("Error thrown from problem child");
    return <div>Error</div>; // eslint-disable-line
}

我的问题是..我该如何测试这个?

const wrapper = mount(<ProblemChild />);

类似这样的事情..

expect(wrapper).toThrow()

最佳答案

在回调中包裹安装。就像这样:

expect(() => mount(<ProblemChild />)).toThrow()

关于jestjs - 如何测试应该抛出错误的组件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52283294/

相关文章:

angular - 如何在 nrgx 8 单元测试中模拟选择器?

javascript - React Enzyme Props 未设置在 Mount 上

javascript - 如何测试在 componentDidMount 中设置 React Component 状态的异步调用

reactjs - 使用 Jest-Enzyme 测试样式组件

reactjs - 如何在 React 组件中测试 componentWillReceiveProps 方法

javascript - 酵素的浅薄是渲染 child 的 child 吗?

reactjs - Simulate vs props - On Change 事件使用 Jest 和 Enzyme

javascript - 我们如何与 npm start 或 npm build 一起运行 jest 单元测试?

jasmine - 获取当前执行的描述/测试名称

node.js - NestJS Jest 找不到具有绝对路径的模块