From e3a428078814bb7865ded1b6473fcbaa63d45c9a Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Wed, 13 May 2026 02:44:01 +0100 Subject: [PATCH] test: dedupe fetch auth mock reads --- src/plugin-sdk/fetch-auth.test.ts | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/src/plugin-sdk/fetch-auth.test.ts b/src/plugin-sdk/fetch-auth.test.ts index e0a2b2d043a..afe2a5aed9e 100644 --- a/src/plugin-sdk/fetch-auth.test.ts +++ b/src/plugin-sdk/fetch-auth.test.ts @@ -4,6 +4,14 @@ import { resolveRequestUrl } from "./request-url.js"; const asFetch = (fn: unknown): typeof fetch => fn as typeof fetch; +function fetchCall(fetchFn: ReturnType, index: number): [unknown, RequestInit?] { + const call = fetchFn.mock.calls[index]; + if (!call) { + throw new Error(`expected fetch call ${index}`); + } + return call as [unknown, RequestInit?]; +} + describe("fetchWithBearerAuthScopeFallback", () => { it("rejects non-https urls when https is required", async () => { await expect( @@ -85,7 +93,7 @@ describe("fetchWithBearerAuthScopeFallback", () => { if (expectedAuthHeader === null) { return; } - const secondCallInit = fetchFn.mock.calls.at(1)?.[1] as RequestInit | undefined; + const secondCallInit = fetchCall(fetchFn, 1)[1]; const secondHeaders = new Headers(secondCallInit?.headers); expect(secondHeaders.get("authorization")).toBe(expectedAuthHeader); }, @@ -143,10 +151,10 @@ describe("fetchWithBearerAuthScopeFallback", () => { expect(response.status).toBe(200); expect(fetchFn).toHaveBeenCalledTimes(2); - expect( - Object.getOwnPropertySymbols(fetchFn.mock.calls.at(0)?.[1]?.headers as object), - ).toStrictEqual([]); - expect(new Headers(fetchFn.mock.calls.at(1)?.[1]?.headers).get("authorization")).toBe( + expect(Object.getOwnPropertySymbols(fetchCall(fetchFn, 0)[1]?.headers as object)).toStrictEqual( + [], + ); + expect(new Headers(fetchCall(fetchFn, 1)[1]?.headers).get("authorization")).toBe( "Bearer token-1", ); expect(Object.getOwnPropertySymbols(headers)).toHaveLength(1);