

























@@ -16,14 +16,30 @@ function jsonResponse(payload: unknown, status = 200) {
1616});
1717}
181819+function requireFirstFetchCall(fetchImpl: ReturnType<typeof vi.fn>) {
20+const [call] = fetchImpl.mock.calls as unknown[][];
21+if (!call) {
22+throw new Error("expected fetch call");
23+}
24+return call;
25+}
26+1927function requireFirstFetchInput(fetchImpl: ReturnType<typeof vi.fn>): RequestInfo | URL {
20-const input = fetchImpl.mock.calls[0]?.[0] as RequestInfo | URL | undefined;
28+const input = requireFirstFetchCall(fetchImpl)[0] as RequestInfo | URL | undefined;
2129if (!input) {
2230throw new Error("expected fetch input");
2331}
2432return input;
2533}
263435+function requireFirstFetchInit(fetchImpl: ReturnType<typeof vi.fn>): RequestInit {
36+const init = requireFirstFetchCall(fetchImpl)[1];
37+if (!init || typeof init !== "object" || Array.isArray(init)) {
38+throw new Error("expected fetch init");
39+}
40+return init as RequestInit;
41+}
42+2743async function expectQaCredentialAdminError(promise: Promise<unknown>, code: string) {
2844const error = await promise.then(
2945() => undefined,
@@ -69,8 +85,10 @@ describe("qa credential admin runtime", () => {
6985});
70867187expect(result.credential.credentialId).toBe("cred-1");
72-const [url, init] = fetchImpl.mock.calls[0] ?? [];
73-expect(url).toBe("https://first-schnauzer-821.convex.site/qa-credentials/v1/admin/add");
88+expect(requireFirstFetchInput(fetchImpl)).toBe(
89+"https://first-schnauzer-821.convex.site/qa-credentials/v1/admin/add",
90+);
91+const init = requireFirstFetchInit(fetchImpl);
7492const headers = init?.headers as Record<string, string>;
7593expect(headers.authorization).toBe("Bearer maint-secret");
7694const bodyText = init?.body;
@@ -207,8 +225,7 @@ describe("qa credential admin runtime", () => {
207225});
208226209227expect(result.credentials).toHaveLength(1);
210-const [, init] = fetchImpl.mock.calls[0] ?? [];
211-const bodyText = init?.body;
228+const bodyText = requireFirstFetchInit(fetchImpl).body;
212229expect(typeof bodyText).toBe("string");
213230const body = JSON.parse(bodyText as string) as Record<string, unknown>;
214231expect(body).toEqual({
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。