From 84693a7af5f38bfb2db85b9a0a372a43e8014432 Mon Sep 17 00:00:00 2001 From: Uwe Schuster Date: Tue, 21 Apr 2026 22:29:54 +0200 Subject: [PATCH] v0.3.3: resolve fetch at call time, not factory-creation Tests that vi.stubGlobal('fetch', ...) AFTER module import (standard vitest pattern) couldn't see their stub because the factory captured globalThis.fetch at createCoreFetch() time. Switch to a thin wrapper that does the lookup per call. Co-Authored-By: Claude Opus 4.7 (1M context) --- package.json | 2 +- src/core-fetch.ts | 8 ++++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index 973c243..e360941 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@uschuster/webapp-scaffold", - "version": "0.3.2", + "version": "0.3.3", "description": "Shared build scripts + Vite config factories for webapp-template-derived projects.", "type": "module", "bin": { diff --git a/src/core-fetch.ts b/src/core-fetch.ts index c976ecb..3f6fd34 100644 --- a/src/core-fetch.ts +++ b/src/core-fetch.ts @@ -96,7 +96,11 @@ const MUTATING = new Set(['POST', 'PUT', 'PATCH', 'DELETE']); export function createCoreFetch(opts: CoreFetchOptions = {}): CoreFetch { const base = (opts.baseUrl ?? '').replace(/\/$/, ''); - const fetchImpl = opts.fetchImpl ?? fetch; + // Resolve fetch at call time, not construction time, so tests that + // `vi.stubGlobal('fetch', ...)` after module import see the stub. + const callFetch: typeof fetch = opts.fetchImpl + ? opts.fetchImpl + : ((...args) => fetch(...(args as Parameters))) as typeof fetch; const shape: ResponseShape = opts.responseShape ?? 'wrapped'; function wrap(data: unknown, status: number, headers: Headers): unknown { @@ -129,7 +133,7 @@ export function createCoreFetch(opts: CoreFetchOptions = {}): CoreFetch { let r: Response; try { - r = await fetchImpl(`${base}${url}`, { + r = await callFetch(`${base}${url}`, { credentials: 'include', ...init, headers,