Skip to content

fix(db): load repo-root .env in the vitest config#44

Open
rmartin93 wants to merge 1 commit into
mainfrom
fix/db-test-loads-env
Open

fix(db): load repo-root .env in the vitest config#44
rmartin93 wants to merge 1 commit into
mainfrom
fix/db-test-loads-env

Conversation

@rmartin93

Copy link
Copy Markdown
Contributor

Problem

pnpm db:test worked but pnpm test failed with "No DB config: set DATABASE_URL" — the vitest integration suite never loaded the repo-root .env.

migrate.ts and test-connection.ts each call dotenvConfig({ path: root .env }); vitest.config.ts did not. So the suite only saw DATABASE_URL when it was set inline in the shell. Confusingly, global setup's migrations still applied (the migrate child process loads .env itself), then the test workers' createDb() threw because the vitest process had no DATABASE_URL/PG_*.

Fix

Load the repo-root .env at the top of vitest.config.ts (evaluated in the main process before globalSetup runs and before workers fork, so both inherit it). dotenv doesn't override already-set variables, so an inline DATABASE_URL=… pnpm test still wins.

Verification

DB suite not run here, but the env load was verified directly: a key from the root .env is absent from the shell and present after the config's dotenv load resolves the root .env.

Same issue exists in the IIS app-starter-iis repo and is fixed there in a parallel PR.

🤖 Generated with Claude Code

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 <noreply@anthropic.com>
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.

1 participant