testing-library / testing-library/dom-testing-library
getByRole doesn't normalize the node text
Nobody has claimed this yet.
- Dominant language
- JavaScript
- Stars
- 3.3k
- Forks
- 474
- PR merge metrics
- No merged PRs in 30d
Description
@testing-library/domversion: 7.29.4- Testing Framework and version: jest 26.6.0 (react-scripts 4.0.2)
- DOM Environment: jsdom 16.4.0
Relevant code or config:
import { render, screen } from "@testing-library/react";
import "@testing-library/jest-dom/extend-expect";
it("doesn't work", () => {
render(<button>Click me</button>);
expect(
screen.getByRole(
"button",
{ name: "Click me" }
)
).toBeInTheDocument();
});
What you did:
Tried querying by role. The component text contains
What happened:
Unable to find an accessible element with the role "button" and name "Click me"
Here are the accessible roles:
button:
Name "Click me":
<button />
--------------------------------------------------
<body>
<div>
<button>
Click me
</button>
</div>
</body>
Reproduction:
https://codesandbox.io/s/billowing-sound-xtnjm?file=/src/role.test.js
Problem description:
This contradicts the documentation
Before running any matching logic against text in the DOM, DOM Testing Library automatically normalizes that text. By default, normalization consists of trimming whitespace from the start and end of text, and collapsing multiple adjacent whitespace characters into a single space.
Suggested solution:
should probably be
matchNormalizer,
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
Start in src/queries/role.js around the referenced line and compare the role query's text matching with the normalization behavior described in the documentation. Use the provided reproduction to verify that getByRole handles the non-breaking space consistently with the documented normalization rules.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript
- Domain
- testing-qa
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 45/100