Testing React: Target Container is not a DOM element

前端 未结 5 910
悲哀的现实
悲哀的现实 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条回答
  •  -上瘾入骨i
    2020-12-16 10:16

    I found a solution for this error to my use case: Using the same Redux store React is using outside of React.

    In trying to export my React's Redux store from index.tsx to be used somewhere else outside of the React application, I was getting the same error while running Jest tests (which make use of Enzyme) in the App.tsx file.

    The error

    The initial code that didn't work when testing React looked like this.

    // index.tsx
    
    import * as React from "react";
    import { render } from "react-dom";
    import { Provider } from "react-redux";
    import { applyMiddleware, compose, createStore } from "redux";
    import App from "./components/App";
    import { rootReducer } from "./store/reducers";
    import { initialState } from "./store/state";
    
    const middlewares = [];
    
    export const store = createStore(
        rootReducer,
        initialState,
        compose(applyMiddleware(...middlewares)),
    );
    
    render(
        
            
        ,
        document.getElementById("root"),
    );
    

    The solution that worked for me

    Separate the Redux store logic into a new file named store.ts, then create a default export (to be used by index.tsx, i.e., the React application) and a non-default export with export const store (to be used from non-React classes), as follows.

    // store.ts
    
    import { applyMiddleware, compose, createStore } from "redux";
    import logger from "redux-logger";
    import { rootReducer } from "./store/reducers";
    import { initialState } from "./store/state";
    
    const middlewares = [];
    
    export const store = createStore(
        rootReducer,
        initialState,
        compose(applyMiddleware(...middlewares)),
    );
    
    export default store;
    
    // updated index.tsx
    
    import * as React from "react";
    import { render } from "react-dom";
    import { Provider } from "react-redux";
    import App from "./components/App";
    import store from "./store";
    
    render(
        
            
        ,
        document.getElementById("root"),
    );
    

    Using the Redux store in non-React classes

    // MyClass.ts
    
    import { store } from "./store"; // store.ts
    
    export default class MyClass {
      handleClick() {
        store.dispatch({ ...new SomeAction() });
      }
    }
    

    The default export

    A small note before you go. Here is how to use the default and the non-default exports.

    • default export store; is used with import store from "./store";
    • export const store = ... is used with import { store } from "./store";

    Hope this helps!

    https://nono.ma/says/solved-invariant-violation-target-container-is-not-a-dom-element

提交回复
热议问题