【问题标题】:Exception handling in Jest/EnzymeJest/Enzyme 中的异常处理
【发布时间】:2018-12-13 06:26:25
【问题描述】:

我正在使用 Jest/Enzyme 测试我的 React 组件。

在某些测试用例中,测试失败并出现一些异常。 在 Jest/Enzyme 中处理错误而不是破坏测试用例的理智/标准方法是什么?

例如 - 如果 'Grid' 组件没有获取任何数据作为参数,则应该通过以下情况。但是,它会失败,并抛出一个从 Grid 组件中抛出的异常。

test('Grid does not render without data',()=>{
const wrapper=shallow(<Grid/>);
expect(wrapper.length.toBe(0));
})

【问题讨论】:

    标签: reactjs jestjs enzyme


    【解决方案1】:

    您不应在测试用例中处理错误。相反,您应该期望代码会抛出错误。

    你的代码应该是这样的

    test('Grid should throw when data is not passed',()=>{
      expect(() => shallow(<Grid/>)).toThrow();
    })
    

    但理想情况下,当正确的道具不存在时,组件不应该抛出,而是不应该渲染。

    【讨论】:

      猜你喜欢
      • 2021-06-21
      • 2021-06-09
      • 2020-06-26
      • 1970-01-01
      • 1970-01-01
      • 2021-09-16
      • 2023-03-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多