diff --git a/packages/vitest/src/integrations/env/jsdom-keys.ts b/packages/vitest/src/integrations/env/jsdom-keys.ts index c349f95c1c95..5dfaf042a8aa 100644 --- a/packages/vitest/src/integrations/env/jsdom-keys.ts +++ b/packages/vitest/src/integrations/env/jsdom-keys.ts @@ -20,6 +20,7 @@ const LIVING_KEYS = [ 'Comment', 'DocumentType', 'NodeList', + 'RadioNodeList', 'HTMLCollection', 'HTMLOptionsCollection', 'DOMStringMap', @@ -96,6 +97,7 @@ const LIVING_KEYS = [ 'HTMLVideoElement', 'HTMLAudioElement', 'HTMLTrackElement', + 'HTMLFormControlsCollection', 'SVGElement', 'SVGGraphicsElement', 'SVGSVGElement', @@ -126,6 +128,7 @@ const LIVING_KEYS = [ 'Location', 'History', 'Screen', + 'Crypto', 'Performance', 'Navigator', 'PluginArray', @@ -159,6 +162,7 @@ const LIVING_KEYS = [ 'Headers', 'AbortController', 'AbortSignal', + 'ArrayBuffer', // not specified in docs, but is available 'Image', diff --git a/packages/vitest/src/integrations/env/utils.ts b/packages/vitest/src/integrations/env/utils.ts index f591836114e1..1b1a08de1a2a 100644 --- a/packages/vitest/src/integrations/env/utils.ts +++ b/packages/vitest/src/integrations/env/utils.ts @@ -1,15 +1,5 @@ import { KEYS } from './jsdom-keys' -const allowRewrite = [ - 'Event', - 'EventTarget', - 'MessageEvent', - // implemented in Node 18 - 'ArrayBuffer', - // implemented in Node 18 - 'Blob', -] - const skipKeys = [ 'window', 'self', @@ -23,7 +13,7 @@ export function getWindowKeys(global: any, win: any) { if (skipKeys.includes(k)) return false if (k in global) - return allowRewrite.includes(k) + return KEYS.includes(k) return true })) @@ -47,9 +37,7 @@ export function populateGlobal(global: any, win: any, options: PopulateOptions = const { bindFunctions = false } = options const keys = getWindowKeys(global, win) - const originals = new Map( - allowRewrite.filter(key => key in global).map(key => [key, global[key]]), - ) + const originals = new Map() const overrideObject = new Map() for (const key of keys) { @@ -58,6 +46,9 @@ export function populateGlobal(global: any, win: any, options: PopulateOptions = && !isClassLikeName(key) && win[key].bind(win) + if (KEYS.includes(key) && key in global) + originals.set(key, global[key]) + Object.defineProperty(global, key, { get() { if (overrideObject.has(key))