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
1 Answer
Reset to default 6Had 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