Created
March 7, 2016 19:35
-
-
Save jaketrent/24db1df6dca6929a3656 to your computer and use it in GitHub Desktop.
jsdom with localStorage
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
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