作者:mobiledu2502914617 | 来源:互联网 | 2022-12-06 10:02
我用Jest / Enzyme编写的测试用例遇到了麻烦。我有一个React / Redux组件,正在尝试编写一个基本测试,但是得到以下错误:
Invariant Violation: ReactShallowRenderer render(): Shallow rendering works only with custom components, but the provided element type was 'undefined'.
这是我的代码:
dashboardComponent.js
import '../stylesheets/dashboardComponent.css';
import React, { Component } from 'react';
import { connect } from 'react-redux';
import { bindActionCreators } from 'redux';
import * as dashboardActions from '../actions/dashboardActions';
class DashboardComponent extends Component {
constructor(props) {
super();
}
componentDidMount() {
this.props.actions.getDashboardContent();
}
render() {
return (
);
}
}
function mapStateToProps(state) {
return {
dashboardContent: state.dashboard.get('dashboardContent')
}
}
function mapDispatchToProps(dispatch) {
return {
actions: bindActionCreators(dashboardActions, dispatch)
};
}
export default connect(mapStateToProps, mapDispatchToProps)(DashboardComponent);
dashboardComponent.test.js
import React from 'react';
import { shallow } from 'enzyme';
import { DashboardComponent as Dashboard } from '../../components/dashboardComponent';
const wrapper = shallow();
describe('Dashboard', () => {
it('renders the Dashboard component', () => {
expect(wrapper).toMatchSnapshot();
});
});
我不确定为什么
在这里未定义。
1> Zaid Crouch..:
您当前正在将包装的组件导出为默认导出,但是要在测试中使用未包装的组件,您还需要将其导出为命名导出,即
export class DashboardComponent extends Component { ... }
export default connect(...)(DashboardComponent)