JestJS -> 不变违规:在 "Connect(Portfolio)" 的上下文或道具中找不到 "store"

JestJS -> Invariant Violation: Could not find "store" in either the context or props of "Connect(Portfolio)"

完整的错误信息:

Invariant Violation: Could not find "store" in either the context or props of "Connect(Portfolio)". Either wrap the root component in a , or explicitly pass "store" as a prop to "Connect(Portfolio)".

不确定为什么我的 Jest 测试会出现此错误,因为我的应用程序正在运行并且我可以通过调度操作更改我的状态。

index.js

import React from 'react'
import ReactDOM from 'react-dom'
import { createStore, applyMiddleware, compose } from 'redux'
import { Provider } from 'react-redux'
import thunk from 'redux-thunk'
import reducer from './reducer'
import App from './App'

const element = document.getElementById('coinhover');

const store = createStore(reducer, compose(
    applyMiddleware(thunk),
    window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__()
));

ReactDOM.render(
    <Provider store={ store }>
        <App />
    </Provider>, element);

组合组件

import React from 'react'
import { connect } from 'react-redux'
import SocialMediaFooter from '../common/SocialMediaFooter'
import AssetsTable from '../assetsTable/AssetsTable'
import local_coins from '../../coins.json'
import * as api from '../../services/api'

const mapStateToProps = ({ portfolio }) => ({
    portfolio
});

let localCoins = local_coins;

class Portfolio extends React.Component {
    constructor(props) {
        super(props)
        this.state = {
            loading: true,
            assets: props.portfolio,
            total: 0
        };
    }

    componentDidMount() {
        this.setState({ loading: false });
    }

    render() {
        const assets = this.state.assets;
        const total  = this.state.total;

        return (
            <div className="app-bg">
                <section className="portfolio">
                    <header>
                        <h1><span className="plus">+</span>COINHOVER</h1>
                        <h2>Watch your cryptocurrency asset balances in once place.</h2>
                        <em className="num">${ total }</em>
                    </header>
                    { this.state.loading ? (
                        <div className="loading">
                            <div className="loader"></div>
                            <span>Loading coin data...</span>
                        </div>
                    ) : (
                        <AssetsTable assets={ assets }/>
                    )}
                    <SocialMediaFooter />
                </section>
            </div>
        )
    }
}

export default connect(mapStateToProps, null)(Portfolio)

根据错误消息,您需要确保连接组件的测试实际上可以访问商店实例。所以,在你的测试代码中,你也应该使用<Provider store={store}><ConnectedPortfolio /></Provider>,或者<ConnectedPortfolio store={store} />。或者,您可以单独导出纯 Portfolio 组件,然后测试它,而不是连接版本。

参见Redux docs on testing for more info, as well as the articles on Redux testing approaches in my React/Redux links list

我 运行 遇到了同样的问题,我是这样解决的: 正如 redux 官方 docs 所建议的,最好将未连接的组件也导出。

为了能够在不处理装饰器的情况下测试 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)

由于默认导出仍然是装饰组件,上图中的导入语句将像以前一样工作,因此您不必更改应用程序代码。但是,您现在可以像这样在测试文件中导入未修饰的 App 组件:

// Note the curly braces: grab the named export instead of default export
import { App } from './App'

如果两者都需要:

import ConnectedApp, { App } from './App'

在应用本身中,您仍然可以正常导入它:

import App from './App'

您只能将命名导出用于测试。