1 year ago
#386509
Dave Meehan
Using react-testing-library without global-jsdom
I want to be able to setup specific instances of window and document for use by render(). I can specify the container by creating a specific instance of jsdom thus:
this.dom = new JSDOM('<body></body>')
const container = this.document().createElement('div')
render(
<Provider store={store}>
<MyComponent />
</Provider>,
{ container: this.dom.window.document.body.appendChild(container) }
)
If I remove global-jsdom/register from the setup, the render() call fails due to window
not being defined. It seems it wants a global object called window, yet the jsdom instance has one (dom.window
).
The reason for avoiding use of global window
and document
is that I want to be able to create separate instances to make sure that there is no shared data between instances (i.e. equivilent of two users making separate browser requests). I can't test for this if its using a global instance it seems.
I'm not using Jest for tests. It's still possible to use the getBy
... queries if you avoid using the versions bound to screen
, as the naked versions take the container element as the first argument, and this seems to work, but I'm concerned that the global and shared window element is going to make the tests brittle or just not work.
Looking at the source for RTL it seems that its just assuming window is a global and doesn't provide an alterantive, so wondering if anyone knows of a workaround or alternative.
react-testing-library
0 Answers
Your Answer