Unmount / destroy Component in jsdom test

treznik picture treznik · May 31, 2014 · Viewed 19.5k times · Source

Is there a way to unmount and garbage collect a React Component that was mounted using TestUtils.renderIntoDocument inside a jsdom test?

I'm trying to test something that happens on componentWillUnmount and TestUtils.renderIntoDocument doesn't return any DOM node to call React. unmountComponentAtNode on.

Answer

Sophie Alpert picture Sophie Alpert · May 31, 2014

No, but you can simply use ReactDOM.render manually:

var container = document.createElement('div');
ReactDOM.render(<Component />, container);
// ...
ReactDOM.unmountComponentAtNode(container);

This is exactly what ReactTestUtils does anyway, so there's no reason not to do it this way if you need a reference to the container.