test: guard discord listener mock calls

This commit is contained in:
Peter Steinberger
2026-05-12 02:32:13 +01:00
parent 411846893c
commit 2f4fa9cbe2

View File

@@ -14,6 +14,15 @@ function createLogger() {
};
}
function firstErrorMessage(logger: ReturnType<typeof createLogger>): string {
const firstCall = logger.error.mock.calls.at(0);
if (!firstCall) {
throw new Error("expected logger.error call");
}
expect(firstCall).toHaveLength(1);
return String(firstCall[0]);
}
function fakeEvent(channelId: string) {
return { channel_id: channelId } as never;
}
@@ -136,10 +145,7 @@ describe("DiscordMessageListener", () => {
await expect(listener.handle(fakeEvent("ch-1"), {} as never)).resolves.toBeUndefined();
await flushAsyncWork();
expect(logger.error).toHaveBeenCalledTimes(1);
expect(logger.error.mock.calls[0]).toHaveLength(1);
expect(String(logger.error.mock.calls[0]?.[0])).toContain(
"discord handler failed: Error: boom",
);
expect(firstErrorMessage(logger)).toContain("discord handler failed: Error: boom");
});
it("calls onEvent callback for each message", async () => {
@@ -185,8 +191,7 @@ describe("DiscordInteractionListener", () => {
await flushAsyncWork();
expect(logger.error).toHaveBeenCalledTimes(1);
expect(logger.error.mock.calls[0]).toHaveLength(1);
expect(String(logger.error.mock.calls[0]?.[0])).toContain(
expect(firstErrorMessage(logger)).toContain(
"discord interaction handler failed: Error: interaction boom",
);
});