testing-library / testing-library/react-testing-library
getByText not working for multiline text
Open
Nobody has claimed this yet.
- Dominant language
- JavaScript
- Stars
- 19.7k
- Forks
- 1.2k
- Avg merge
- 3d 16h
- Merged PRs (30d)
- 1
Description
@testing-library/reactversion: 13.3.0- Testing Framework and version:jest@27.5.1
- DOM Environment:jsdom@16.7.0 jest-environment-jsdom@^27.5.1
Using multiline text in getByText fails. Is there a workaround for this?
Failing test
const exampleText = `
hello
world
`
const textElement = screen.getByText(
exampleText.slice(0, 10),
{ exact: false }
);
console.info(textElement.textContent === exampleText); // == True
expect(screen.getByText(exampleText)).toBeInTheDocument(); // == Fail
Passing test
const exampleText = `hello world`
const textElement = screen.getByText(
exampleText.slice(0, 10),
{ exact: false }
);
console.info(textElement.textContent === exampleText); // == True
expect(screen.getByText(exampleText)).toBeInTheDocument(); // == Pass
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Reproduce the failing and passing JavaScript examples using the getByText query entry point. The issue template directs query API work to the dom-testing-library repository, so begin there rather than this React repository. Done means the multiline getByText assertion succeeds while preserving the passing single-line behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript, react
- Domain
- testing-qa
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100