From 6cca517eb6ddd050e79e15a912fb83dcff012638 Mon Sep 17 00:00:00 2001 From: Vladimir Sheremet Date: Thu, 12 Jan 2023 10:48:41 +0100 Subject: [PATCH 1/2] fix: document.defaultView references the same window as the global one --- packages/vitest/src/integrations/env/utils.ts | 6 ++++++ test/core/test/dom.test.ts | 5 +++++ test/core/test/happy-dom.test.ts | 6 ++++++ 3 files changed, 17 insertions(+) diff --git a/packages/vitest/src/integrations/env/utils.ts b/packages/vitest/src/integrations/env/utils.ts index 1b1a08de1a2a..37e562a9a8b7 100644 --- a/packages/vitest/src/integrations/env/utils.ts +++ b/packages/vitest/src/integrations/env/utils.ts @@ -72,6 +72,12 @@ export function populateGlobal(global: any, win: any, options: PopulateOptions = if (global.global) global.global = global + Object.defineProperty(global.document, 'defaultView', { + get: () => global, + enumerable: true, + configurable: true, + }) + skipKeys.forEach(k => keys.add(k)) return { diff --git a/test/core/test/dom.test.ts b/test/core/test/dom.test.ts index 261cf1d5cf92..27fcd7b977fa 100644 --- a/test/core/test/dom.test.ts +++ b/test/core/test/dom.test.ts @@ -130,10 +130,15 @@ it('can call global functions without window works as expected', async () => { }) it('globals are the same', () => { + expect(window).toBe(globalThis) + expect(window).toBe(global) expect(window.globalThis).toBe(globalThis) expect(window.Blob).toBe(globalThis.Blob) expect(window.globalThis.Blob).toBe(globalThis.Blob) expect(Blob).toBe(globalThis.Blob) + expect(document.defaultView).toBe(window) + const el = document.createElement('div') + expect(el.ownerDocument.defaultView).toBe(globalThis) }) it('can extend global class', () => { diff --git a/test/core/test/happy-dom.test.ts b/test/core/test/happy-dom.test.ts index 5dbb079a6fda..516ea02ebdd5 100644 --- a/test/core/test/happy-dom.test.ts +++ b/test/core/test/happy-dom.test.ts @@ -102,8 +102,14 @@ it('can call global functions without window works as expected', async () => { }) it('globals are the same', () => { + expect(window).toBe(globalThis) + expect(window).toBe(global) expect(window.globalThis).toBe(globalThis) expect(window.Blob).toBe(globalThis.Blob) expect(window.globalThis.Blob).toBe(globalThis.Blob) expect(Blob).toBe(globalThis.Blob) + expect(document.defaultView).toBe(window) + expect(document.defaultView).toBe(globalThis) + const el = document.createElement('div') + expect(el.ownerDocument.defaultView).toBe(globalThis) }) From a9002dbf88d0152371c7e68d6c921733a91c5724 Mon Sep 17 00:00:00 2001 From: Vladimir Sheremet Date: Thu, 12 Jan 2023 11:02:31 +0100 Subject: [PATCH 2/2] chore: add a guard --- packages/vitest/src/integrations/env/utils.ts | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/packages/vitest/src/integrations/env/utils.ts b/packages/vitest/src/integrations/env/utils.ts index 37e562a9a8b7..230f1f2bbd55 100644 --- a/packages/vitest/src/integrations/env/utils.ts +++ b/packages/vitest/src/integrations/env/utils.ts @@ -72,11 +72,14 @@ export function populateGlobal(global: any, win: any, options: PopulateOptions = if (global.global) global.global = global - Object.defineProperty(global.document, 'defaultView', { - get: () => global, - enumerable: true, - configurable: true, - }) + // rewrite defaultView to reference the same global context + if (global.document && global.document.defaultView) { + Object.defineProperty(global.document, 'defaultView', { + get: () => global, + enumerable: true, + configurable: true, + }) + } skipKeys.forEach(k => keys.add(k))