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

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

Merged
merged 2 commits into from Jan 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions packages/vitest/src/integrations/env/utils.ts
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
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
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)
})