我看过几个答案,但都建议将主要组件包装在提供程序中。
我已经做到了,但是错误仍然存在。
这是我的App.js组件
const App = ({ store }) =>
<Provider store={store}>
<div className="App">
<Users/>
</div>
</Provider>
我正在做一个非常简单的测试。第一次使用酶,
import React from 'react'
import Adapter from 'enzyme-adapter-react-16'
import Users from './'
import { shallow, configure } from 'enzyme'
configure({adapter: new Adapter()});
describe('First React component test with Enzyme', () => {
it('renders without crashing', () => {
shallow(<Users />);
});
});
错误是:
不变违反:在“ Connect(Users)”的上下文中找不到“ store”。将根组件包装在Provider中,或者将自定义的React上下文提供程序传递给Provider,并将相应的React上下文使用者传递给connect选项中的Connect(Users)。
答案 0 :(得分:1)
可能的解决方法如下:
import React from "react";
import { shallow } from "enzyme";
import { Provider } from "react-redux";
import configureMockStore from "redux-mock-store";
import Userfrom "../User";
const mockStore = configureMockStore();
const store = mockStore({});
describe('First React component test with Enzyme', () => {
it('renders without crashing', () => {
shallow(
<Provider store={store}>
<User/>
</Provider>
);
});
});