perf(test): replace timeout ticks with microtask waits

This commit is contained in:
Peter Steinberger
2026-02-18 16:23:55 +00:00
parent 983a68c23e
commit d77dcebcb1
2 changed files with 11 additions and 3 deletions

View File

@@ -15,6 +15,10 @@ type MockIncomingMessage = IncomingMessage & {
__unhandledDestroyError?: unknown;
};
async function waitForMicrotaskTurn(): Promise<void> {
await new Promise<void>((resolve) => queueMicrotask(resolve));
}
function createMockRequest(params: {
chunks?: string[];
headers?: Record<string, string>;
@@ -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();
});
});

View File

@@ -6,6 +6,10 @@ vi.mock("../infra/net/fetch-guard.js", () => ({
fetchWithSsrFGuard: (...args: unknown[]) => fetchWithSsrFGuardMock(...args),
}));
async function waitForMicrotaskTurn(): Promise<void> {
await new Promise<void>((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);