简体   繁体   English

如何使用反应测试库测试需要钩子的组件?

[英]How to test Component which requires hook using react testing library?

I have built a component which requires a methods and data supplied by custom hook我已经构建了一个组件,它需要自定义钩子提供的方法和数据

const MyComponent = () => {
  const { data, methods } = useMyCustomHook({ defaultValues: defaultValues });

  return <MyAnotherComponent data={data} label="Some Text" method={methods} />;
}

I am writing test using react testing library to test MyComponent or to be more specific to test MyAnotherComponent我正在使用反应测试库编写测试来测试MyComponent或更具体地测试MyAnotherComponent

Here is my testing code这是我的测试代码

test("Test MyAnotherComponent Label", () => {
  const { data, methods } = useMyCustomHook({ defaultValues: defaultValues });
  render(
    <MyAnotherComponent
      data={data}
      label="Some Text"
      method={methods}
    />
  );
  expect(screen.getByLabelText("Some Text")).toBeInTheDocument();
});

My testcase fails with an error saying Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons我的测试用例失败,错误提示Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons

I have looked up for solutions but some are too simple and some are too complex.我一直在寻找解决方案,但有些太简单了,有些太复杂了。 Thanks in advance提前致谢

The error is caused by the fact that you are calling the hook inside a function, not inside a React component.该错误是由于您在 function 内调用钩子,而不是在 React 组件内。 Since you only want to test MyAnotherComponent, I don't see why you'd need to call the hook and not mock the data and methods directly to pass them to MyAnotherComponent because what you are currently doing is to re-write MyComponent .由于您只想测试 MyAnotherComponent,我不明白为什么您需要调用钩子而不是直接模拟datamethods以将它们传递给 MyAnotherComponent,因为您当前正在做的是重新编写MyComponent

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 React 测试库:如何测试需要来自其父级的 useState 钩子的组件? - React Testing Library: How to test a component that requires the useState hook from its parent? 如何使用反应测试库测试带有钩子的反应组件 - How to test react component with hooks using react testing library 如何测试使用自定义 TypeScript React Hook 的组件? - How to test a component which is using a custom TypeScript React Hook? 如何使用 react 测试库 jest 测试下拉组件? - How to test a dropdown component in react using react testing library jest? 测试组件使用自定义钩子 react-testing-library - Test component use custom hook react-testing-library 如何使用 React 测试库测试记忆化组件的回调? - How to test a memoized component's callback using React Testing Library? 使用 test 和 react-testing-library 测试反应组件 - Test a react component using test and react-testing-library 使用 Redux 和 react-testing-library 测试 React 组件 - Test React Component using Redux and react-testing-library 如何使用反应测试库对父组件中子组件的渲染进行单元测试? - How to Unit test the rendering of a child component in the parent component using react testing library? 如何使用 onClick 函数 Jest/React 测试库测试 React 组件 - How to test react component with onClick function Jest/React testing library
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM