How to prevent “Property ‘…’ does not exist on type ‘Global'” with jsdom and typescript?

Original Answer To Avoid Error Put this at the top of your typescript file const globalAny:any = global; Then use globalAny instead. globalAny.document = jsdom(”); globalAny.window = global.document.defaultView; Updated Answer To Maintain Type Safety If you want to keep your type safety, you can augment the existing NodeJS.Global type definition. You need to put your … Read more

Mocking `document` in jest

Similar to what others have said, but instead of trying to mock the DOM yourself, just use JSDOM: // __mocks__/client.js import { JSDOM } from “jsdom” const dom = new JSDOM() global.document = dom.window.document global.window = dom.window Then in your jest config: “setupFiles”: [ “./__mocks__/client.js” ],