Bug: `Found multiple elements with text`
See original GitHub issueDescribe the bug
It seems that in a certain case the library will find more than one matching element with getByText
. See the reproduction.
Expected behavior
Should find just one element.
Steps to Reproduce
function TestText({ children }) {
return <Text>{children}</Text>;
}
function TestComponent() {
return (
<TestText>
<TestText>Foo</TestText>
</TestText>
);
}
test('shows bug', () => {
const component = render(<TestComponent />);
component.getByText('Foo'); // Found multiple elements with text: Foo
});
function TestComponent2() {
return (
<Text>
<Text>Foo</Text>
</Text>
);
}
test('works properly', () => {
const component = render(<TestComponent2 />);
component.getByText('Foo'); // OK
});
debug
shows:
<Text>
<Text>
Foo
</Text>
</Text>
Note that if TestText
is replaced with just Text
, so that it is used directly, the error goes away.
Versions
npmPackages: @testing-library/react-native: ^7.2.0 => 7.2.0 react: 17.0.1 => 17.0.1 react-native: 0.64.1 => 0.64.1 react-test-renderer: 17.0.1 => 17.0.1
Issue Analytics
- State:
- Created 2 years ago
- Comments:6 (2 by maintainers)
Top Results From Across the Web
Found multiple elements error in React Testing ... - JS-HowTo
The reason for this error is coming from trying to query a single element by text/attribute, but indeed there are multiple element that...
Read more >Found multiple elements error in React Testing Library
Error thrown by RTL. TestingLibraryElementError: Found multiple elements with the role "radio" and name `/to/i` Here are the matching elements: ...
Read more >Found multiple elements error in React Testing Library-Reactjs
What was causing this for me was simply having another render() in the test file that was not inside an it().... so the...
Read more >Common mistakes with React Testing Library - Kent C. Dodds
This could be // because the text is broken up by multiple elements. In this case, you can // provide a function for...
Read more >React Testing Library Common Scenarios
How to write tests that are common to many applications using React Testing Library.
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
That’s unfortunate… If it’s breaking, I don’t see why it couldn’t be included in 8 😓
Bumping this with a comment to get maintainer attention, as this is one of several issues I’m running into with my component tests after upgrading my React Native from
0.63.4
to0.64.2
.