@@ -247,44 +247,66 @@ export {
247247You can then override and append the new queries via the render function by
248248passing a [ ` queries ` ] ( api.mdx#render-options ) option.
249249
250- If you want to add custom queries globally, you can do this by defining a custom
251- render method :
250+ If you want to add custom queries globally, you can do this by defining your customized
251+ ` render ` , ` screen ` and ` within ` methods :
252252
253253 <Tabs groupId = " test-utils" defaultValue = " jsx" values = { [ {label: ' Javascript' ,
254254 value: ' jsx' }, {label: ' Typescript' , value: ' tsx' }, ]} >
255255
256256 <TabItem value = " jsx" >
257257
258258``` js title="test-utils.js"
259- import {render , queries } from ' @testing-library/react'
259+ import {render , queries , within } from ' @testing-library/react'
260260import * as customQueries from ' ./custom-queries'
261261
262+ const allQueries = {
263+ ... queries,
264+ ... customQueries,
265+ }
266+
267+ const customScreen = within (document .body , allQueries)
268+ const customWithin = element => within (element, allQueries)
262269const customRender = (ui , options ) =>
263- render (ui, {queries: { ... queries, ... customQueries} , ... options})
270+ render (ui, {queries: allQueries , ... options})
264271
265272// re-export everything
266273export * from ' @testing-library/react'
267274
268275// override render method
269- export {customRender as render }
276+ export {
277+ customScreen as screen ,
278+ customWithin as within ,
279+ customRender as render ,
280+ }
270281```
271282
272283 </TabItem >
273284
274285 <TabItem value = " tsx" >
275286
276287``` ts title="test-utils.ts"
277- import {render , queries , RenderOptions } from ' @testing-library/react'
288+ import {render , queries , within , RenderOptions } from ' @testing-library/react'
278289import * as customQueries from ' ./custom-queries'
279290import {ReactElement } from ' react'
280291
292+ const allQueries = {
293+ ... queries ,
294+ ... customQueries ,
295+ }
296+
297+ const customScreen = within (document .body , allQueries )
298+ const customWithin = (element : ReactElement ) => within (element , allQueries )
281299const customRender = (
282300 ui : ReactElement ,
283301 options ? : Omit <RenderOptions , ' queries' >,
284- ) => render (ui , {queries: { ... queries , ... customQueries } , ... options })
302+ ) => render (ui , {queries: allQueries , ... options })
285303
286304export * from ' @testing-library/react'
287- export {customRender as render }
305+ export {
306+ customScreen as screen ,
307+ customWithin as within ,
308+ customRender as render ,
309+ }
288310```
289311
290312 </TabItem >
0 commit comments