Skip to content

Instantly share code, notes, and snippets.

@jaketrent
Created March 7, 2016 19:35
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 jaketrent/24db1df6dca6929a3656 to your computer and use it in GitHub Desktop.
Save jaketrent/24db1df6dca6929a3656 to your computer and use it in GitHub Desktop.
jsdom with localStorage
import jsdomGlobal from 'jsdom-global'
import jsdom from 'jsdom'
import localStorage from 'localStorage'
const doc = jsdom.jsdom('<!doctype html><html><body></body></html>')
const win = doc.defaultView
win.localStorage = localStorage
global.document = doc
global.window = win
jsdomGlobal()
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