






















@@ -199,6 +199,20 @@ describe("createDiscordGatewayPlugin", () => {
199199};
200200}
201201202+type MockWithCalls = { mock: { calls: unknown[][] } };
203+204+function firstMockCall(mock: MockWithCalls, label: string): unknown[] {
205+const call = mock.mock.calls.at(0);
206+if (!call) {
207+throw new Error(`expected ${label} call`);
208+}
209+return call;
210+}
211+212+function firstMockArg(mock: MockWithCalls, label: string, index = 0) {
213+return firstMockCall(mock, label)[index];
214+}
215+202216function createProxyTestingOverrides() {
203217return {
204218HttpsProxyAgentCtor:
@@ -279,7 +293,7 @@ describe("createDiscordGatewayPlugin", () => {
279293"wss://gateway.discord.gg/",
280294);
281295expect(runtime.log).toHaveBeenCalledTimes(1);
282-expect(String(runtime.log.mock.calls[0]?.[0])).toContain(
296+expect(String(firstMockArg(runtime.log, "runtime.log"))).toContain(
283297"discord: gateway metadata lookup failed transiently",
284298);
285299}
@@ -328,10 +342,12 @@ describe("createDiscordGatewayPlugin", () => {
328342await registerGatewayClientWithMetadata({ plugin, fetchMock: globalFetchMock });
329343330344expect(globalFetchMock).toHaveBeenCalledTimes(1);
331-const fetchInit = globalFetchMock.mock.calls[0]?.[1] as
345+const fetchInit = firstMockArg(globalFetchMock, "globalFetchMock", 1) as
332346| { headers?: Record<string, string>; signal?: unknown }
333347| undefined;
334-expect(globalFetchMock.mock.calls[0]?.[0]).toBe("https://discord.com/api/v10/gateway/bot");
348+expect(firstMockArg(globalFetchMock, "globalFetchMock")).toBe(
349+"https://discord.com/api/v10/gateway/bot",
350+);
335351expect(fetchInit?.headers).toEqual({ Authorization: "Bot token-123" });
336352expect(fetchInit?.signal).toBeInstanceOf(AbortSignal);
337353expect(baseRegisterClientSpy).toHaveBeenCalledTimes(1);
@@ -349,7 +365,9 @@ describe("createDiscordGatewayPlugin", () => {
349365createWebSocket("wss://gateway.discord.gg");
350366351367expect(httpsAgentSpy).toHaveBeenCalledTimes(1);
352-const httpsAgentOptions = httpsAgentSpy.mock.calls[0]?.[0] as { lookup?: unknown } | undefined;
368+const httpsAgentOptions = firstMockArg(httpsAgentSpy, "httpsAgentSpy") as
369+| { lookup?: unknown }
370+| undefined;
353371expect(Object.keys(httpsAgentOptions ?? {})).toEqual(["lookup"]);
354372expect(typeof httpsAgentOptions?.lookup).toBe("function");
355373expect(webSocketSpy).toHaveBeenCalledWith("wss://gateway.discord.gg", {
@@ -499,10 +517,12 @@ describe("createDiscordGatewayPlugin", () => {
499517await registerGatewayClientWithMetadata({ plugin, fetchMock: globalFetchMock });
500518501519expect(globalFetchMock).toHaveBeenCalledTimes(1);
502-const fetchInit = globalFetchMock.mock.calls[0]?.[1] as
520+const fetchInit = firstMockArg(globalFetchMock, "globalFetchMock", 1) as
503521| { headers?: Record<string, string>; signal?: unknown }
504522| undefined;
505-expect(globalFetchMock.mock.calls[0]?.[0]).toBe("https://discord.com/api/v10/gateway/bot");
523+expect(firstMockArg(globalFetchMock, "globalFetchMock")).toBe(
524+"https://discord.com/api/v10/gateway/bot",
525+);
506526expect(fetchInit?.headers).toEqual({ Authorization: "Bot token-123" });
507527expect(fetchInit?.signal).toBeInstanceOf(AbortSignal);
508528expect(baseRegisterClientSpy).toHaveBeenCalledTimes(1);
@@ -548,7 +568,7 @@ describe("createDiscordGatewayPlugin", () => {
548568549569expect(Object.getPrototypeOf(plugin)).not.toBe(GatewayPlugin.prototype);
550570expect(runtime.error).toHaveBeenCalledTimes(1);
551-expect(String(runtime.error.mock.calls[0]?.[0])).toContain("loopback host");
571+expect(String(firstMockArg(runtime.error, "runtime.error"))).toContain("loopback host");
552572expect(runtime.log).not.toHaveBeenCalled();
553573});
554574@@ -580,7 +600,7 @@ describe("createDiscordGatewayPlugin", () => {
580600"wss://gateway.discord.gg/",
581601);
582602expect(runtime.log).toHaveBeenCalledTimes(1);
583-expect(String(runtime.log.mock.calls[0]?.[0])).toContain(
603+expect(String(firstMockArg(runtime.log, "runtime.log"))).toContain(
584604"discord: gateway metadata lookup failed transiently",
585605);
586606});
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。