我正在为一个新项目像往常一样用jest和Ezyme做一些单元测试。我曾经以这种方式测试连接到redux的组件:
a) 存储生成器
import { createStore } from 'redux';
import rootReducer from '../src/reducers';
export const storeFactory = (initialState) => {
return createStore(rootReducer, initialState);
}
import React from 'react';
import { shallow } from 'enzyme';
import { findByTestAttr,storeFactory } from '../../../test/testUtils';
import Input from './Input';
const setup = (initialState={}) => {
const store = storeFactory(initialState);
const wrapper = shallow(
<Input store={store} />
).dive();
console.log(wrapper.debug());
}
作为示例组件Input.js:
import React, { Component } from 'react';
import { connect } from 'react-redux';
class Input extends Component {
render(){
return <div />;
}
}
const mapStateToProps = (state) => {
return {};
}
export default connect(mapStateToProps)(Input);
我的npm软件包版本为:
"dependencies": {
"ajv": "^6.6.2",
"react": "^16.7.0",
"react-dom": "^16.7.0",
"react-redux": "^6.0.0",
"react-scripts": "2.1.3",
"redux": "^4.0.1"
}
"devDependencies": {
"check-prop-types": "^1.1.2",
"enzyme": "^3.8.0",
"enzyme-adapter-react-16": "^1.7.1",
"jest": "^23.6.0",
"jest-enzyme": "^7.0.1",
"prop-types": "^15.6.2"
}
这过去是可行的,但在测试执行报告上运行测试时,我收到了以下消息:
什么也不做。为特定的应用程序使用自定义Redux存储
组件,使用React.createContext()创建自定义React上下文,
并将上下文对象传递给React Redux的提供程序和特定
组件,如:。您还可以传递{上下文:
MyContext}选项来连接
我试图将上下文作为参数传递给shallow
const setup = (initialState={}) => {
const store = storeFactory(initialState);
const wrapper = shallow(
<Input />, { store }
);
console.log(wrapper.debug());
}
<ContextConsumer>
[function bound renderWrappedComponent]
</ContextConsumer>
如果我尝试使用enzyme dive()方法,我会得到:
const setup = (initialState={}) => {
const store = storeFactory(initialState);
const wrapper = shallow(
<Input />, { store }
).dive();
console.log(wrapper.debug());
}
测试套件无法运行
TypeError: ShallowWrapper::dive() can only be called on components
现在建议的做法是什么?我知道消息是怎么说的,但之前没有必要将元素包装到jest/Ezyme单元测试的提供者中。非常感谢你!