Last active
September 10, 2015 13:31
-
-
Save npryce/21468c7a0795280b1cf6 to your computer and use it in GitHub Desktop.
Testing react components with Mocha and NodeJS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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