简体   繁体   English

TypeError:对不可迭代实例 React/Jest 的解构尝试无效

[英]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() {}])启动上下文

I found the solution here我在这里找到了解决方案

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Jest 快照测试 TypeError:尝试解构不可迭代实例无效 - Jest snapshot testing TypeError: Invalid attempt to destructure non-iterable instance 未处理的拒绝(TypeError):破坏非迭代实例的无效尝试 - Unhandled Rejection (TypeError): Invalid attempt to destructure non-iterable instance 未捕获的 TypeError:无效的解构不可迭代实例的尝试 - Uncaught TypeError: Invalid attempt to destructure non-iterable instance 解构不可迭代实例的无效尝试 - Invalid attempt to destructure non-iterable instance React,获取 TypeError:在将全局状态与 Context 结合使用时,尝试解构不可迭代的实例无效 - React, Getting TypeError: Invalid attempt to destructure non-iterable instance when using global state with Context React Uncaught TypeError:传播不可迭代实例的无效尝试 - React Uncaught TypeError: Invalid attempt to spread non-iterable instance 使用反应测试库时尝试解构不可迭代实例无效 - Invalid attempt to destructure non-iterable instance while using react testing library TypeError:无效的解构不可迭代实例的尝试。 为了可迭代,非数组对象必须有一个 [Symbol.iterator]() - TypeError: Invalid attempt to destructure non-iterable instance. In order to be iterable, non-array objects must have a [Symbol.iterator]() 未处理的拒绝(TypeError):散布不可迭代实例的无效尝试 - Unhandled Rejection (TypeError): Invalid attempt to spread non-iterable instance 类型错误:传播不可迭代实例和合成事件的尝试无效 - TypeError: Invalid attempt to spread non-iterable instance and Synthetic Events
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM