react-testing-library icon indicating copy to clipboard operation
react-testing-library copied to clipboard

fix: Run render queries in the render container only

Open Floby opened this issue 3 years ago • 2 comments

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

Floby avatar Jul 26 '22 17:07 Floby

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

codesandbox-ci[bot] avatar Jul 26 '22 17:07 codesandbox-ci[bot]

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.

codecov[bot] avatar Jul 26 '22 18:07 codecov[bot]

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.

nickserv avatar Sep 12 '23 01:09 nickserv