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) <noreply@anthropic.com>
This commit is contained in:
Uwe Schuster 2026-04-21 22:29:54 +02:00
parent de150e790d
commit 84693a7af5
2 changed files with 7 additions and 3 deletions

View file

@ -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": {

View file

@ -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<typeof fetch>))) 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,