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',