【发布时间】:2020-03-10 16:36:15
【问题描述】:
我正在尝试使用 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: 无效的解构不可迭代实例的尝试
也尝试过 Enzyme,但得到了同样的错误。花很多时间参考其他相关问题,但无法弄清楚。谢谢你。
【问题讨论】:
-
你会模拟你在组件中使用的任何钩子吗?
-
你好,我有几个钩子。 useAppState(内部使用 useContext)和 useState。我没有嘲笑他们俩
标签: javascript reactjs jestjs enzyme destructuring