Skip to content

Instantly share code, notes, and snippets.

@banyudu
Last active November 20, 2019 08:51
Show Gist options
  • Save banyudu/3f11cc4e7c294b68766d80638ec75bf7 to your computer and use it in GitHub Desktop.
Save banyudu/3f11cc4e7c294b68766d80638ec75bf7 to your computer and use it in GitHub Desktop.
Browser polyfill in nodejs
const { JSDOM } = require('jsdom')
const { window } = new JSDOM('', {
url: 'http://localhost/'
})
global.window = window
for (let item of ['document', 'navigator', 'location', 'localStorage', 'sessionStorage']) {
global[item] = window[item]
}
// other custom properties, rename futureWindowProperties to what you need.
Object.defineProperty(window, 'futureWindowProperties', {
set: val => { global.futureWindowProperties = val },
get: () => global.futureWindowProperties
})
require('matchmedia-polyfill');
require('matchmedia-polyfill/matchMedia.addListener');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment