React testing library - await findBy* vs promise.then



我有一个测试,我等待一些文本显示:const allTodosPromise = findByText('All Todos')

我的测试实际上检查返回的HTMLElement是否有一些样式(看看它是否活跃):

expect(await allTodosPromise).toHaveStyle(activeItemStyle);

无法找到包含"所有待办事项"文本的元素。

但是,如果我用then解决我的承诺,测试通过,如:

allTodosPromise.then(htmlElement => 
expect(htmlElement).toHaveStyle(activeItemStyle)
);

为什么?是的,我确实说过我的测试函数是async

同样尝试在第一个引用中等待失败:const allTodosElement = await findByText('All Todos');

您可以尝试waitFor函数:https://testing-library.com/docs/dom-testing-library/api-async/#waitfor

最新更新