Skip to content

Commit 1ac4bb8

Browse files
authoredJan 12, 2023
fix: document.defaultView references the same window as the global one (#2649)
* fix: document.defaultView references the same window as the global one * chore: add a guard
1 parent 2daf44a commit 1ac4bb8

File tree

3 files changed

+20
-0
lines changed

3 files changed

+20
-0
lines changed
 

‎packages/vitest/src/integrations/env/utils.ts

+9
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,15 @@ export function populateGlobal(global: any, win: any, options: PopulateOptions =
7272
if (global.global)
7373
global.global = global
7474

75+
// rewrite defaultView to reference the same global context
76+
if (global.document && global.document.defaultView) {
77+
Object.defineProperty(global.document, 'defaultView', {
78+
get: () => global,
79+
enumerable: true,
80+
configurable: true,
81+
})
82+
}
83+
7584
skipKeys.forEach(k => keys.add(k))
7685

7786
return {

‎test/core/test/dom.test.ts

+5
Original file line numberDiff line numberDiff line change
@@ -130,10 +130,15 @@ it('can call global functions without window works as expected', async () => {
130130
})
131131

132132
it('globals are the same', () => {
133+
expect(window).toBe(globalThis)
134+
expect(window).toBe(global)
133135
expect(window.globalThis).toBe(globalThis)
134136
expect(window.Blob).toBe(globalThis.Blob)
135137
expect(window.globalThis.Blob).toBe(globalThis.Blob)
136138
expect(Blob).toBe(globalThis.Blob)
139+
expect(document.defaultView).toBe(window)
140+
const el = document.createElement('div')
141+
expect(el.ownerDocument.defaultView).toBe(globalThis)
137142
})
138143

139144
it('can extend global class', () => {

‎test/core/test/happy-dom.test.ts

+6
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,14 @@ it('can call global functions without window works as expected', async () => {
102102
})
103103

104104
it('globals are the same', () => {
105+
expect(window).toBe(globalThis)
106+
expect(window).toBe(global)
105107
expect(window.globalThis).toBe(globalThis)
106108
expect(window.Blob).toBe(globalThis.Blob)
107109
expect(window.globalThis.Blob).toBe(globalThis.Blob)
108110
expect(Blob).toBe(globalThis.Blob)
111+
expect(document.defaultView).toBe(window)
112+
expect(document.defaultView).toBe(globalThis)
113+
const el = document.createElement('div')
114+
expect(el.ownerDocument.defaultView).toBe(globalThis)
109115
})

0 commit comments

Comments
 (0)
Please sign in to comment.