From 2245b5efd6dbf2610640a3a4e151f43586c4e626 Mon Sep 17 00:00:00 2001 From: Meghan Denny Date: Sun, 15 Jun 2025 12:20:11 -0800 Subject: [PATCH] test: use randomized listeners in serve-types.test.ts (#20387) Co-authored-by: nektro <5464072+nektro@users.noreply.github.com> --- .../bun-types/fixture/serve-types.test.ts | 33 +++++++++++-------- 1 file changed, 20 insertions(+), 13 deletions(-) diff --git a/test/integration/bun-types/fixture/serve-types.test.ts b/test/integration/bun-types/fixture/serve-types.test.ts index 4a5da8e491..f1c4686beb 100644 --- a/test/integration/bun-types/fixture/serve-types.test.ts +++ b/test/integration/bun-types/fixture/serve-types.test.ts @@ -2,8 +2,16 @@ // on its own to make sure that the types line up with actual implementation of Bun.serve() import { expect, test as it } from "bun:test"; +import fs from "node:fs"; +import os from "node:os"; +import { join } from "node:path"; import { expectType } from "./utilities"; +// XXX: importing this from "harness" caused a failure in bun-types.test.ts +function tmpdirSync(pattern: string = "bun.test."): string { + return fs.mkdtempSync(join(fs.realpathSync.native(os.tmpdir()), pattern)); +} + function expectInstanceOf(value: unknown, constructor: new (...args: any[]) => T): asserts value is T { expect(value).toBeInstanceOf(constructor); } @@ -39,7 +47,6 @@ function test { try { using server = Bun.serve(serveConfig); - try { await testServer(server); } finally { @@ -182,7 +189,7 @@ test( ); test({ - port: 1234, + port: 0, fetch(req, server) { server.upgrade(req); if (Math.random() > 0.5) return undefined; @@ -197,7 +204,7 @@ test({ test( { - unix: "/tmp/bun.sock", + unix: `${tmpdirSync()}/bun.sock`, fetch() { return new Response(); }, @@ -214,7 +221,7 @@ test( test( // @ts-expect-error - TODO Fix this { - unix: "/tmp/bun.sock", + unix: `${tmpdirSync()}/bun.sock`, fetch(req, server) { server.upgrade(req); if (Math.random() > 0.5) return undefined; @@ -234,7 +241,7 @@ test( test( // @ts-expect-error - TODO Fix this { - unix: "/tmp/bun.sock", + unix: `${tmpdirSync()}/bun.sock`, fetch(req, server) { server.upgrade(req); if (Math.random() > 0.5) return undefined; @@ -254,7 +261,7 @@ test( test( { - unix: "/tmp/bun.sock", + unix: `${tmpdirSync()}/bun.sock`, fetch(req, server) { server.upgrade(req); return new Response(); @@ -272,7 +279,7 @@ test( test( // @ts-expect-error - TODO Fix this { - unix: "/tmp/bun.sock", + unix: `${tmpdirSync()}/bun.sock`, fetch(req, server) { if (server.upgrade(req)) { return; @@ -334,7 +341,7 @@ test( test( { - unix: "/tmp/bun.sock", + unix: `${tmpdirSync()}/bun.sock`, routes: { "/": new Response("Hello World"), }, @@ -351,7 +358,7 @@ test( test( // @ts-expect-error - Missing fetch or routes { - unix: "/tmp/bun.sock", + unix: `${tmpdirSync()}/bun.sock`, }, { onConstructorFailure: error => { @@ -469,9 +476,9 @@ test({ test( { - unix: "/tmp/bun.sock", + unix: `${tmpdirSync()}/bun.sock`, // @ts-expect-error - port: 1234, + port: 0, fetch() { return new Response(); }, @@ -487,9 +494,9 @@ test( test( { - unix: "/tmp/bun.sock", + unix: `${tmpdirSync()}/bun.sock`, // @ts-expect-error - port: 1234, + port: 0, fetch(req, server) { server.upgrade(req); if (Math.random() > 0.5) return undefined;