在react-redux连接的组件中进行单元测试时出错

时间:2019-01-07 14:30:17

标签: reactjs react-redux jestjs enzyme connected-components

我正在尝试使用jest酶测试连接的组件(react-redux)。我正在使用react-redux-mock存储。当我运行测试以在组件中找到一个div时,就会出现此错误。

Invariant Violation: Passing redux store in props has been removed and does not do anything. To use a custom Redux store for specific components, create a custom React context with React.createContext(), and pass the context object to React-Redux's Provider and specific components like: <Provider context={MyContext}><ConnectedComponent context={MyContext} /></Provider>. You may also pass a {context : MyContext} option to connect

我确实安装并测试了组件,但没有重新安装它,但是我想做一个>浅层测试。

describe("Input Component", () => {
let wrapper;
let store;
beforeEach(() => {
    store = mockStore(initialState);

    wrapper = shallow(<Input store={store} />);
});

it("should rendder without error", () => {
    expect(wrapper.find("div")).toHaveLength(1);
});
});

1 个答案:

答案 0 :(得分:0)

如何导入组件?

例如,如果您是使用Import App从'./yourpath/App'导入的,那么您实际上是在保存connect()返回的包装器组件,而不是App组件本身。

为了能够测试App组件本身而无需处理装饰器,您还必须导出未修饰的组件:

import { connect } from 'react-redux'

// Use named export for unconnected component (for tests)
export class App extends Component {
/* ... */
}

// Use default export for the connected component (for app)
export default connect(mapStateToProps)(App)

将其导入到您的测试文件中,如下所示:

import { App } from './yourpath/App'