最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - TypeError: Invalid attempt to destructure non-iterable instance ReactJest - Stack Overflow

programmeradmin5浏览0评论

I am trying to do a SnapShot verification using Jest for my React App for one of the functional ponent. Here is the ponent and test file using 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

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

Tried with Enzyme as well but getting the same error. Spend good time in referring other related questions but couldn't figure out. Thank you.

I am trying to do a SnapShot verification using Jest for my React App for one of the functional ponent. Here is the ponent and test file using 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

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

Tried with Enzyme as well but getting the same error. Spend good time in referring other related questions but couldn't figure out. Thank you.

Share Improve this question edited Nov 14, 2019 at 16:55 skyboyer 23.8k7 gold badges62 silver badges71 bronze badges asked Nov 14, 2019 at 16:30 siddhuKantipudisiddhuKantipudi 3034 silver badges11 bronze badges 2
  • do you mock any of hooks you are using in ponent? – skyboyer Commented Nov 14, 2019 at 16:55
  • Hello, I have couple of hooks. useAppState(which uses internally useContext) and useState. I haven't mocked neither of them – siddhuKantipudi Commented Nov 14, 2019 at 18:41
Add a ment  | 

1 Answer 1

Reset to default 6

Had a similar problem where Storybook threw Invalid attempt to destruct non-iterable instance on a ponent using hooks (useContext).

The solution was to initiate the context with a default value, createContext([{}, function() {}])

I found the solution here

发布评论

评论列表(0)

  1. 暂无评论