testing-library / testing-library/react-testing-library

getByText not working for multiline text

Open
#1,160 2 comments 2 reactions 0 assignees View on GitHub

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/react version: 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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.