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: always rewrite defined jsdom keys #2290

Merged
merged 1 commit into from Nov 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/vitest/src/integrations/env/jsdom-keys.ts
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
@@ -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