From 7f57469f2e6f54279e31c95a53ee3f497ebdeff6 Mon Sep 17 00:00:00 2001 From: Matt Date: Sun, 3 Nov 2024 22:09:27 +0100 Subject: [PATCH] fix: [#1546] Use `globalThis` instead of `global` to make Happy DOM work in other runtimes such as Cloudflare workers (#1546) `global` is a legacy, node.js-only global variable. I think `globalThis` this should be used instead, which will make global-registrator portable on other runtimes like Cloudflare workers. --- .../src/GlobalRegistrator.ts | 24 +++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/packages/global-registrator/src/GlobalRegistrator.ts b/packages/global-registrator/src/GlobalRegistrator.ts index 1392216c2..5796bbc29 100644 --- a/packages/global-registrator/src/GlobalRegistrator.ts +++ b/packages/global-registrator/src/GlobalRegistrator.ts @@ -28,7 +28,7 @@ export default class GlobalRegistrator { throw new Error('Failed to register. Happy DOM has already been globally registered.'); } - const window = new GlobalWindow({ ...options, console: global.console }); + const window = new GlobalWindow({ ...options, console: globalThis.console }); this.registered = {}; @@ -38,7 +38,7 @@ export default class GlobalRegistrator { for (const key of Object.keys(propertyDescriptors)) { if (!IGNORE_LIST.includes(key)) { const windowPropertyDescriptor = propertyDescriptors[key]; - const globalPropertyDescriptor = Object.getOwnPropertyDescriptor(global, key); + const globalPropertyDescriptor = Object.getOwnPropertyDescriptor(globalThis, key); if ( globalPropertyDescriptor?.value === undefined || @@ -48,11 +48,11 @@ export default class GlobalRegistrator { // If the property is the window object, replace it with the global object if (windowPropertyDescriptor.value === window) { - window[key] = global; - windowPropertyDescriptor.value = global; + window[key] = globalThis; + windowPropertyDescriptor.value = globalThis; } - Object.defineProperty(global, key, { + Object.defineProperty(globalThis, key, { ...windowPropertyDescriptor, configurable: true }); @@ -69,18 +69,18 @@ export default class GlobalRegistrator { // If the property is the window object, replace it with the global object if (propertyDescriptor.value === window) { - window[key] = global; - propertyDescriptor.value = global; + window[key] = globalThis; + propertyDescriptor.value = globalThis; } - Object.defineProperty(global, key, { + Object.defineProperty(globalThis, key, { ...propertyDescriptor, configurable: true }); } // Set owner window on document to global - global.document[PropertySymbol.defaultView] = global; + globalThis.document[PropertySymbol.defaultView] = globalThis; } /** @@ -93,13 +93,13 @@ export default class GlobalRegistrator { ); } - const happyDOM = global.happyDOM; + const happyDOM = globalThis.happyDOM; for (const key of Object.keys(this.registered)) { if (this.registered[key] !== null) { - Object.defineProperty(global, key, this.registered[key]); + Object.defineProperty(globalThis, key, this.registered[key]); } else { - delete global[key]; + delete globalThis[key]; } }