porsager / postgres

Postgres.js - The Fastest full featured PostgreSQL client for Node.js, Deno, Bun and CloudFlare
The Unlicense
7.26k stars 262 forks source link

Cannot use in multiple tests in Bun #682

Closed derTuca closed 10 months ago

derTuca commented 11 months ago

Hi! I have a Bun + Elysia app and I'm trying to write tests for the Elysia routes (and they all use Postgres.js). I have a .test.ts file for each test but the issue is when I run bun test the process hangs after the first file finishes. If I add the block of code below it passes to the second file but I get a CONNECTION_CLOSED error:

afterAll(async () => {
  await sql.end()
})

Is there a specific way I can reinitialize the connection or tell the tester to ignore the ongoing one?

porsager commented 11 months ago

Could you try setting idle_timeout to 0?

derTuca commented 11 months ago

I tried it now but it behaves the same way.

masfahru commented 11 months ago

Hi! I have a Bun + Elysia app and I'm trying to write tests for the Elysia routes (and they all use Postgres.js). I have a .test.ts file for each test but the issue is when I run bun test the process hangs after the first file finishes. If I add the block of code below it passes to the second file but I get a CONNECTION_CLOSED error:

afterAll(async () => {
  await sql.end()
})

I got the same behavior when using an imported global Postgres sql instance. My workaround is to create a local Postgres sql instance for each test file.

For example, suppose I have an exported method called createAdmin from ./crud. The method accepts two parameters: admin object and a Postgres sql object, and returns a promise that resolves to the id of the created admin. This is a simple test for it:

create-admin.test.ts

import { databaseConfig } from "@config/database";
import postgres from "postgres";
import { createAdmin } from "./crud";
import { afterAll, describe, expect, it } from "bun:test";

describe("Database Test: Create Admin", () => {
  const sql = postgres({
    host: databaseConfig.host,
    port: databaseConfig.port,
    username: databaseConfig.user,
    password: databaseConfig.password,
    database: databaseConfig.database,
    ssl: databaseConfig.ssl,
    transform: postgres.camel,
  });

  afterAll(async () => {
    await sql.end();
  });

  it("should create an admin", async () => {
    const admin = {
      userName: "admin",
      email: "admin@example.com",
      name: "Admin",
    };
    const id = await createAdmin(admin, sql);
    expect(id).toBe(1);
  });
});

this test won't hang and won't throw an error

porsager commented 10 months ago

Mind giving this a shot with latest bun? I think there were quite some fixes around connections and setImmediate and setTimeout

porsager commented 10 months ago

Reopen if there are still issues

Th1nkK1D commented 9 months ago

I still found this issue on bun version 1.0.11 which happened to postgres.js, pg, and also other ORMs as reported in https://github.com/oven-sh/bun/issues/4035. Look like it has something to do with Bun and the Postgres connection that's hanging after the test.

Th1nkK1D commented 9 months ago

Tested again on Bun 1.0.13 and the issue is solved.