From 6ca343aa5449f0f8fd9e85c03b56b173bcc78f3f Mon Sep 17 00:00:00 2001 From: Vladimir Date: Mon, 21 Feb 2022 08:58:03 +0300 Subject: [PATCH] fix: global Event should be equal to env Event (#793) --- .../vitest/src/integrations/env/happy-dom.ts | 5 ++--- packages/vitest/src/integrations/env/jsdom.ts | 5 ++--- packages/vitest/src/integrations/env/utils.ts | 18 ++++++++++++++++++ 3 files changed, 22 insertions(+), 6 deletions(-) create mode 100644 packages/vitest/src/integrations/env/utils.ts diff --git a/packages/vitest/src/integrations/env/happy-dom.ts b/packages/vitest/src/integrations/env/happy-dom.ts index 501f36666612..35d3ddc2e68e 100644 --- a/packages/vitest/src/integrations/env/happy-dom.ts +++ b/packages/vitest/src/integrations/env/happy-dom.ts @@ -1,6 +1,6 @@ import { importModule } from 'local-pkg' import type { Environment } from '../../types' -import { KEYS } from './jsdom-keys' +import { getWindowKeys } from './utils' export default ({ name: 'happy-dom', @@ -8,8 +8,7 @@ export default ({ 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() for (const key of keys) { diff --git a/packages/vitest/src/integrations/env/jsdom.ts b/packages/vitest/src/integrations/env/jsdom.ts index f865793f74a1..0e457b998473 100644 --- a/packages/vitest/src/integrations/env/jsdom.ts +++ b/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 ({ name: 'jsdom', @@ -40,8 +40,7 @@ export default ({ }, ) - 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() for (const key of keys) { diff --git a/packages/vitest/src/integrations/env/utils.ts b/packages/vitest/src/integrations/env/utils.ts new file mode 100644 index 000000000000..821b6846e385 --- /dev/null +++ b/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 +}