简体   繁体   English

对于异步挂钩,@testing-library/react 中的 function waitForNextUpdate (@testing-library/react-hooks) 有什么替代方案?

[英]What is the alternative the function waitForNextUpdate (@testing-library/react-hooks) in @testing-library/react for async hooks?

I want to test my custom hook but in React 18 @testing-library/react-hooks library is not working, instead I am using @testing-library/react it has renderHook function and it works fine, but this library does not have waitForNextUpdate function for asynchronous hooks.我想测试我的自定义钩子,但在 React 18 中 @testing-library/react-hooks 库不工作,我使用的是 @testing-library/react 它有 renderHook function 并且工作正常,但这个库没有 waitForNextUpdate function 用于异步挂钩。 For this reason, I can't test my custom async hooks.出于这个原因,我无法测试我的自定义异步挂钩。

An alternative could be replacing it by waitFor .另一种方法是用waitFor替换它。

Before:前:

await waitForNextUpdate();
expect(fetch).toHaveBeenCalledTimes(1)

After

await waitFor(() => {
   expect(fetch).toHaveBeenCalledTimes(1)
}

It only worked for me when I used act and waitFor :它只在我使用actwaitFor时对我有用:

await act(async () => {
  await waitFor(() => {
    expect(result.current.isAuthenticated).toBeUndefined();
  });
});

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM