Skip to content

fix: use getDb() instead of stale db import in integration tests#1

Closed
gamitej with Copilot wants to merge 9 commits into
mainfrom
copilot/fix-integration-test-db-definition
Closed

fix: use getDb() instead of stale db import in integration tests#1
gamitej with Copilot wants to merge 9 commits into
mainfrom
copilot/fix-integration-test-db-definition

Conversation

Copilot AI commented Apr 9, 2026

Copy link
Copy Markdown
  • Diagnose root cause: Bun's mock.module() is global per test run; auth.test.ts mocks @/database with getDb: () => ({}), which poisons the module registry so todo.test.ts's getDb() returns {} instead of the real drizzle instance
  • Move tests/integration/auth.test.tstests/unit/auth.test.ts (mocks everything; no real DB; unit and integration tests run in separate bun test processes in CI so mocks cannot bleed between them)
Original prompt

Problem

The integration test job is failing with:

TypeError: db.delete is not a function. (In 'db.delete(todos)', 'db.delete' is undefined)
    at <anonymous> (tests/integration/todo.test.ts:33:12)

Root Cause

In src/database/index.ts, db is exported as a module-level let variable initialized to null:

export let db: PostgresJsDatabase<typeof schema> =
  null as unknown as PostgresJsDatabase<typeof schema>;

It is only assigned the real drizzle instance inside startDatabase():

database = drizzle(queryClient, { schema });
db = database;

However, in tests/integration/todo.test.ts, the db binding is imported at module-load time:

import { db, startDatabase, stopDatabase } from "@/database";

Because ES module live bindings for let re-exported from a barrel/index may not update in all bundler/runtime contexts (and Bun's test runner evaluates imports before beforeAll runs), the db reference in the test file remains null when beforeAll calls startDatabase().

The module already exports a getDb() function for exactly this purpose:

export function getDb(): PostgresJsDatabase<typeof schema> {
  if (!database) {
    throw new Error(
      "Database not initialized. Call startDatabase() before accessing db.",
    );
  }
  return database;
}

Fix

In tests/integration/todo.test.ts, replace every direct call to db.delete(...) and db.insert(...) with getDb().delete(...) / getDb().insert(...).

Specifically:

  1. Add getDb to the import from @/database:

    import { getDb, startDatabase, stopDatabase } from "@/database";

    (Remove db from this import.)

  2. Replace all usages of db.delete(...) and db.insert(...) in the file with getDb().delete(...) and getDb().insert(...).

The affected lines are:

  • Line 33: await db.delete(todos);
  • Line 41: await db.delete(todos);
  • Line 72: await db.insert(todos).values([...])
  • Line 86: await db.delete(todos);

Also check tests/integration/helpers.ts for any similar direct use of db and apply the same fix there if needed.

Do not change any application source files — only the test files.

The following is the prior conversation context from the user's chat exploration (may be truncated):

User: can you fix the issue and check the sam the fix you did is working

Assistant: Now I understand the issue. The db export is a module-level let variable initialized to null. When startDatabase() is called in beforeAll, it assigns to the local database variable AND sets db = database, but the imported db binding in the test file is a snapshot of the initial null value — it doesn't reflect the later reassignment.

The fix is to use getDb() instead of the direct db export in the test file, so it always fetches the live initialized instance.

Assistant: We're waiting for your response to the confirmation dialog to proceed with creating the pull request.

This pull request was created from Copilot chat.

Copilot AI changed the title [WIP] Fix integration test by correctly defining db fix: use getDb() instead of stale db import in integration tests Apr 9, 2026
Copilot AI requested a review from gamitej April 9, 2026 14:01
@gamitej gamitej marked this pull request as ready for review April 9, 2026 14:02
… integration tests

Agent-Logs-Url: https://github.com/gamitej/Learning-Bun/sessions/57c202ba-7a88-43f3-936d-50676de30428

Co-authored-by: gamitej <55736105+gamitej@users.noreply.github.com>
Copilot stopped work on behalf of gamitej due to an error April 9, 2026 14:20
@gamitej gamitej force-pushed the main branch 2 times, most recently from 5d25e67 to 16e951d Compare April 9, 2026 14:50
@gamitej gamitej closed this Apr 9, 2026
@gamitej gamitej deleted the copilot/fix-integration-test-db-definition branch April 10, 2026 04:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants