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.

Bug: `Found multiple elements with text`

See original GitHub issue

Describe 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:closed
  • Created 2 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
milesjcommented, Jul 15, 2021

That’s unfortunate… If it’s breaking, I don’t see why it couldn’t be included in 8 😓

1reaction
prometheascommented, Nov 4, 2021

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 to 0.64.2.

Read more comments on GitHub >

github_iconTop 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 >

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