简而言之,如果我通过它的标签抓取屏幕的一部分:
const foo = screen.getByLabelText('Some Label');
我可以看到我感兴趣的元素存在于输出中:
debug(foo);
...
<div
class=" css-15zcpdi-NoOptionsMessage"
>
Something went wrong
</div>
但是,如果我在屏幕中搜索“出现问题”:
screen.getByText('Something went wrong');
RTL声称它找不到它:
TestingLibraryElementError: Unable to find an element with the text: Something went wrong. ...
我一定在这里做错了什么..什么?
最佳答案
我有一个类似的问题。我正在使用自定义渲染方法将我的组件包装在 Redux Provider 中。然后做一些简单的事情,比如:expect(screen.getByText(/been the victim of bank fraud/i)).to.exist;
失败:
TestingLibraryElementError: Unable to find an element with the text: /been the victim of bank fraud/i. This could be because the text is broken up by multiple elements. In this case, you can provide a function for your text matcher to make your matcher more flexible.
<body />
那个<body />
表示没有渲染任何内容。但是,做 screen.debug()
就在它起作用并显示我希望看到的所有 DOM 之前。最后,使用
getByText()
从我的渲染方法工作得很好。我也确定我在这里做错了什么。
关于reactjs - 即使 screen.debug() 显示文本存在,React 测试库也无法找到文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62225194/