Error: Unable to find an element with text: "My text", while that text exist.
See original GitHub issueDescribe 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:
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:
- Created 2 years ago
- Comments:34 (13 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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
However if there is an effective way to fix this it would be very nice, or maybe it could be documented somewhere
@pierrezimmermannbam I refactored back into
toJSON
method onReactTestInstance
and I’ve submitted PR: https://github.com/facebook/react/pull/25329Let’s see how the review goes…