Skip to content

"bun test" hangs up in timeout with PostgreSQL and expect->toThrow() #19130

@wertania

Description

@wertania

What version of Bun is running?

1.2.10

What platform is your computer?

Microsoft Windows NT 10.0.26100.0, x64

What steps can reproduce the bug?

Unit test with "Bun test" are handing up when "Test 3" is active with expect(async () => {}).toThrow();.

If I drop "Test 3" everything is running.
Also if I replace the inner function with something that is not working with the DB it will work.

But with a database function inside it will run into timeout and will never close.
It hangs up and I need to kill the process from console.

Image

I wrote a simple test to reproduce:

import { describe, test, expect, beforeAll, afterAll } from "bun:test";
import { getDbSchema, users, type DatabaseSchema } from "../../db/db-schema";
import { BunSQLDatabase, drizzle } from "drizzle-orm/bun-sql";
import { SQL } from "bun";

/**
 * DB connection
 */
let dbClient = new SQL(
  `postgresql://${process.env.POSTGRES_USER}:${process.env.POSTGRES_PASSWORD}@${process.env.POSTGRES_HOST}:${process.env.POSTGRES_PORT}/${process.env.POSTGRES_DB}`
);
let drizzleClient: BunSQLDatabase<DatabaseSchema>;

const connectToDb = async () => {
  const schema = getDbSchema();
  drizzleClient = drizzle(dbClient, { schema, logger: false });
  return drizzleClient;
};
const cleanUpAllData = async () => {
  // do something with the db
  await drizzleClient.select().from(users);
};

/**
 * Mock functions
 */
const addSomeData = async () => {
  // do something with the db
  const r = await drizzleClient.select().from(users);
  return {
    id: r[0].id,
  };
};
const doSomething = async (id: string) => {
  // do something with the db
  const r = await drizzleClient.select().from(users);
  return r[0].id === id;
};
const doSomethingElse = async () => {
  // do something with the db
  const r = await drizzleClient.select().from(users);
  throw new Error("This fails");
};

describe("getKnowledgeEntries Permissions", () => {
  let addedData: { id: string };

  beforeAll(async () => {
    await connectToDb();
    addedData = await addSomeData();
  });

  afterAll(async () => {
    await cleanUpAllData();
  });
  test("Test 1", async () => {
    const result = await doSomething(addedData.id);
    expect(result).toBe(true);
  });

  test("Test 2", async () => {
    expect(async () => {
      await doSomethingElse();
    }).toThrow();
  });

  test("Test 3", async () => {
    const result = await doSomething(addedData.id);
    expect(result).toBe(true);

    expect(async () => {
      await doSomethingElse();
    }).toThrow();
  });
});

What is the expected behavior?

Run the test and close application after that

What do you see instead?

Hanging up and tests are not resolving.

Additional information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingbun:testSomething related to the `bun test` runnersqlSomething to do with `sql` in the "bun" module (postgres, sqlite & mysql)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions