Testing React: Target Container is not a DOM element

前端 未结 5 914
悲哀的现实
悲哀的现实 2020-12-16 09:27

I\'m attempting to test a React component with Jest/Enzyme while using Webpack.

I have a very simple test @

import React from \'react\';
import { sha         


        
5条回答
  •  轮回少年
    2020-12-16 10:31

    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(
      
        
      ,
      document.getElementById("root")
    );
    registerServiceWorker();
    

    to:

    ReactDOM.render(
        (
            
        ),
         document.getElementById('root') || document.createElement('div') // for testing purposes
    );
    registerServiceWorker();
    

提交回复
热议问题