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

Error when Component render function returns different type of root element #853

Closed
mocheng opened this issue Mar 22, 2017 · 1 comment
Closed

Comments

@mocheng
Copy link

mocheng commented Mar 22, 2017

I found this issue when unit testing a server-side-only like component. To make it simple, below code snippet demo the problem.

    class DummyComponent extends React.Component {
      constructor() {
        super(...arguments);

        this.state = {
          ready: false
        };
      }

      componentDidMount() {
        this.setState({ready: true});
      }

      render() {
         // first render returns <div/>;
         // re-render after mount will return <span/>
         return this.state.ready ? <span/> : <div/>;
      }
    }

    const wrapper = mount(<DummyComponent />);

The mount method report error like this.

Invariant Violation: dangerouslyReplaceNodeWithMarkup(...): Cannot render markup in a worker thread. Make sure `window` and `document` are available globally before requiring React when unit testing or use ReactDOMServer.renderToString() for server rendering.

Though the error message reads lacking of window and document. It is not the root cause. I have jsdom-global set up.

If the render function changes to return same type on re-render. The unit testing works fine.

      render() {
        return this.state.ready ? <div>hello</div> : <div>world</div>;
      }

It should be valid for one component to return different type of root element in re-render. I tried in in live app and it works. So, there must be something different in mount.

@mocheng
Copy link
Author

mocheng commented Mar 23, 2017

It turns out not a issue of enzyme, but chai-enzyme enzymejs/chai-enzyme#158

@mocheng mocheng closed this as completed Mar 23, 2017
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

1 participant