jeudi 6 octobre 2016

React Snapshot testing with jest - Failed prop type: Invalid prop `children` of type `string` supplied

I am trying to test a pure react component.

Component

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 Snapshot Test

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();
    });
});

When I execute the test I get below error.

 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

I can understand that is says Props.children is invalid. How can I mock props.children? Or is there some other way test such components

Aucun commentaire:

Enregistrer un commentaire