[英]React App - cannot read prop of children (getting undefined) while testing using Jest and Enzyme
I am trying to do my first tests for the React application using Jest and Enzyme.我正在尝试使用 Jest 和 Enzyme 对 React 应用程序进行第一次测试。 I have a component SignUpForm which takes children and render the correct one based on its current state value.
我有一个组件 SignUpForm ,它接受孩子并根据其当前状态值呈现正确的。
Example:例子:
if (children.length < currentStep) {
return <Success />;
}
Everything works fine except when I try to do test for it.一切正常,除非我尝试对其进行测试。 This is because I am getting error for pointing at the children prop and saying:
这是因为我指向儿童道具并说:
TypeError: Cannot read property 'length' of undefined
类型错误:无法读取未定义的属性“长度”
This is what I am doing in my test for now:这就是我现在在测试中所做的:
it("Should render without crashing", () => {
const component = shallow(<SignUpForm />);
console.log(component);
});
You want to test for the scenario where there is no children at all or undefined and that's fine.您想测试根本没有孩子或未定义的场景,这很好。 All what you need to do is to add one more check for the existence of the
children
variable :)您需要做的就是再添加一项检查
children
变量是否存在:)
if (!children || children.length < currentStep) {
return <Success />;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.