react-testing-library为什么不等待react-hooks-form的输入。

huangapple go评论59阅读模式
英文:

Why does react-testing-library not wait for input into a react-hooks-form

问题

我正在尝试测试一个使用react-hook-form构建的表单。我通过在字段中输入文本并点击提交按钮来进行测试。然后,我断言按钮会消失,然后被一个简单的"谢谢!"消息所替代。

问题是,react-testing-library似乎不等待过渡发生,无法找到感谢消息。

到目前为止,我尝试过使用:actwaitForwaitForElementToBeRemoved,以及在userEvent.click()方法上使用await

英文:

I am trying to test a Form, built with react-hook-form. I test by putting text into the fields and clicking the submit Button.
Then i assert that the Button disappears and is replaced by a simple "Thank you!" message.

https://codesandbox.io/s/rtl-react-hook-form-question-mmh9tz

The Problem is, that react-testing-library does not seem to wait until the transition happens and can't find the thank you message.

So far i have tried using: act waitFor and waitForElementToBeRemoved as well as using await on the userEvent.click() method.

答案1

得分: 1

userEvent.type 返回一个 Promise,所以需要使用 await

在进行这个更改之后,它在这里对我有效 - https://codesandbox.io/p/sandbox/rtl-react-hook-form-question-forked-3op0r5?file=%2Fsrc%2FForm.test.tsx%3A22%2C1

然而,如果我在你分享的 codesandbox 链接中进行相同的更改,它不起作用,可能是一些环境问题。

英文:

userEvent.type returns a promise so it needs to await

After making this change, it works for me here - https://codesandbox.io/p/sandbox/rtl-react-hook-form-question-forked-3op0r5?file=%2Fsrc%2FForm.test.tsx%3A22%2C1

However, if I make the same change in the codesandbox link you have shared, it does not work, probably some environment issue.

huangapple
  • 本文由 发表于 2023年5月17日 17:45:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/76270708.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定