I am trying to convert an existing project to using Typescript, and I am having problems setting up testing.
I had a setup file for my tests that installs jsdom so that all of my DOM interaction code would work during my tests. Using Typescript (ts-node with a wet), I always get errors like this:
Property 'window' does not exist on type 'Global'.
To prevent this, I tried fixing the NodeJS.Global interface as follows:
declare namespace NodeJS{ interface Global { document: Document; window: Window; navigator: Navigator; } }
But that didnโt change anything.
How to enable these browser properties in a global variable NodeJS?
Additionally:
This is my mocha setup.ts
:
import { jsdom, changeURL } from 'jsdom'; const exposedProperties = ['window', 'navigator', 'document']; global.document = jsdom(''); global.window = global.document.defaultView; Object.keys(global.document.defaultView).forEach((property) => { if (typeof global[property] === 'undefined') { exposedProperties.push(property); global[property] = global.document.defaultView[property]; } }); global.navigator = { userAgent: 'node.js', }; changeURL(global.window, 'http://example.com/');
source share