React 测试库无法找到文本,即使 screen.debug() 显示文本存在
React Testing Library unable to find text even though screen.debug() shows the text to exist
简而言之,如果我通过标签抓取屏幕的一部分:
const foo = screen.getByLabelText('Some Label');
我可以看到输出中存在我感兴趣的元素:
debug(foo);
...
<div
class=" css-15zcpdi-NoOptionsMessage"
>
Something went wrong
</div>
但是,如果我在屏幕中搜索 "Something went wrong":
screen.getByText('Something went wrong');
RTL 声称找不到它:
TestingLibraryElementError: Unable to find an element with the text: Something went wrong. ...
我一定是哪里做错了..什么?
使用精确的字符串值似乎不太一致。尝试使用正则表达式:
screen.getByText(/Something went wrong/);
简而言之,如果我通过标签抓取屏幕的一部分:
const foo = screen.getByLabelText('Some Label');
我可以看到输出中存在我感兴趣的元素:
debug(foo);
...
<div
class=" css-15zcpdi-NoOptionsMessage"
>
Something went wrong
</div>
但是,如果我在屏幕中搜索 "Something went wrong":
screen.getByText('Something went wrong');
RTL 声称找不到它:
TestingLibraryElementError: Unable to find an element with the text: Something went wrong. ...
我一定是哪里做错了..什么?
使用精确的字符串值似乎不太一致。尝试使用正则表达式:
screen.getByText(/Something went wrong/);