Testing React: Target Container is not a DOM element Testing React: Target Container is not a DOM element javascript javascript

Testing React: Target Container is not a DOM element


For anyone else that was combing through the internet like I've been - looking for a solution to this when testing with Jest - I will back up the answer by @biphobe by saying Jest will cause this error to occur when you export something inside the same file that is calling ReactDOM.render.

In my case, I was exporting an object within my index.js where I was also calling ReactDOM.render. I removed this export and voila!


App.jsx is supposed to export the App class and do nothing more, render should be called elsewhere.

If you remove the render call from the App.jsx error should disappear, it pops up because the test environment doesn't supply the DOM with an app id.


As I see, this error arises in many cases and requires different approaches to solve it. My scenario is not the same as the example above, I use redux & router, although I was struggling with the same error. What helped me to solve this problem is to change index.js from:

ReactDOM.render(  <Provider store={store}>    <AppRouter />  </Provider>,  document.getElementById("root"));registerServiceWorker();

to:

ReactDOM.render(    (<Provider store={store}>        <AppRouter/>    </Provider>),     document.getElementById('root') || document.createElement('div') // for testing purposes);registerServiceWorker();