Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion packages/vitest/src/integrations/env/jsdom.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { ConstructorOptions, DOMWindow, VirtualConsole as IVirtualConsole } from 'jsdom'
import type { Environment } from '../../types/environment'
import type { JSDOMOptions } from '../../types/jsdom-options'
import { createRequire } from 'node:module'
import { URL as NodeURL } from 'node:url'
import { populateGlobal } from './utils'

Expand Down Expand Up @@ -306,6 +307,11 @@ function createCompatUtils(window: DOMWindow): CompatUtils {
const implSymbol = Object.getOwnPropertySymbols(
Object.getOwnPropertyDescriptors(new window.Blob()),
)[0]
// jsdom 30.1 moved the implementation from the `Symbol(impl)` own property
// into a private class field, so `implSymbol` is `undefined` there and
// `blob[implSymbol]` cannot unwrap the wrapper anymore;
// jsdom's own `implForWrapper` can unwrap both layouts
const implForWrapper = getImplForWrapper()
const utils = {
window,
makeCompatFormData(formData: FormData) {
Expand All @@ -321,14 +327,35 @@ function createCompatUtils(window: DOMWindow): CompatUtils {
return nodeFormData
},
makeCompatBlob(blob: Blob) {
const impl = (blob as any)[implSymbol]
const impl = (implForWrapper?.(blob) ?? (blob as any)[implSymbol]) as any
// jsdom 28 renamed `_buffer` to `_bytes`
return new NodeBlob_([impl._bytes ?? impl._buffer], { type: blob.type })
},
}
return utils
}

function getImplForWrapper(): ((wrapper: object) => unknown) | undefined {
const require = createRequire(import.meta.url)
// jsdom 30.1+ exports `implForWrapper` from the new subpath, older
// versions from the old one; jsdom has no `exports` map, so both
// subpaths can be required directly
const subpaths = [
'jsdom/lib/generated/idl/utils.js',
'jsdom/lib/jsdom/living/generated/utils.js',
]
for (const subpath of subpaths) {
try {
const utils = require(subpath)
if (typeof utils?.implForWrapper === 'function') {
return utils.implForWrapper
}
}
catch {}
}
return undefined
}

function patchAddEventListener(window: DOMWindow) {
const abortControllers = new WeakMap<AbortSignal, AbortController>()
const JSDOMAbortSignal = window.AbortSignal
Expand Down
2 changes: 2 additions & 0 deletions test/unit/test/environments/jsdom.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,8 @@ test('URL.createObjectUrl works properly', () => {
expect(() => {
URL.createObjectURL(new File([], 'name.js'))
}).not.toThrow()
expect(URL.createObjectURL(new Blob(['hello']))).toMatch(/^blob:/)
expect(URL.createObjectURL(new File(['hello'], 'name.js'))).toMatch(/^blob:/)
})

test('compat classes preserve their .name property', () => {
Expand Down
Loading