diff --git a/src/infra/http-body.test.ts b/src/infra/http-body.test.ts index d5599c623..cfe693486 100644 --- a/src/infra/http-body.test.ts +++ b/src/infra/http-body.test.ts @@ -15,6 +15,10 @@ type MockIncomingMessage = IncomingMessage & { __unhandledDestroyError?: unknown; }; +async function waitForMicrotaskTurn(): Promise { + await new Promise((resolve) => queueMicrotask(resolve)); +} + function createMockRequest(params: { chunks?: string[]; headers?: Record; @@ -101,7 +105,7 @@ describe("http body limits", () => { const req = createMockRequest({ chunks: ["small", "x".repeat(256)], emitEnd: false }); const res = createMockServerResponse(); const guard = installRequestBodyLimitGuard(req, res, { maxBytes: 128, responseFormat: "text" }); - await new Promise((resolve) => setTimeout(resolve, 0)); + await waitForMicrotaskTurn(); expect(guard.isTripped()).toBe(true); expect(guard.code()).toBe("PAYLOAD_TOO_LARGE"); expect(res.statusCode).toBe(413); @@ -127,7 +131,7 @@ describe("http body limits", () => { message: "PayloadTooLarge", }); // Wait a tick for any async destroy(err) emission. - await new Promise((resolve) => setTimeout(resolve, 0)); + await waitForMicrotaskTurn(); expect(req.__unhandledDestroyError).toBeUndefined(); }); }); diff --git a/src/media/input-files.fetch-guard.test.ts b/src/media/input-files.fetch-guard.test.ts index f731b90c0..3ae622564 100644 --- a/src/media/input-files.fetch-guard.test.ts +++ b/src/media/input-files.fetch-guard.test.ts @@ -6,6 +6,10 @@ vi.mock("../infra/net/fetch-guard.js", () => ({ fetchWithSsrFGuard: (...args: unknown[]) => fetchWithSsrFGuardMock(...args), })); +async function waitForMicrotaskTurn(): Promise { + await new Promise((resolve) => queueMicrotask(resolve)); +} + describe("fetchWithGuard", () => { it("rejects oversized streamed payloads and cancels the stream", async () => { let canceled = false; @@ -47,7 +51,7 @@ describe("fetchWithGuard", () => { ).rejects.toThrow("Content too large"); // Allow cancel() microtask to run. - await new Promise((resolve) => setTimeout(resolve, 0)); + await waitForMicrotaskTurn(); expect(canceled).toBe(true); expect(release).toHaveBeenCalledTimes(1);