react-testing-library
react-testing-library copied to clipboard
fix: Run render queries in the render container only
What: This is a bugfix (as far as I can tell).
Why: render() by default appends a new container to a baseElement which is, by default, the global variable document.body. This container ends up containing the resulting markup for the system under test. However, when using the query methods on the RenderResult object, the whole document is scanned for matches, not just the markup resulting from the render() call. This breaks assumptions about the scope in which queries a run : one would expect the getByText and other queries to be scoped to that particular render.
As a matter of fact, this is what happens when the container parameter is given explicitly and baseElement takes it value, however this is not what happens otherwise. The default behaviour (global querying on all renders) is redundant with what screen from @testing-library/dom does, without being very clear about it being global.
How:
- a test reproducing the issue
- a lot of code reading to eventually just change a single function call parameter =)
Checklist:
- [x] Documentation added to the docs site (pull request)
- [x] Tests
- [x] TypeScript definitions updated
- [ ] Ready to be merged
This pull request is automatically built and testable in CodeSandbox.
To see build info of the built libraries, click here or the icon next to each commit SHA.
Latest deployment of this branch, based on commit dad164e12cf661b80d2176fcc80810a8be914643:
| Sandbox | Source |
|---|---|
| React | Configuration |
| react-testing-library-examples | Configuration |
Codecov Report
Merging #1097 (dad164e) into main (c80809a) will not change coverage. The diff coverage is
n/a.
@@ Coverage Diff @@
## main #1097 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 4 4
Lines 181 181
Branches 34 36 +2
=========================================
Hits 181 181
| Flag | Coverage Δ | |
|---|---|---|
| experimental | 100.00% <ø> (ø) |
|
| latest | 100.00% <ø> (ø) |
|
| next | 100.00% <ø> (ø) |
Flags with carried forward coverage won't be shown. Click here to find out more.
| Impacted Files | Coverage Δ | |
|---|---|---|
| src/pure.js | 100.00% <ø> (ø) |
Help us with your feedback. Take ten seconds to tell us how you rate us.
Closing as there hasn't been activity in a year. If this is still needed someone can push a fix for the review comment and ask us to reopen it.