screen
is provided by @testing-library/dom
, which is what @testing-library/react
is built upon. When using the screen
methods, they will query within the html <body>
element, as described in the docs:
Because querying the entire document.body is very common, DOM Testing Library also exports a screen object which has every query that is pre-bound to document.body
render()
is only in @testing-library/react
. It returns an object similar to screen
and the default is to also bind the queries to the <body>
. By default, there is little difference, but you can customize its behavior by passing in options.
For example, you can specify an element other than the <body>
to query within, and can even provide custom query methods.
Update:
Read the link in the answer from @rrebase
The maintainers of the project now recommend using screen
, and they likely know much better than I do.
I am leaving my original recommendation for using render
below if you would still like to read it.
To answer your question of which is the best, I would say using render()
is better because the options
make it more flexible, but to quote the docs:
You won't often need to specify options
Still, my preference would be to use the methods provided by render()
, because if you ever decide to add in options, you wont need to remember to change all your queries.
keep the render call destructure up-to-date as you add/remove the queries you need
– Scrivener