英文:
Why does react-testing-library not wait for input into a react-hooks-form
问题
我正在尝试测试一个使用react-hook-form
构建的表单。我通过在字段中输入文本并点击提交按钮来进行测试。然后,我断言按钮会消失,然后被一个简单的"谢谢!"消息所替代。
问题是,react-testing-library
似乎不等待过渡发生,无法找到感谢消息。
到目前为止,我尝试过使用:act
,waitFor
和waitForElementToBeRemoved
,以及在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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论