类型错误:尝试解构不可迭代实例 React/Jest 的尝试无效

sid*_*udi 7 javascript destructuring reactjs jestjs enzyme

我正在尝试使用 Jest 对我的 React 应用程序中的一个功能组件进行 SnapShot 验证。这是使用 Jest 的组件和测试文件

import React, { useState } from 'react';
import useForm from 'react-hook-form';
import { useAppState } from 'Shared/store';
...

const Form = () => {

const {customReducer, dispatch} = useAppState();
const { register, handleSubmit, errors, reset } = useForm({});

//custom form methods

return (
<>
   <Form ...>
     ...
   </Form>
   ....
</>
);
export default Form;
Run Code Online (Sandbox Code Playgroud)

和我使用 Jest 的测试

import React from 'react';
import TestRenderer from 'react-test-renderer';
import ShallowRenderer from 'react-test-renderer/shallow';
import Form from '../form';

describe('<Form />', () => {
    test('Snapshot', () => {
    const tree = TestRenderer.create(<Form />).toJSON();
    expect(tree).toMatchSnapshot();
  });
});
Run Code Online (Sandbox Code Playgroud)

并且我遇到了错误 TypeError: Invalid trial to destructure non-iterable instance

也试过酶,但得到同样的错误。花时间参考其他相关问题,但无法弄清楚。谢谢你。

Nat*_*ams 5

有一个类似的问题,StorybookInvalid attempt to destruct non-iterable instance使用钩子(useContext)抛出一个组件。

解决方案是使用默认值启动上下文,createContext([{}, function() {}])

我在这里找到了解决方案