Invariant Violation: Could not find “store” in either the context or props of “Connect(SportsDatabase)”

后端 未结 9 1622
别那么骄傲
别那么骄傲 2020-12-12 10:36

Full code here: https://gist.github.com/js08/0ec3d70dfda76d7e9fb4

Hi,

  • I have an application where it shows different templates for desktop and mobile
相关标签:
9条回答
  • 2020-12-12 10:49

    jus do this import { shallow, mount } from "enzyme";

    const store = mockStore({
      startup: { complete: false }
    });
    
    describe("==== Testing App ======", () => {
      const setUpFn = props => {
        return mount(
          <Provider store={store}>
            <App />
          </Provider>
        );
      };
    
      let wrapper;
      beforeEach(() => {
        wrapper = setUpFn();
      });
    
    0 讨论(0)
  • 2020-12-12 10:52

    This happened to me when I upgraded. I had to downgrade back.

    react-redux ^5.0.6 → ^7.1.3

    0 讨论(0)
  • 2020-12-12 10:58

    When we put together a react-redux application we should expect to see a structure where at the top we have the Provider tag which has an instance of a redux store.

    That Provider tag then renders your parent component, lets call it the App component which in turn renders every other component inside the application.

    Here is the key part, when we wrap a component with the connect() function, that connect() function expects to see some parent component within the hierarchy that has the Provider tag.

    So the instance you put the connect() function in there, it will look up the hierarchy and try to find the Provider.

    Thats what you want to have happen, but in your test environment that flow is breaking down.

    Why?

    Why?

    When we go back over to the assumed sportsDatabase test file, you must be the sportsDatabase component by itself and then trying to render that component by itself in isolation.

    So essentially what you are doing inside that test file is just taking that component and just throwing it off in the wild and it has no ties to any Provider or store above it and thats why you are seeing this message.

    There is not store or Provider tag in the context or prop of that component and so the component throws an error because it want to see a Provider tag or store in its parent hierarchy.

    So that’s what that error means.

    0 讨论(0)
  • 2020-12-12 10:59

    Possible solution that worked for me with jest

    import React from "react";
    import { shallow } from "enzyme";
    import { Provider } from "react-redux";
    import configureMockStore from "redux-mock-store";
    import TestPage from "../TestPage";
    
    const mockStore = configureMockStore();
    const store = mockStore({});
    
    describe("Testpage Component", () => {
        it("should render without throwing an error", () => {
            expect(
                shallow(
                    <Provider store={store}>
                        <TestPage />
                    </Provider>
                ).exists(<h1>Test page</h1>)
            ).toBe(true);
        });
    });
    
    0 讨论(0)
  • 2020-12-12 11:08

    in my case just

    const myReducers = combineReducers({
      user: UserReducer
    });
    
    const store: any = createStore(
      myReducers,
      applyMiddleware(thunk)
    );
    

    shallow(<Login />, { context: { store } });

    0 讨论(0)
  • 2020-12-12 11:10

    For me it was import issue, hope it helps. default import by WebStorm was wrong.

    replace

    import connect from "react-redux/lib/connect/connect";
    

    with

    import {connect} from "react-redux";
    
    0 讨论(0)
提交回复
热议问题