Skip to content

Bug: Found multiple elements with text #776

Closed
@andreialecu

Description

@andreialecu

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions