Files
bun.sh/test/cli/run/sql-preconnect.test.ts
jarred-sumner-bot e9ccc81e03 Add --sql-preconnect CLI flag for PostgreSQL startup connections (#21035)
Co-authored-by: Claude Bot <claude-bot@bun.sh>
Co-authored-by: Claude <noreply@anthropic.com>
Co-authored-by: jarred-sumner-bot <220441119+jarred-sumner-bot@users.noreply.github.com>
Co-authored-by: Jarred Sumner <jarred@jarredsumner.com>
Co-authored-by: Jarred-Sumner <709451+Jarred-Sumner@users.noreply.github.com>
2025-07-14 15:05:30 -07:00

85 lines
2.1 KiB
TypeScript

import { describe, expect, test } from "bun:test";
import { bunEnv, bunExe, tempDirWithFiles } from "harness";
describe("--sql-preconnect", () => {
test("should attempt to preconnect to PostgreSQL on startup", async () => {
let connectionAttempts = 0;
const { promise, resolve } = Promise.withResolvers<void>();
await using server = Bun.listen({
port: 0,
hostname: "127.0.0.1",
socket: {
open(socket) {
connectionAttempts++;
socket.end();
if (connectionAttempts >= 1) {
resolve();
}
},
data() {},
close() {},
},
});
const testDir = tempDirWithFiles("sql-preconnect-test", {
"index.js": `console.log("Script executed");`,
});
const proc = Bun.spawn({
cmd: [bunExe(), "--sql-preconnect", "index.js"],
env: {
...bunEnv,
DATABASE_URL: `postgres://127.0.0.1:${server.port}/MY_DATABASE`,
},
cwd: testDir,
});
await promise;
proc.kill();
await proc.exited;
expect(connectionAttempts).toBeGreaterThan(0);
});
test("should not connect when flag is not used", async () => {
let connectionAttempts = 0;
await using server = Bun.listen({
port: 0,
hostname: "127.0.0.1",
socket: {
open(socket) {
connectionAttempts++;
socket.end();
},
data() {},
close() {},
},
});
const testDir = tempDirWithFiles("sql-no-preconnect", {
"index.js": `console.log("Normal script executed");`,
});
await using proc = Bun.spawn({
cmd: [bunExe(), "index.js"],
env: {
...bunEnv,
DATABASE_URL: `postgres://127.0.0.1:${server.port}/MY_DATABASE`,
},
cwd: testDir,
});
const [stdout, stderr, exitCode] = await Promise.all([
new Response(proc.stdout).text(),
new Response(proc.stderr).text(),
proc.exited,
]);
expect(exitCode).toBe(0);
expect(stdout).toContain("Normal script executed");
expect(connectionAttempts).toBe(0); // No connection should be attempted without the flag
});
});