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: correctly restore globals in env teardown #1774

Merged
merged 2 commits into from Aug 2, 2022
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
3 changes: 2 additions & 1 deletion packages/vitest/src/integrations/env/utils.ts
Expand Up @@ -4,6 +4,7 @@ const allowRewrite = [
'Event',
'EventTarget',
'MessageEvent',
// implemented in Node 18
'ArrayBuffer',
]

Expand Down Expand Up @@ -41,7 +42,7 @@ export function populateGlobal(global: any, win: any, options: PopulateOptions =
const keys = getWindowKeys(global, win)

const originals = new Map<string | symbol, any>(
allowRewrite.map(([key]) => [key, global[key]]),
allowRewrite.filter(key => key in global).map(key => [key, global[key]]),
)

const overrideObject = new Map<string | symbol, any>()
Expand Down
15 changes: 15 additions & 0 deletions test/core/test/env-runtime.test.ts
@@ -0,0 +1,15 @@
import { populateGlobal } from 'vitest/src/integrations/env/utils'
import { expect, test, vi } from 'vitest'

test('returns valid globals', () => {
const globalEvent = vi.fn()
const winEvent = vi.fn()
const global = {
Event: globalEvent,
}
const win = { Event: winEvent }
const { originals } = populateGlobal(global, win)
expect(originals.get('Event')).toBe(globalEvent)
expect(win.Event).toBe(winEvent)
expect(global.Event).toBe(winEvent)
})