Skip to content

fix(jsdom): override globals that Fetch API relies on#8390

Merged
sheremet-va merged 2 commits intovitest-dev:mainfrom
sheremet-va:fix/override-fetch-api
Aug 6, 2025
Merged

fix(jsdom): override globals that Fetch API relies on#8390
sheremet-va merged 2 commits intovitest-dev:mainfrom
sheremet-va:fix/override-fetch-api

Conversation

@sheremet-va
Copy link
Copy Markdown
Member

@sheremet-va sheremet-va commented Aug 5, 2025

Description

Fixes #8374
Fixes #7906

Please don't delete this checklist! Before submitting the PR, please make sure you do the following:

  • It's really useful if your PR references an issue where it is discussed ahead of time. If the feature is substantial or introduces breaking changes without a discussion, PR might be closed.
  • Ideally, include a test that fails without this PR but passes with it.
  • Please, don't make changes to pnpm-lock.yaml unless you introduce a new test example.
  • Please check Allow edits by maintainers to make review process faster. Note that this option is not available for repositories that are owned by Github organizations.

Tests

  • Run the tests with pnpm test:ci.

Documentation

  • If you introduce new functionality, document it. You can run documentation with pnpm run docs command.

Changesets

  • Changes in changelog are generated from PR name. Please, make sure that it explains your changes in an understandable manner. Please, prefix changeset messages with feat:, fix:, perf:, docs:, or chore:.

@netlify
Copy link
Copy Markdown

netlify bot commented Aug 5, 2025

Deploy Preview for vitest-dev ready!

Name Link
🔨 Latest commit e44dfdc
🔍 Latest deploy log https://app.netlify.com/projects/vitest-dev/deploys/6891fc157505f30008268c6a
😎 Deploy Preview https://deploy-preview-8390--vitest-dev.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

hi-ogawa
hi-ogawa previously approved these changes Aug 6, 2025
@sheremet-va sheremet-va merged commit 05b4178 into vitest-dev:main Aug 6, 2025
14 checks passed
@sheremet-va sheremet-va deleted the fix/override-fetch-api branch August 6, 2025 15:59
matej-pavlik added a commit to matej-pavlik/school-vavjs that referenced this pull request Aug 7, 2025
The error originates from Vitest when used with jsdom. See vitest-dev/vitest#7906 for more details. This issue is fixed in vitest-dev/vitest#8390, but not yet released.
@ghost
Copy link
Copy Markdown

ghost commented Sep 5, 2025

Sorry to bump this @sheremet-va. Is there any way we could get this added into the next release? Seems like it's currently slotted for the v4 release. Not sure the timeline on when v4 will be released, so this may not be important enough.

@sheremet-va
Copy link
Copy Markdown
Member Author

Sorry to bump this @sheremet-va. Is there any way we could get this added into the next release? Seems like it's currently slotted for the v4 release. Not sure the timeline on when v4 will be released, so this may not be important enough.

You can use Vitest 4 beta to test this.

@silas
Copy link
Copy Markdown

silas commented Sep 24, 2025

pnpm patch for anyone that might want to manually apply this change to 3.2.4.

pnpm-workspace.yaml
patchedDependencies:
  vitest@3.2.4: patches/vitest@3.2.4.patch
patches/vitest@3.2.4.patch
diff --git a/dist/chunks/index.CmSc2RE5.js b/dist/chunks/index.CmSc2RE5.js
index 90004b3064c731a22f584000525e0bff3f423867..7fa0351ff36a5d21640520685420046298b71959 100644
--- a/dist/chunks/index.CmSc2RE5.js
+++ b/dist/chunks/index.CmSc2RE5.js
@@ -3,8 +3,6 @@ import { Console } from 'node:console';
 // SEE https://github.com/jsdom/jsdom/blob/master/lib/jsdom/living/interfaces.js
 const LIVING_KEYS = [
 	"DOMException",
-	"URL",
-	"URLSearchParams",
 	"EventTarget",
 	"NamedNodeMap",
 	"Node",
@@ -161,9 +159,6 @@ const LIVING_KEYS = [
 	"ShadowRoot",
 	"MutationObserver",
 	"MutationRecord",
-	"Headers",
-	"AbortController",
-	"AbortSignal",
 	"Uint8Array",
 	"Uint16Array",
 	"Uint32Array",
@@ -441,9 +436,6 @@ var jsdom = {
 		// https://nodejs.org/dist/latest/docs/api/globals.html
 		const globalNames = [
 			"structuredClone",
-			"fetch",
-			"Request",
-			"Response",
 			"BroadcastChannel",
 			"MessageChannel",
 			"MessagePort",
@@ -454,6 +446,20 @@ var jsdom = {
 			const value = globalThis[name];
 			if (typeof value !== "undefined" && typeof dom.window[name] === "undefined") dom.window[name] = value;
 		}
+		const overrideGlobals = [
+			"fetch",
+			"Request",
+			"Response",
+			"Headers",
+			"AbortController",
+			"AbortSignal",
+			"URL",
+			"URLSearchParams",
+		];
+		for (const name of overrideGlobals) {
+			const value = globalThis[name];
+			if (typeof value !== "undefined") dom.window[name] = value;
+		}
 		return {
 			getVmContext() {
 				return dom.getInternalVMContext();

gr2m added a commit to vercel/ai that referenced this pull request Mar 12, 2026
- Fix elevenlabs transcription test snapshot for Node 24 File serialization
- Allow Node 24 test matrix to fail until jsdom/vitest compat is resolved

Node 24 introduces a stricter AbortSignal instanceof check in native
fetch that rejects jsdom's AbortSignal. This is fixed in vitest 3.2.4+
(vitest-dev/vitest#8390) but requires a vitest major version upgrade.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
louise-davies added a commit to ral-facilities/scigateway that referenced this pull request Mar 13, 2026
- see vitest-dev/vitest#8390 (comment) for path details
- I was running node 22 locally hence I didn't see this issues until CI ran
gr2m added a commit to vercel/ai that referenced this pull request Mar 16, 2026
## Summary

Adds Node.js 24 to the CI test matrix and fixes all build/test
incompatibilities.

Closes #13357

## Changes

### CI & config
- Adds Node.js 24 to `.github/workflows/ci.yml` test matrix
- Adds `^24.0.0` to root `package.json` engines field

### SvelteKit example build fix
- Updates `@sveltejs/adapter-vercel` from v5 to v6 and `@sveltejs/kit`
to v2.55+ in `examples/sveltekit-openai` — adapter-vercel v5 only
supported Node 18/20/22, v6 adds Node 24

### Vitest v2 → v4 upgrade
Node 24 ships with undici v7, which enforces strict `AbortSignal`
instanceof checks in `fetch()`. The jsdom environment in vitest v2/v3
replaces the global `AbortController`/`AbortSignal` with its own
polyfill, and Node 24's native fetch rejects these with `"Expected
signal to be an instance of AbortSignal"`. Vitest v4 stops overriding
these globals
([vitest-dev/vitest#8390](vitest-dev/vitest#8390)),
fixing the incompatibility.

#### Test adaptations required by vitest v4

**`vi.fn()` arrow functions can no longer be used as constructors** —
vitest v4 wraps mock functions differently, so arrow-function mocks fail
with `TypeError: is not a constructor` when called with `new`. Fixed by
changing arrow functions to `function` expressions in mock
implementations:
-
`packages/amazon-bedrock/src/bedrock-event-stream-response-handler.test.ts`
- `packages/baseten/src/baseten-provider.unit.test.ts`
- `packages/gateway/src/gateway-provider.test.ts`
-
`packages/google-vertex/src/google-vertex-auth-google-auth-library.test.ts`

**`toStrictEqual(expect.objectContaining(...))` is stricter** — vitest
v4 now requires that `undefined` properties on the actual object (e.g.
`metadata: undefined`) also appear in the expected pattern. Since these
tests intentionally do partial matching, changed to
`toEqual(expect.objectContaining(...))`:
- `packages/angular/src/lib/chat.ng.test.ts`
- `packages/svelte/src/chat.svelte.test.ts`

**`toThrowError` with an Error object checks all properties** —
previously only checked `message`, now checks every property. The
anthropic overloaded-error test compared `requestBodyValues: {}` against
the full request body. Changed to try/catch with individual property
assertions:
- `packages/anthropic/src/anthropic-messages-language-model.test.ts`

**`test.workspace` renamed to `test.projects`**:
- `packages/svelte/vite.config.ts`

### Other test fixes
- `packages/openai` and `packages/elevenlabs`: Made `File` object
snapshot tests Node version-agnostic (Node 24 serializes additional
`Symbol(state)` on File objects)
- `packages/mcp`: Fixed race condition in SSE reconnect test where
`server.calls[2]` was accessed before the fire-and-forget
`openInboundSse()` completed — added `vi.waitFor()`
- Pinned `tinyexec` to 1.0.2 via pnpm overrides (1.0.3 has broken
package exports on npm)

### Backports 

- v6: #13440
- v5: #13441

---------

Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

3 participants