Skip to content

Instantly share code, notes, and snippets.

@andrewagain
Last active May 7, 2016 19:42
Show Gist options
  • Save andrewagain/4f95332a0ce3a0390f2a6b60787c06af to your computer and use it in GitHub Desktop.
Save andrewagain/4f95332a0ce3a0390f2a6b60787c06af to your computer and use it in GitHub Desktop.
jsdom with mocha
var jsdom = require('jsdom')
// setup the simplest document possible
var doc = jsdom.jsdom('<!doctype html><html><body></body></html>')
// get the window object out of the document
var win = doc.defaultView
// set globals for mocha that make access to document and window feel
// natural in the test environment
global.document = doc
global.window = win
// take all properties of the window object and also attach it to the
// mocha global object
propagateToGlobal(win)
// from mocha-jsdom https://github.com/rstacruz/mocha-jsdom/blob/master/index.js#L80
function propagateToGlobal (window) {
for (let key in window) {
if (!window.hasOwnProperty(key)) continue
if (key in global) continue
global[key] = window[key]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment