Skip to content

Commit

Permalink
fix: document.defaultView references the same window as the global one (
Browse files Browse the repository at this point in the history
#2649)

* fix: document.defaultView references the same window as the global one

* chore: add a guard
  • Loading branch information
sheremet-va authored Jan 12, 2023
1 parent 2daf44a commit 1ac4bb8
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 0 deletions.
9 changes: 9 additions & 0 deletions packages/vitest/src/integrations/env/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,15 @@ export function populateGlobal(global: any, win: any, options: PopulateOptions =
if (global.global)
global.global = global

// 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))

return {
Expand Down
5 changes: 5 additions & 0 deletions test/core/test/dom.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
6 changes: 6 additions & 0 deletions test/core/test/happy-dom.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})

0 comments on commit 1ac4bb8

Please sign in to comment.