Skip to content

Commit

Permalink
fix: global Event should be equal to env Event (#793)
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va committed Feb 21, 2022
1 parent a80ce42 commit 6ca343a
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 6 deletions.
5 changes: 2 additions & 3 deletions packages/vitest/src/integrations/env/happy-dom.ts
@@ -1,15 +1,14 @@
import { importModule } from 'local-pkg'
import type { Environment } from '../../types'
import { KEYS } from './jsdom-keys'
import { getWindowKeys } from './utils'

export default <Environment>({
name: 'happy-dom',
async setup(global) {
const { Window } = await importModule('happy-dom') as typeof import('happy-dom')
const win: any = new Window()

const keys = new Set(KEYS.concat(Object.getOwnPropertyNames(win))
.filter(k => !k.startsWith('_') && !(k in global)))
const keys = getWindowKeys(global, win)

const overrideObject = new Map<string, any>()
for (const key of keys) {
Expand Down
5 changes: 2 additions & 3 deletions packages/vitest/src/integrations/env/jsdom.ts
@@ -1,6 +1,6 @@
import { importModule } from 'local-pkg'
import type { Environment, JSDOMOptions } from '../../types'
import { KEYS } from './jsdom-keys'
import { getWindowKeys } from './utils'

export default <Environment>({
name: 'jsdom',
Expand Down Expand Up @@ -40,8 +40,7 @@ export default <Environment>({
},
)

const keys = new Set(KEYS.concat(Object.getOwnPropertyNames(dom.window))
.filter(k => !k.startsWith('_') && !(k in global)))
const keys = getWindowKeys(global, dom.window)

const overrideObject = new Map<string, any>()
for (const key of keys) {
Expand Down
18 changes: 18 additions & 0 deletions packages/vitest/src/integrations/env/utils.ts
@@ -0,0 +1,18 @@
import { KEYS } from './jsdom-keys'

const allowRewrite = new Set([
'Event',
])

export function getWindowKeys(global: any, win: any) {
const keys = new Set(KEYS.concat(Object.getOwnPropertyNames(win))
.filter((k) => {
if (k.startsWith('_')) return false
if (k in global)
return allowRewrite.has(k)

return true
}))

return keys
}

0 comments on commit 6ca343a

Please sign in to comment.