Skip to content

toHaveTextContent matching rules are not aligned with jest-dom #1613

Closed as not planned
@AugustinLF

Description

@AugustinLF

Hey hey, long time not posted here^^'

Describe the bug

toHaveTextContent() does exact matching by default, while the same helper exposed by jest-dom does partial matching.

test('toHaveTextContent() does exact match by default', () => {
  render(<Text testID="text">Hello World</Text>);

  const text = screen.getByTestId('text');
  expect(text).toHaveTextContent('Hello World');
  // this will error with jest-dom when using RTL.
  expect(text).not.toHaveTextContent('Hello');
  expect(text).not.toHaveTextContent('World');
});

Expected behavior

I would expect both APIs to behave similarly. Furthermore, the jest-dom matcher doesn't support the exact option, which makes it a bit hard to run both web and native tests with the same code.

I'll happily open a conversion in jest-dom's repo, but we should be sure of what we want before hand. Have you had discussions with them on this topic?

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