Skip to content

Commit

Permalink
fix: correctly restore globals in env teardown (#1774)
Browse files Browse the repository at this point in the history
* fix: correctly restore globals in env teardown

* test: more tests
  • Loading branch information
sheremet-va committed Aug 2, 2022
1 parent cbcba4f commit 90f4995
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
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)
})

0 comments on commit 90f4995

Please sign in to comment.