testing-library / testing-library/react-testing-library
queries returned by render are not scoped
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: 14.1.2- Testing Framework and version: Jest 29.7
- DOM Environment: jsdom 20.0.3
Relevant code or config:
it('uses a consistent scope', () => {
const MyComponent = () => {
useEffect(() => {
const separateElement = document.createElement('div');
separateElement.textContent = 'hello';
document.body.append(separateElement);
return () => separateElement.remove();
}, []);
return (
<div>
<div>hello</div>
</div>
);
};
const { container, getByText } = render(<MyComponent />);
within(container).getByText('hello'); // passes
getByText('hello'); // fails (finds 2 elements)
});
What happened:
The first test (within(container).getByText('hello')) passes, and the second (getByText('hello')) fails:
Found multiple elements with the text: hello
Here are the matching elements:
Ignored nodes: comments, script, style
<div>
hello
</div>
Ignored nodes: comments, script, style
<div>
hello
</div>
Problem description:
The container and queries returned by render should be consistent with each other: the queries should search within the returned container by default, to avoid pollution from other tests and libraries which attach elements to other parts of the DOM. For users who need to check the wider document, they can continue to use screen.
It is also worth noting that the documentation claims that the queries are "bound", which does not match the current behaviour (since they apply globally across the document).
Suggested solution:
It is possible to make a wrapper function in user-space which works around this, which should be easy to integrate into the core library:
export const renderScoped = (ui: ReactElement, options?: RenderOptions) => {
const rendered = render(ui, options);
return {
...rendered,
...within(rendered.container),
};
};
This will be a potentially breaking change for users who currently rely on the queries not being scoped, so probably needs to be a "4.2" release.
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 by examining how render returns its queries and how within(container) binds queries. Reproduce the provided example, then verify that render's queries search only the returned container while screen can still search the wider document. The breaking-change and release implications need to be resolved before implementation.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript, react
- Domain
- frontend, testing-qa
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 35/100