Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/lit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
},
"devDependencies": {
"@vitest/browser-playwright": "latest",
"jsdom": "^29.1.1",
"jsdom": "^30.1.1",
"playwright": "^1.62.1",
"vite": "latest",
"vitest": "latest"
Expand Down
2 changes: 1 addition & 1 deletion examples/projects/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"@vitejs/plugin-react": "^5.2.0",
"@vitest/ui": "latest",
"fastify": "^5.12.1",
"jsdom": "^29.1.1",
"jsdom": "^30.1.1",
"react": "^19.2.8",
"supertest": "^7.2.2",
"tsx": "^4.23.13",
Expand Down
4 changes: 2 additions & 2 deletions packages/vitest/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@
"@jridgewell/trace-mapping": "catalog:",
"@opentelemetry/api": "^1.9.1",
"@sinonjs/fake-timers": "15.4.0",
"@types/jsdom": "^28.0.3",
"@types/jsdom": "^30.0.0",
"@types/node": "^24.13.3",
"@types/picomatch": "^4.0.3",
"@types/prompts": "^2.4.9",
Expand All @@ -204,7 +204,7 @@
"cac": "catalog:",
"flatted": "catalog:",
"happy-dom": "^20.12.0",
"jsdom": "^29.1.1",
"jsdom": "^30.1.1",
"local-pkg": "^1.2.1",
"mime": "^4.1.0",
"pathe": "catalog:",
Expand Down
37 changes: 31 additions & 6 deletions 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 @@ -292,12 +293,31 @@ interface CompatUtils {
makeCompatFormData: (formData: FormData) => FormData
}

// jsdom keeps Blob bytes on an internal "impl" object and exposes no synchronous
// public way to read them, so this reaches into its generated bindings
function createBlobImplGetter(window: DOMWindow): (blob: Blob) => any {
const _require = createRequire(import.meta.url)
// jsdom 28.1 moved the generated bindings; jsdom has no "exports" map, so both subpaths resolve
for (const id of [
'jsdom/lib/generated/idl/utils.js',
'jsdom/lib/jsdom/living/generated/utils.js',
]) {
try {
const { implForWrapper } = _require(id)
if (typeof implForWrapper === 'function') {
return implForWrapper
}
} catch {}
}
// jsdom < 30.1 also stores the impl under an own Symbol("impl")
const implSymbol = Object.getOwnPropertySymbols(new window.Blob())[0]
return (blob) => (blob as any)[implSymbol]
}

let getBlobImpl: (blob: Blob) => any

function createCompatUtils(window: DOMWindow): CompatUtils {
// this returns a hidden Symbol(impl)
// this is cursed, and jsdom should just implement fetch API itself
const implSymbol = Object.getOwnPropertySymbols(
Object.getOwnPropertyDescriptors(new window.Blob()),
)[0]
getBlobImpl ??= createBlobImplGetter(window)
const utils = {
window,
makeCompatFormData(formData: FormData) {
Expand All @@ -312,7 +332,12 @@ function createCompatUtils(window: DOMWindow): CompatUtils {
return nodeFormData
},
makeCompatBlob(blob: Blob) {
const impl = (blob as any)[implSymbol]
const impl = getBlobImpl(blob)
if (!impl) {
throw new TypeError(
'Vitest cannot read the bytes of a jsdom Blob. This is a Vitest bug, please report it with your jsdom version.',
)
}
// jsdom 28 renamed `_buffer` to `_bytes`
return new NodeBlob_([impl._bytes ?? impl._buffer], { type: blob.type })
},
Expand Down
Loading
Loading