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: global Event should be equal to env Event #793

Merged
merged 7 commits into from Feb 21, 2022
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
}