test: use randomized listeners in serve-types.test.ts (#20387)

Co-authored-by: nektro <5464072+nektro@users.noreply.github.com>
This commit is contained in:
Meghan Denny
2025-06-15 12:20:11 -08:00
committed by GitHub
parent 155475693b
commit 2245b5efd6

View File

@@ -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<T>(value: unknown, constructor: new (...args: any[]) => T): asserts value is T {
expect(value).toBeInstanceOf(constructor);
}
@@ -39,7 +47,6 @@ function test<T, R extends { [K in keyof R]: Bun.RouterTypes.RouteValue<K & stri
it(`Bun.serve() types test ${++id}`, async () => {
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;