Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Testing Library unable to find text even though screen.debug() shows the text to exist

In a nutshell, if I grab a portion of the screen by it's label:

const foo = screen.getByLabelText('Some Label');

I can see that the element I'm interested in exists in the output:

debug(foo);
...
<div
  class=" css-15zcpdi-NoOptionsMessage"
>
   Something went wrong
</div>

However if I search for "Something went wrong" in the screen:

screen.getByText('Something went wrong');

RTL claims it can't find it:

TestingLibraryElementError: Unable to find an element with the text: Something went wrong. ...

I must be doing something wrong here.. What?

like image 670
nurikabe Avatar asked Jun 05 '20 22:06

nurikabe


1 Answers

Using exact string values does not seem to work very consistently. Try a regular expression instead:

screen.getByText(/Something went wrong/);
like image 160
lyzazel Avatar answered Sep 22 '22 06:09

lyzazel