Skip to content

Instantly share code, notes, and snippets.

@npryce
Last active September 10, 2015 13:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save npryce/21468c7a0795280b1cf6 to your computer and use it in GitHub Desktop.
Save npryce/21468c7a0795280b1cf6 to your computer and use it in GitHub Desktop.
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