Files
stack/apps/api/vitest.setup.ts
Jason Woltje 75766a37b4
Some checks failed
ci/woodpecker/push/woodpecker Pipeline failed
fix(test): Skip loading .env.test in CI environments
The .env.test file was being loaded in CI and overriding the CI-provided
DATABASE_URL, causing tests to try connecting to localhost:5432 instead of
the postgres:5432 service.

Fix: Only load .env.test when NOT in CI (check for CI or WOODPECKER env vars).

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-07 21:44:02 -06:00

34 lines
1.2 KiB
TypeScript

import "reflect-metadata";
import * as dotenv from "dotenv";
import * as path from "path";
import * as fs from "fs";
// Load environment variables from .env.test if it exists
// This allows local integration tests to run with a local database
// CI environments explicitly provide DATABASE_URL in the test step, so skip loading .env.test there
const envTestPath = path.resolve(__dirname, ".env.test");
const isCI = process.env.CI === "true" || process.env.WOODPECKER === "true";
if (!isCI && fs.existsSync(envTestPath)) {
const result = dotenv.config({ path: envTestPath });
if (result.error) {
throw new Error(
`Failed to load test environment configuration: ${result.error.message}\n` +
`Ensure .env.test exists in the api directory and is properly formatted.`
);
}
// Validate DATABASE_URL format if provided
if (process.env.DATABASE_URL && !process.env.DATABASE_URL.startsWith("postgresql://")) {
throw new Error(
"Invalid DATABASE_URL format in .env.test. " +
"Expected format: postgresql://user:password@host:port/database"
);
}
// Log only in debug mode
if (process.env.DEBUG) {
console.debug("Test environment variables loaded from .env.test");
}
}