[英]TypeError: Invalid attempt to destructure non-iterable instance React/Jest
I am trying to do a SnapShot verification using Jest for my React App for one of the functional component.我正在尝试使用 Jest 对我的 React 应用程序的一个功能组件进行 SnapShot 验证。 Here is the component and test file using Jest
这是使用 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;
and my test using Jest和我使用 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();
});
});
and i was getting below error TypeError: Invalid attempt to destructure non-iterable instance我遇到了错误 TypeError: Invalid attempt to destruction non-iterable instance
Tried with Enzyme as well but getting the same error.也尝试过 Enzyme,但得到了同样的错误。 Spend good time in referring other related questions but couldn't figure out.
花很多时间参考其他相关问题,但无法弄清楚。 Thank you.
谢谢你。
Had a similar problem where Storybook threw Invalid attempt to destruct non-iterable instance
on a component using hooks (useContext).有一个类似的问题,Storybook 使用钩子(useContext)抛出了
Invalid attempt to destruct non-iterable instance
。
The solution was to initiate the context with a default value, createContext([{}, function() {}])
解决方案是使用默认值
createContext([{}, function() {}])
启动上下文
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.