




















@@ -33,6 +33,16 @@ function makeErrServer(code: string): net.Server {
3333return fake;
3434}
353536+async function expectRejectCode(promise: Promise<unknown>, code: string): Promise<void> {
37+try {
38+await promise;
39+} catch (error) {
40+expect((error as NodeJS.ErrnoException).code).toBe(code);
41+return;
42+}
43+throw new Error(`expected rejection with code ${code}`);
44+}
45+3646describe("probePortFree", () => {
3747it("resolves false (not rejects) when bind returns EADDRINUSE", async () => {
3848mockCreateServer.mockReturnValue(makeErrServer("EADDRINUSE"));
@@ -41,17 +51,17 @@ describe("probePortFree", () => {
41514252it("rejects immediately for EADDRNOTAVAIL (non-retryable: host address not on any interface)", async () => {
4353mockCreateServer.mockReturnValue(makeErrServer("EADDRNOTAVAIL"));
44-await expect(probePortFree(9999, "192.0.2.1")).rejects.toMatchObject({ code: "EADDRNOTAVAIL" });
54+await expectRejectCode(probePortFree(9999, "192.0.2.1"), "EADDRNOTAVAIL");
4555});
46564757it("rejects immediately for EACCES (non-retryable bind error)", async () => {
4858mockCreateServer.mockReturnValue(makeErrServer("EACCES"));
49-await expect(probePortFree(80, "0.0.0.0")).rejects.toMatchObject({ code: "EACCES" });
59+await expectRejectCode(probePortFree(80, "0.0.0.0"), "EACCES");
5060});
51615262it("rejects immediately for other non-retryable errors", async () => {
5363mockCreateServer.mockReturnValue(makeErrServer("EINVAL"));
54-await expect(probePortFree(9999, "0.0.0.0")).rejects.toMatchObject({ code: "EINVAL" });
64+await expectRejectCode(probePortFree(9999, "0.0.0.0"), "EINVAL");
5565});
56665767it("resolves true when the port is free", async () => {
@@ -115,9 +125,7 @@ describe("waitForPortBindable", () => {
115125// mockCreateServer would be called many times. We assert it's called exactly once.
116126mockCreateServer.mockClear();
117127mockCreateServer.mockReturnValue(makeErrServer("EACCES"));
118-await expect(
119-waitForPortBindable(80, { timeoutMs: 5000, intervalMs: 50 }),
120-).rejects.toMatchObject({ code: "EACCES" });
128+await expectRejectCode(waitForPortBindable(80, { timeoutMs: 5000, intervalMs: 50 }), "EACCES");
121129// Only one probe should have been attempted — no spinning through the retry loop.
122130expect(mockCreateServer).toHaveBeenCalledTimes(1);
123131});
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。