作者:娜丷衣阵风丶 | 来源:互联网 | 2023-09-24 11:03
我有一个使用Formik制作的基本登录表单,只是尝试编写 RTL 和 jest 代码来填写详细信息、提交表单并导航到主页。因此,写了以下内容:
it('Fill the form and login', async () => {
render()
await userEvent.type(screen.getByTestId('emailInput'), 'neeraj@gmail.com')
await userEvent.type(screen.getByTestId('passwordInput'), 'neeraj')
await userEvent.click(screen.getByTestId('submitBtn'))
expect(window.location.pathname).toBe('/')
})
上面的测试已经通过,但出现了经典的行为错误。
When testing, code that causes React state updates should be wrapped into act(...): act(() => {
/* fire events that update state */
});
/* assert on the output */
When testing, code that causes React state updates should be wrapped into act(...): act(() => {
/* fire events that update state */
});
/* assert on the output */
因此,参考此博客文章并按照建议使用waitForElementToBeRemoved函数,但现在测试因超时错误而失败。
使用 waitForElementToBeRemoved 更新测试用例
错误:
Timed out in waitForElementToBeRemoved.
Ignored nodes: comments, ,
19 |
20 | expect(window.location.pathname).toBe('/')
> 21 | await waitForElementToBeRemoved(screen.getByTestId('emailInput'))
| ^
22 | })
23 | })
24 |
at waitForElementToBeRemoved (node_modules/@testing-library/dom/dist/wait-for-element-to-be-removed.js:22:24)
at Object. (tests/login.test.js:21:11)
试图将这三个userEvent包装在act函数中,但得到相同的超时错误,无法弄清楚我哪里出错了。
代码沙盒链接