Skip to content

Commit

Permalink
fix: always rewrite defined jsdom keys (#2290)
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va authored Nov 8, 2022
1 parent b1961f0 commit 922e525
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 14 deletions.
4 changes: 4 additions & 0 deletions packages/vitest/src/integrations/env/jsdom-keys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const LIVING_KEYS = [
'Comment',
'DocumentType',
'NodeList',
'RadioNodeList',
'HTMLCollection',
'HTMLOptionsCollection',
'DOMStringMap',
Expand Down Expand Up @@ -96,6 +97,7 @@ const LIVING_KEYS = [
'HTMLVideoElement',
'HTMLAudioElement',
'HTMLTrackElement',
'HTMLFormControlsCollection',
'SVGElement',
'SVGGraphicsElement',
'SVGSVGElement',
Expand Down Expand Up @@ -126,6 +128,7 @@ const LIVING_KEYS = [
'Location',
'History',
'Screen',
'Crypto',
'Performance',
'Navigator',
'PluginArray',
Expand Down Expand Up @@ -159,6 +162,7 @@ const LIVING_KEYS = [
'Headers',
'AbortController',
'AbortSignal',
'ArrayBuffer',

// not specified in docs, but is available
'Image',
Expand Down
19 changes: 5 additions & 14 deletions packages/vitest/src/integrations/env/utils.ts
Original file line number Diff line number Diff line change
@@ -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',
Expand All @@ -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
}))
Expand All @@ -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<string | symbol, any>(
allowRewrite.filter(key => key in global).map(key => [key, global[key]]),
)
const originals = new Map<string | symbol, any>()

const overrideObject = new Map<string | symbol, any>()
for (const key of keys) {
Expand All @@ -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))
Expand Down

0 comments on commit 922e525

Please sign in to comment.