Skip to content

Instantly share code, notes, and snippets.

@npryce
Last active Sep 10, 2015
Embed
What would you like to do?
Testing react components with Mocha and NodeJS
// React decides if it has a DOM on loading, so if we always need to
// initialise JSDOM before loading it.
var testdom = require('testdom');
testdom('<html><body></body></html>');
// But React hates you even more than that... it caches the rendered component
// in the DOM and if the same type of component with the same key is rendered at
// into the same element, then it uses the cached instance, not the just rendered
// instance. Even if the props are different. So the instance being tested will
// not be exercised.
//
// Therefore we have to create a new element to render into for each test.
beforeEach(function() {
document.body.innerHTML = "<div id='test'></div>";
});
module.exports = require('react');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment