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: don't bind global classes #1345

Merged
merged 2 commits into from May 21, 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
18 changes: 15 additions & 3 deletions packages/vitest/src/integrations/env/utils.ts
Expand Up @@ -26,6 +26,10 @@ export function getWindowKeys(global: any, win: any) {
return keys
}

function isClassLikeName(name: string) {
return name[0] === name[0].toUpperCase()
}

interface PopulateOptions {
bindFunctions?: boolean
}
Expand All @@ -40,13 +44,21 @@ export function populateGlobal(global: any, win: any, options: PopulateOptions =

const overrideObject = new Map<string | symbol, any>()
for (const key of keys) {
const bindedFunction = bindFunctions && typeof win[key] === 'function' && win[key].bind(win)
// we bind functions such as addEventListener and others
// because they rely on `this` in happy-dom, and in jsdom it
// has a priority for getting implementaion from symbols
// (global doesn't have these symbols, but window - does)
const boundFunction = bindFunctions
&& typeof win[key] === 'function'
&& !isClassLikeName(key)
&& win[key].bind(win)

Object.defineProperty(global, key, {
get() {
if (overrideObject.has(key))
return overrideObject.get(key)
if (bindedFunction)
return bindedFunction
if (boundFunction)
return boundFunction
return win[key]
},
set(v) {
Expand Down
6 changes: 6 additions & 0 deletions test/core/test/dom.test.ts
Expand Up @@ -126,3 +126,9 @@ it('globals are the same', () => {
expect(window.globalThis.Blob).toBe(globalThis.Blob)
expect(Blob).toBe(globalThis.Blob)
})

it('can extend global class', () => {
class SuperBlob extends Blob {}

expect(SuperBlob).toBeDefined()
})