【发布时间】:2017-07-14 19:16:15
【问题描述】:
我正在尝试测试一个纯反应组件。
组件
import React, {Component} from 'react';
class App extends Component {
constructor (props){
super(props);
props.init();
}
render() {
return (
<div className="container-wrapper">
{this.props.children}
</div>
);
}
}
App.propTypes = {
init : React.PropTypes.func,
children : React.PropTypes.element,
};
export default App;
Jest 快照测试
import React from 'react';
import App from 'app/main/components/App';
import renderer from 'react-test-renderer';
jest.mock('react-dom');
const blank = jest.fn();
describe('App', () => {
it('Renders App', () => {
const component = renderer.create(<App init={blank}> </App>);
const tree = component.toJSON();
expect(tree).toMatchSnapshot();
});
});
当我执行测试时,出现以下错误。
console.error node_modules/fbjs/lib/warning.js:36
Warning: Failed prop type: Invalid prop `children` of type `string` supplied to `App`, expected a single ReactElement.
in App
我可以理解是说 Props.children 无效。我如何模拟 props.children?或者有没有其他方法测试这些组件
【问题讨论】:
标签: unit-testing testing reactjs snapshot jestjs