dom-testing-library icon indicating copy to clipboard operation
dom-testing-library copied to clipboard

errror when using `getAllByText` with a function matcher

Open benmonro opened this issue 5 years ago • 1 comments

  • @testing-library/dom version: 7.24.1
  • Testing Framework and version: "@testing-library/dom": "^7.24.1", "@testing-library/jest-dom": "^5.11.4", "@testing-library/react": "^11.0.2", "@testing-library/user-event": "^12.1.3",
  • DOM Environment: jest

Relevant code or config:

expect(
      screen.getAllByText(
        (_content, el) =>
          el.textContent === "foo bar baz"
      )[0]
    ).toBeInTheDocument();

What you did:

ran the tests

What happened:

got an error:

TypeError: Cannot read property 'queryName' of undefined

Reproduction:

Problem description:

getAllByText has this error, but getByText does not. however getByText will fail when there are more than 1 element returned by the matcher.

Suggested solution:

benmonro avatar Oct 22 '20 17:10 benmonro

Hi @benmonro :),

I have tried by this small example and I'm not having any issue. Could you please provide more info about your test?

renderIntoDocument(`
  <div>
    <span>foo bar baz</span>
    <span>foo bar baz</span>
    <span>foo bar baz</span>
  </div>
`)

expect(
   screen.getAllByText((_content, el) => el.textContent === 'foo bar baz'),
).toHaveLength(3)

marcosvega91 avatar Oct 24 '20 12:10 marcosvega91