Skip to content

getByText not working for multiline text #1160

Open
@sbland

Description

@sbland
  • @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

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