From 9a735d0a2b8e9744d516af2db8bb1153884a09d7 Mon Sep 17 00:00:00 2001 From: Till JS Date: Wed, 20 May 2026 15:07:14 +0200 Subject: [PATCH] fix(api): vi.fn() in lib-url-fetch.test typt zu typeof fetch MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 5 tsc-Errors „Property 'preconnect' is missing in type Mock". Bun's `typeof fetch` deklariert eine `preconnect(url, options)`-Methode, vitests `vi.fn()`-Mock hat die nicht. Cast über `unknown` umgeht den strikten Type-Check, ohne den Mock künstlich aufzublähen. - makeFetch() bekommt `typeof globalThis.fetch` als Return-Type - direkter inline-vi.fn().mockRejectedValue() cast'd am Use-Site - 6/6 Tests grün Co-Authored-By: Claude Opus 4.7 (1M context) --- apps/api/tests/lib-url-fetch.test.ts | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/apps/api/tests/lib-url-fetch.test.ts b/apps/api/tests/lib-url-fetch.test.ts index c7675bf..f5432a3 100644 --- a/apps/api/tests/lib-url-fetch.test.ts +++ b/apps/api/tests/lib-url-fetch.test.ts @@ -1,12 +1,17 @@ import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; import { fetchUrlContent } from '../src/lib/url-fetch.ts'; -function makeFetch(responses: Array<{ ok: boolean; json?: () => Promise; text?: () => Promise }>) { +function makeFetch( + responses: Array<{ ok: boolean; json?: () => Promise; text?: () => Promise }>, +): typeof globalThis.fetch { let call = 0; - return vi.fn(async () => { + const fn = vi.fn(async () => { const r = responses[Math.min(call++, responses.length - 1)]; return r as unknown as Response; }); + // Bun's `typeof fetch` includes a `preconnect`-method that vi.fn() + // doesn't have. Cast über `unknown` umgeht den strikten Type-Check. + return fn as unknown as typeof globalThis.fetch; } describe('fetchUrlContent', () => { @@ -67,7 +72,9 @@ describe('fetchUrlContent', () => { }); it('returns null when mana-search returns no content and direct fetch fails', async () => { - globalThis.fetch = vi.fn().mockRejectedValue(new Error('Network error')); + globalThis.fetch = vi + .fn() + .mockRejectedValue(new Error('Network error')) as unknown as typeof globalThis.fetch; const result = await fetchUrlContent('https://example.com'); expect(result).toBeNull();