Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Calling setState in componentWillMount crashes skin-deep #32

Closed
nikcorg opened this issue Oct 27, 2015 · 2 comments
Closed

Calling setState in componentWillMount crashes skin-deep #32

nikcorg opened this issue Oct 27, 2015 · 2 comments

Comments

@nikcorg
Copy link

nikcorg commented Oct 27, 2015

We recently upgraded out React dependency to 0.14. To my surprise this caused some of our skin-deep powered tests to fail with TypeError: Cannot read property '_currentElement' of null.

The skin-deep test suite passes with flying colours in our setup, so I was a bit stumped. Eventually I found the cause of the error to be a call to setState in componentWillMount.

The error originates in react/lib/ReactCompositeComponent, which is why I'm unsure of whether I should be reporting it here or rather to the React project. If I'm in the wrong place, speak up and I will move on.

See a failing test below this line.

import React, { Component } from "react"; // 0.14
import test from "tape";
import sd from "skin-deep";

class FailingComp extends Component {
    constructor(props) {
        super(props);
        this.state = {};
    }
    componentWillMount() {
        // comment the line below to pass the test
        this.setState({ hello: "World!" });
    }
    render() {
        const { hello = "World" } = this.state;
        return <p>{`Hello ${hello}!`}</p>;
    }
}

test("component", t => {
    const render = (props = {}) => {
        const tree = sd.shallowRender(React.createElement(FailingComp, props));
        const inst = tree.getMountedInstance();
        const vdom = tree.getRenderOutput();

        return { tree, inst, vdom };
    };

    const { vdom } = render();

    t.equal(vdom.type, "p", "renders p-tag");
    t.deepEqual(vdom.props.children, "Hello World!", "contains the text 'Hello World!'");
    t.end();
});
@glenjamin
Copy link
Owner

This is a bug in React itself, see facebook/react#4019 (comment) for a workaround

@nikcorg
Copy link
Author

nikcorg commented Oct 27, 2015

Though it might be. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants