简体   繁体   English

@testing-library/react (rtl) 'waitFor' 只有在没有 await 关键字的情况下才会成功

[英]@testing-library/react (rtl) 'waitFor' makes only success without await keyword

await waitFor() makes my test fail but waitFor() makes my test successful (without await). await waitFor()使我的测试失败,但waitFor()使我的测试成功(没有等待)。

The official doc said官方文档说

The async methods return a Promise, so you must always use await or.then(done) when calling them.异步方法返回 Promise,因此在调用它们时必须始终使用 await 或.then(done)。 ( https://testing-library.com/docs/guide-disappearance ) https://testing-library.com/docs/guide-disappearance

I don't know how can I test correctly.我不知道如何正确测试。 do I have to use rerender ?我必须使用重新rerender吗?

it('toggles active status', async () => {
  render(<List {...listProps} />);
  const targetItem = screen.getByRole('heading', { name: /first/i });

  // de-active color is GRAY2, active color is MINT
  expect(targetItem).toHaveStyle({ color: GRAY2 });

  // click to change the color of targetItem
  // it dispatch action that update listProps
  // So changing listProps makes <List /> re-rendering
  fireEvent.click(targetItem);

  await waitFor(() => {
    // It throws an error because the color is still GRAY2 in jest runner.
    // But, in chrome browser, it's color MINT.
    expect(targetItem).toHaveStyle({ color: MINT }); // fail
  });

  // If not use 'await' keyword, this works well.
  // jest runner knows the color is MINT
  waitFor(() => {
    expect(targetItem).toHaveStyle({ color: MINT });
  });
});

If I use watingFor() with out await it was not failed but also not successful.如果我在没有await的情况下使用watingFor() ,它并没有失败,但也没有成功。

expect statement pass through without testing.期望语句通过而无需测试。 waitFor() without await is my misconception.没有等待的waitFor()是我的误解。 It's always successful even if has to fail.即使失败了也总是成功的。

Finally, I have known that test framework can not detect results of changing props.最后,我知道测试框架无法检测到更改道具的结果。 The props are passed from Parent Component even if it is in redux store in fact.道具是从父组件传递的,即使它实际上在 redux 存储中。

In summary, I want to test Child Component.总之,我想测试子组件。 It gets props from Parent Component, Parent gets data to pass props from redux store.它从 Parent Component 获取 props,Parent 获取数据以从 redux store 传递 props。

Click event of Child fires dispatch and change store state well. Child fires dispatch 的 Click 事件并更改存储 state 好。 But in test runner, because rendering is isolated, It was seemed to cause an error.但是在测试运行器中,由于渲染是孤立的,似乎会导致错误。 Store state was changed but props was passed still not changed So I changed Child data structure not getting props from Parent but getting from store.存储 state 已更改,但通过的道具仍未更改所以我更改了子数据结构,不是从父级获取道具,而是从存储区获取。

because if you are not awaiting, you are getting a promise, which is a truthy value.因为如果您不等待,您将得到一个 promise,这是一个真实值。 now if you do await and the promise resolves to a falsy value only in that case your test case will fail现在,如果您等待并且 promise 仅在这种情况下解析为虚假值,您的测试用例将失败

暂无
暂无

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

相关问题 React 测试库如何使用 waitFor - React testing library how to use waitFor @testing-library/react 中的 toBeInTheDocument 和 getBy* 有什么区别 - Whats the difference between toBeInTheDocument and getBy* in @testing-library/react 测试库反应模拟 function 与回调将设置 state 返回 - testing-library react mock function with callback that will set state on return React Native 测试库显示使用 `waitFor` 的警告 - React Native Testing Library shows a warning for using `waitFor` 使用 Jest 和 @testing-library/react-hooks 在 TypeScript 中单元测试自定义 React Hook - Unit Testing Custom React Hooks in TypeScript using Jest and @testing-library/react-hooks 反应测试 | 从“@testing-library/user-event”导入 userEvent 会生成错误“toBeInTheDocument 不是函数” - React Testing | importing userEvent from '@testing-library/user-event' generates error "toBeInTheDocument is not a function" React testing-library - 测试在第一个 useEffect 挂钩中设置状态的 promise - React testing-library - Testing a promise that set states in the first useEffect hook 在 React / testing-library/react 中呈现所有组件后,如何从表中获取元素? - How to get the elements from a table after all components are rendered in React / testing-library/react? 使用@testing-library/react 对 react-router 的链接进行最简单的测试 - Simplest test for react-router's Link with @testing-library/react 如何使用测试库在 React 中为单个渲染测试不同类型的测试? - How to test different types of test for a single render in React with testing-library?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM