error ‘document’ is not defined : eslint / React
Add “browser”: true your env to include global variables (like document, window, etc.) See the ESLint environment documentation for more information.
Add “browser”: true your env to include global variables (like document, window, etc.) See the ESLint environment documentation for more information.
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” ],
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