Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jestjs 如何测试应该抛出错误的组件?_Jestjs_Enzyme - Fatal编程技术网

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

Jestjs 如何测试应该抛出错误的组件?,jestjs,enzyme,Jestjs,Enzyme,我的组件抛出错误(,应该是) 您可以这样做: it('should throw an error when there is an error', () => { try { wrapper.setState({error: true}); expect(true).toBe(false); //Fail test if no error is thrown } catch (error) { expect(error).toBe(error); //Pas

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


您可以这样做:

it('should throw an error when there is an error', () => {
  try {
    wrapper.setState({error: true});
    expect(true).toBe(false); //Fail test if no error is thrown
  } catch (error) {
    expect(error).toBe(error); //Pass test if an error is thrown
  }
});

在上面的例子中,我在状态中有错误。您的函数可能被称为onClick或onChange。在这种情况下,您必须在断言之前使用。

在回调中包装装载。像这样:

expect(() => mount(<ProblemChild />)).toThrow()
expect(()=>mount()).toThrow()
expect(wrapper).toThrow()
it('should throw an error when there is an error', () => {
  try {
    wrapper.setState({error: true});
    expect(true).toBe(false); //Fail test if no error is thrown
  } catch (error) {
    expect(error).toBe(error); //Pass test if an error is thrown
  }
});
expect(() => mount(<ProblemChild />)).toThrow()