From d815512608d3c995c0b1c2d6d378fac86f47220b Mon Sep 17 00:00:00 2001 From: Tiago Lupepic Date: Fri, 31 Jul 2026 11:29:46 +0200 Subject: [PATCH 1/2] fix(test): replace mock_fetch dependency --- dev_deps.ts | 1 - tests/utils.ts | 17 +++++++++++++---- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/dev_deps.ts b/dev_deps.ts index 3a00c34..8f97861 100644 --- a/dev_deps.ts +++ b/dev_deps.ts @@ -1,2 +1 @@ export { assertEquals } from "https://deno.land/std@0.196.0/assert/mod.ts"; -export * as mf from "https://deno.land/x/mock_fetch@0.3.0/mod.ts"; diff --git a/tests/utils.ts b/tests/utils.ts index f006108..83e3f42 100644 --- a/tests/utils.ts +++ b/tests/utils.ts @@ -1,6 +1,5 @@ // deno-lint-ignore-file no-explicit-any ban-types import { assertEquals } from "../dev_deps.ts"; -import { mf } from "../dev_deps.ts"; import { Client, getLagoError } from "../mod.ts"; import type { Api, @@ -27,10 +26,20 @@ type ExtractLagoResponse = E extends ( const errorMessage = "Lago Error" as const; -export function setupMockClient(route: string, handler: mf.MatchHandler) { - const { fetch, mock } = mf.sandbox(); +type MatchHandler = (request: Request) => Response | Promise; - mock(route, handler); +export function setupMockClient(route: string, handler: MatchHandler) { + const [method, path] = route.split("@"); + + const fetch = (async (input: RequestInfo | URL, init?: RequestInit) => { + const request = input instanceof Request ? input : new Request(input, init); + const url = new URL(request.url); + + assertEquals(request.method, method); + assertEquals(url.pathname, path); + + return await handler(request); + }) as typeof globalThis.fetch; return Client("api_key", { customFetch: fetch }); } From c32f2bc0a2373b9f6b2f5b74f9cf52d6786468f0 Mon Sep 17 00:00:00 2001 From: Tiago Lupepic Date: Fri, 31 Jul 2026 14:25:12 +0200 Subject: [PATCH 2/2] fix(test): improve local mock assertions --- tests/utils.ts | 54 +++++++++++++++++++++++++++++++++++--------------- 1 file changed, 38 insertions(+), 16 deletions(-) diff --git a/tests/utils.ts b/tests/utils.ts index 83e3f42..3e1b8d8 100644 --- a/tests/utils.ts +++ b/tests/utils.ts @@ -26,21 +26,30 @@ type ExtractLagoResponse = E extends ( const errorMessage = "Lago Error" as const; +type LagoRoute = `${"POST" | "GET" | "PUT" | "DELETE"}@/api/v1/${string}`; type MatchHandler = (request: Request) => Response | Promise; -export function setupMockClient(route: string, handler: MatchHandler) { - const [method, path] = route.split("@"); +export function createMockFetch(route: LagoRoute, handler: MatchHandler) { + const [method, path] = route.split("@") as [string, string]; + let request: Request | undefined; const fetch = (async (input: RequestInfo | URL, init?: RequestInit) => { - const request = input instanceof Request ? input : new Request(input, init); - const url = new URL(request.url); - - assertEquals(request.method, method); - assertEquals(url.pathname, path); + request = input instanceof Request ? input : new Request(input, init); return await handler(request); }) as typeof globalThis.fetch; + return { + fetch, + expectedMethod: method, + expectedPath: path, + getRequest: () => request, + }; +} + +export function setupMockClient(route: LagoRoute, handler: MatchHandler) { + const { fetch } = createMockFetch(route, handler); + return Client("api_key", { customFetch: fetch }); } @@ -60,7 +69,7 @@ export async function lagoTest< }: { testType: "error" | "200"; t: Deno.TestContext; - route: `${"POST" | "GET" | "PUT" | "DELETE"}@/api/v1/${string}`; + route: LagoRoute; clientPath: [T, U]; inputParams: ExtractLagoInput[T][U]>; responseObject?: ExtractLagoDataOrError< @@ -70,21 +79,32 @@ export async function lagoTest< urlParams?: Record; }, ) { - const client = setupMockClient( + const { fetch, getRequest, expectedMethod, expectedPath } = createMockFetch( route, - (_req) => { - if (urlParams) { - const urlSearchParams = new URLSearchParams(new URL(_req.url).search); - Object.entries(urlParams).forEach(([key, value]) => { - assertEquals(urlSearchParams.get(key), value); - }); - } + () => { return new Response( responseObject ? JSON.stringify(responseObject) : null, { status }, ); }, ); + const client = Client("api_key", { customFetch: fetch }); + + const assertRequest = () => { + const request = getRequest(); + if (!request) throw new Error("Expected a request to be sent"); + + const url = new URL(request.url); + assertEquals(request.method, expectedMethod); + assertEquals(url.pathname, expectedPath); + + if (urlParams) { + const urlSearchParams = new URLSearchParams(url.search); + Object.entries(urlParams).forEach(([key, value]) => { + assertEquals(urlSearchParams.get(key), value); + }); + } + }; switch (testType) { case "error": @@ -105,6 +125,7 @@ export async function lagoTest< (lagoError as ApiErrorUnprocessableEntity).error, errorMessage, ); + assertRequest(); } }); break; @@ -117,6 +138,7 @@ export async function lagoTest< ) as Response; assertEquals(response.status, 200); + assertRequest(); }); break;