Nodejs doesn't have window
object (obviously, not?). So, when you want to test pure Javascript or Typescript code that have window
dependencies like localStorage
and many more that are only able on browsers, your tests will not works, because you will get the XXXX is not defined
error.
So, this library was designed to use in Node testing environment with mocha, chai, etc. where have browsers APIs dependencies. Some features of browsers was don't do what do you want like document.createElement('div')
, but will returns you an object where you have access to properties and functions.
If you don't use a window
polyfill you will get an error like this:
window is not defined
First install as dev dependency, never use in production server:
npm i @videsk/window-node-polyfill -D
Only need add on teh top of the test file, like this:
require('@videsk/window-node-polyfill');
...
Then you can access to the current properties:
window
window.localStorage
window.sessionStorage
document
document.createElement
document.body
document.body.appendChild
window.location
window.top
window.parent
// window.localStorage or window.sessionStorage
window.localStorage.getItem('my-key');
window.localStorage.setItem('my-key', 'my-value');
window.localStorage.removeItem('my-key');
const element = document.createElement('div');
typeof element // object
If you want add more properties, you're welcome, so send you PR or issue with feature request.
LGPL-2.1 License - By Videsk™