![](/img/trans.png)
[英]Jest snapshot testing TypeError: Invalid attempt to destructure non-iterable instance
[英]TypeError: Invalid attempt to destructure non-iterable instance React/Jest
我正在尝试使用 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;
和我使用 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();
});
});
我遇到了错误 TypeError: Invalid attempt to destruction non-iterable instance
也尝试过 Enzyme,但得到了同样的错误。 花很多时间参考其他相关问题,但无法弄清楚。 谢谢你。
有一个类似的问题,Storybook 使用钩子(useContext)抛出了Invalid attempt to destruct non-iterable instance
。
解决方案是使用默认值createContext([{}, function() {}])
启动上下文
我在这里找到了解决方案
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.