Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

backendStartupReadiness.test.ts

58 lines1.8 KB

1import { describe, expect, it, vi } from "vitest";
2​
3import { BackendReadinessAbortedError } from "./backendReadiness.ts";
4import { waitForBackendStartupReady } from "./backendStartupReadiness.ts";
5​
6describe("waitForBackendStartupReady", () => {
7 it("falls back to the HTTP probe when no listening signal exists", async () => {
8 const waitForHttpReady = vi.fn<() => Promise<void>>().mockResolvedValue(undefined);
9 const cancelHttpWait = vi.fn();
10​
11 await expect(
12 waitForBackendStartupReady({
13 waitForHttpReady,
14 cancelHttpWait,
15 }),
16 ).resolves.toBe("http");
17​
18 expect(waitForHttpReady).toHaveBeenCalledTimes(1);
19 expect(cancelHttpWait).not.toHaveBeenCalled();
20 });
21​
22 it("uses the listening signal and cancels the HTTP probe", async () => {
23 let rejectHttpWait: ((error: unknown) => void) | null = null;
24 const waitForHttpReady = vi.fn(
25 () =>
26 new Promise<void>((_resolve, reject) => {
27 rejectHttpWait = reject;
28 }),
29 );
30 const cancelHttpWait = vi.fn(() => {
31 rejectHttpWait?.(new BackendReadinessAbortedError());
32 });
33​
34 await expect(
35 waitForBackendStartupReady({
36 listeningPromise: Promise.resolve(),
37 waitForHttpReady,
38 cancelHttpWait,
39 }),
40 ).resolves.toBe("listening");
41​
42 expect(waitForHttpReady).toHaveBeenCalledTimes(1);
43 expect(cancelHttpWait).toHaveBeenCalledTimes(1);
44 });
45​
46 it("rejects when the listening signal fails before HTTP readiness", async () => {
47 const error = new Error("backend exited");
48 const waitForHttpReady = vi.fn(() => new Promise<void>(() => {}));
49​
50 await expect(
51 waitForBackendStartupReady({
52 listeningPromise: Promise.reject(error),
53 waitForHttpReady,
54 cancelHttpWait: vi.fn(),
55 }),
56 ).rejects.toBe(error);
57 });
58});