当我从我的反应应用程序运行npm test
时,我收到此错误,而我不确定如何修复它。它告诉我:
将根组件包装在
<Provider>
中,或显式传递 “存储”作为“连接(应用)”的支柱
我的根文件已经包含在Provider:
中import React from "react";
import ReactDOM from "react-dom";
import { Provider } from "react-redux";
import { createStore, applyMiddleware } from "redux";
import reduxThunk from "redux-thunk";
import App from "./App";
import reducers from "./reducers";
const store = createStore(reducers, {}, applyMiddleware(reduxThunk));
ReactDOM.render(
<Provider store={store}>
<App />
</Provider>,
document.querySelector("#root")
);
在我的App.js文件中,我有这个:
import React, { Component } from "react";
import { BrowserRouter, Route } from "react-router-dom";
import { connect } from "react-redux";
import * as actions from "./actions";
import LoginPage from "./components/auth/LoginPage";
import Signup from "./components/auth/Signup";
import Header from "./components/header/Header";
import HomePage from "./components/home/HomePage";
class App extends Component {
componentDidMount() {
this.props.fetchUser();
}
render() {
return (
<div>
<BrowserRouter>
<div>
<Header />
<Route path="/" exact component={HomePage} />
<Route path="/login" component={LoginPage} />
<Route path="/signup" component={Signup} />
</div>
</BrowserRouter>
</div>
);
}
}
export default connect(null, actions)(App);
错误:
npm test
> my-app@0.1.0 test /var/jenkins_home/workspace/...
> react-scripts test --env=jsdom
FAIL src/App.test.js
● renders without crashing
Invariant Violation: Could not find "store" in either the context or props of "Connect(App)". Either wrap the root component in a <Provider>, or explicitly pass "store" as a prop to "Connect(App)".
我的App.test.js
文件:
import React from "react";
import ReactDOM from "react-dom";
import App from "./App";
it("renders without crashing", () => {
const div = document.createElement("div");
ReactDOM.render(<App />, div);
});
当我使用npm start
在本地运行此应用程序时,不会抛出任何错误,并且redux工作正常。发生了什么事?
注意:如果更改了某些内容,我的Jenkins / BlueOcean服务器管道中会运行npm test