question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

getByText can't find <Text>

See original GitHub issue

Describe the bug

I’m trying the most basic example possible of getByText query in a component that just renders one <Text> component. It can’t find the element.

Expected behavior

It finds the element.

Steps to Reproduce

Create an empty component that just renders <Text>text</Text> and test with getByText(/text/i).

Screenshots

image

Versions

  npmPackages:
    @testing-library/react-native: ^7.1.0 => 7.1.0
    react: 16.13.1 => 16.13.1
    react-native: 0.63.4 => 0.63.4
    react-test-renderer: ^16.13 => 16.14.0

Extra info

Maybe it has to do with the way I setup the project? you can find the repository here: https://gitlab.com/joanca/rn-playground

Thanks!

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

5reactions
janithlcommented, Apr 3, 2021

bump

This is a real pain point for someone trying to upgrade from 6.0 as partial matches were working just fine on previous versions.

2reactions
MattAgncommented, Mar 31, 2021

@lukewlms I think your second example does not work because the queries try to find a perfect match for the text, finding substrings does not work yet. I you have “2MR Plus +” in your component but search for “2MR Plus”, it won’t find the node. However I don’t know how you can search for a font awesome icon in your test. This open PR should help solve your problem by allowing to search for partial match.

Read more comments on GitHub >

github_iconTop Results From Across the Web

getByText doesn't find element with the text - Stack Overflow
As the react-native testing library shows in the documentation examples the getByText query, if provided a string, will look for the exact ...
Read more >
ByText - Testing Library
This will search for all elements that have a text node with textContent matching the given TextMatch . <a href="/about"> ...
Read more >
Queries | React Native Testing Library - Open Source
Returns a ReactTestInstance with matching text – may be a string or regular expression. This method will join <Text> siblings to find matches,...
Read more >
Making sure you're using the correct query - Tim Deschryver
getByText (/hello world/i);. // |>Error: TestingLibraryElementError: Unable to find an element with the text: /hello world/i. This could ...
Read more >
Testing Visible Text with React Testing Library's getByText ...
You'll notice that I didn't expect on our total here, and that's because screen.getByText will actually throw an error if it doesn't exist....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found