From c8db416f11bd9426340c6d3893326f0c188a148c Mon Sep 17 00:00:00 2001 From: Ryan Martin Date: Mon, 8 Jun 2026 13:57:24 -0500 Subject: [PATCH] fix(db): load repo-root .env in the vitest config MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The integration suite read DB config straight from process.env (via createDb), but nothing loaded the repo-root .env for the vitest process — only migrate.ts and test-connection.ts did. So `pnpm db:test` (test-connection) worked while `pnpm test` (vitest) failed with "No DB config: set DATABASE_URL", even though the global-setup migrations applied (the migrate child loads .env itself). Load the root .env at the top of vitest.config.ts so DATABASE_URL / PG_* reach both globalSetup and the test workers. dotenv does not override vars already set, so an inline `DATABASE_URL=… pnpm test` still wins. Co-Authored-By: Claude Opus 4.8 --- packages/db/vitest.config.ts | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/packages/db/vitest.config.ts b/packages/db/vitest.config.ts index c17f6af..34f64fa 100644 --- a/packages/db/vitest.config.ts +++ b/packages/db/vitest.config.ts @@ -8,13 +8,28 @@ // // DATABASE_URL=postgres://app:app@localhost:5432/app_test pnpm --filter @app/db test // +// DB config also resolves from the repo-root .env (loaded below) — the migrate +// CLI and test-connection already do this, so the suite stays consistent with +// them instead of only working when DATABASE_URL is set inline in the shell. +// // Tests import the BUILT packages (`@app/db`, `@app/types`) via their exports // map, so `pretest` builds them first. That sidesteps NodeNext `.js`-import // resolution issues that arise when transpiling the TS source on the fly. // ============================================================================= +import * as path from 'node:path'; +import { fileURLToPath } from 'node:url'; + +import { config as dotenvConfig } from 'dotenv'; import { defineConfig } from 'vitest/config'; +// Load the repo-root .env so DATABASE_URL / PG_* reach both globalSetup (the +// migrate CLI) and the test workers' createDb(). dotenv does NOT override vars +// already in the environment, so an inline `DATABASE_URL=… pnpm test` still wins. +// vitest.config.ts is at packages/db/, so the root .env is two dirs up. +const configDir = path.dirname(fileURLToPath(import.meta.url)); +dotenvConfig({ path: path.resolve(configDir, '..', '..', '.env') }); + export default defineConfig({ test: { environment: 'node',