简体   繁体   English

React App - 使用 Jest 和 Enzyme 进行测试时无法读取孩子的道具(变得未定义)

[英]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.

相关问题 开始使用Enzyme和Jest测试React组件 - Getting started testing React components with Enzyme and Jest 用笑话和酶标记问题测试反应应用程序 - Testing react app with jest and enzyme token problem React - 使用 Jest 进行测试 - TypeError:无法读取未定义的属性“销毁” - React - Testing w/ Jest - TypeError: Cannot read property 'destroy' of undefined 如何使用Jest和Enzyme在React中测试表单提交? 无法读取未定义的属性&#39;preventDefault&#39; - How to test form submission in React with Jest and Enzyme? Cannot read property 'preventDefault' of undefined 反应,酶:无法读取未定义的属性“历史” - React, Enzyme : Cannot read property 'history' of undefined 使用redux时无法读取react应用程序中未定义的属性“状态” - Cannot read property 'state' of undefined in react app while using redux 类型错误:无法读取未定义的笑话酶的属性“值” - TypeError: Cannot read property 'value' of undefined jest enzyme 将prop插入反应组件的Jest测试 - 接收&#39;无法读取属性&#39;映射&#39;未定义&#39;错误 - Inserting prop into Jest test for react component - receiving 'Cannot read property 'map' of undefined' error 类型错误:无法读取 null 的属性“scrollIntoView” - 反应。 笑话酶 - TypeError: Cannot read property 'scrollIntoView' of null - react. jest enzyme 在组件中使用 React.Children 时,Jest 和 React 测试库返回未定义的错误 - Jest and React Testing Library returning undefined error when using React.Children in a component
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM