Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use globalThis instead of global #1546

Merged
merged 2 commits into from
Nov 3, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions packages/global-registrator/src/GlobalRegistrator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {};

Expand All @@ -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 ||
Expand All @@ -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
});
Expand All @@ -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;
}

/**
Expand All @@ -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];
}
}

Expand Down
Loading