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.

Error: Unable to find an element with text: "My text", while that text exist.

See original GitHub issue

Describe the bug

getByText can find element if text render component with text as not children (See “Steps to Reproduce” below)

-const Trans = p => p.i18nKey; // can find (error)
+const Trans = p => p.children; // everything ok

Expected behavior

I see in debug output (see screenshot below) that text exists! But API can find it( It should work properly!

Steps to Reproduce

    test('test',  () => {
      const { Text } = require('react-native');
      const { render } = require('@testing-library/react-native');
      const Trans = p => p.i18nKey;

      const screen = render(
        <Text>
          <Trans i18nKey="My text" />
        </Text>,
      );

      screen.debug();

      expect(screen.getByText('My text')); // Error: Unable to find an element with text: My text
    });

Screenshots

Output:

image

Versions

  npmPackages:
    @testing-library/react-native: ^7.2.0 => 7.2.0 
    react: 17.0.2 => 16.13.1 
    react-native: 0.66.4 => 0.66.4 
    react-test-renderer: 17.0.2 => 17.0.2 

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:34 (13 by maintainers)

github_iconTop GitHub Comments

3reactions
pierrezimmermannbamcommented, Mar 24, 2022

Not sure this is easy to fix, the type of <Trans i18nKey=“text”/> isn’t string so the getChildrenAsText method is looking for strings in its children but it doesn’t have any. For this to work it would require to be able to tell that it renders as a string but i’m not sure it’s easily doable.

@retyui this can be fixed on your end by mocking the Trans component like this

jest.mock('./Trans', () => {
  const { Text } = require('react-native');

  return {
    Trans: (props) => {
      return <Text>{props.i18nKey}</Text>;
    },
  };
});

However if there is an effective way to fix this it would be very nice, or maybe it could be documented somewhere

2reactions
mdjastrzebskicommented, Sep 27, 2022

@pierrezimmermannbam I refactored back into toJSON method on ReactTestInstance and I’ve submitted PR: https://github.com/facebook/react/pull/25329

Let’s see how the review goes…

Read more comments on GitHub >

github_iconTop Results From Across the Web

"myText" error when using react-testing-library - Stack Overflow
Unable to find an element with the text: imageDescripton. This could be because the text is broken up by multiple elements. In this...
Read more >
[Solved]-Unable to find an element with the text - appsloveworld
Coding example for the question Unable to find an element with the text: "myText" error when using react-testing-library-Reactjs.
Read more >
Common mistakes with React Testing Library - Kent C. Dodds
getByText(/hello world/i) // ❌ fails with the following error: // Unable to find an element with the text: /hello world/i.
Read more >
Making sure you're using the correct query - Tim Deschryver
Querying broken text. The *ByText and *ByLabelText can't find elements that are broken into multiple elements. For example, given the following ...
Read more >
“theText” error when using react-testing-library – iTecNote
Reactjs – Unable to find an element with the text: “theText” error when using react-testing-library ... I'm trying to use react-testing-library with React...
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