Files
euchre_camp/vitest.setup.ts
T

63 lines
2.1 KiB
TypeScript

import '@testing-library/jest-dom'
import path from 'path'
import fs from 'fs'
/**
* Vitest Setup File
*
* This file runs before each test file in the Vitest environment.
* It validates that tests are running against the development database
* to prevent accidental data corruption.
*/
// Load .env file first, then .env.development (which will override .env)
const envPath = path.resolve(process.cwd(), '.env');
const envDevPath = path.resolve(process.cwd(), '.env.development');
// Load base .env file
if (fs.existsSync(envPath)) {
require('dotenv').config({ path: envPath });
}
// Load .env.development file (will override .env settings)
if (fs.existsSync(envDevPath)) {
require('dotenv').config({ path: envDevPath, override: true });
}
// Check if DATABASE_URL is set and points to dev database
const databaseUrl = process.env.DATABASE_URL;
if (databaseUrl) {
const isDevDatabase = databaseUrl.includes('euchre_camp_dev');
const isProductionDatabase =
databaseUrl.includes('euchre_camp') &&
!databaseUrl.includes('_dev') &&
!databaseUrl.includes('_dev_');
if (isProductionDatabase) {
console.error('');
console.error('='.repeat(80));
console.error('CRITICAL ERROR: Tests are attempting to run against PRODUCTION database!');
console.error('='.repeat(80));
console.error('');
console.error('Current DATABASE_URL:', databaseUrl);
console.error('');
console.error('Tests MUST run against the development database (euchre_camp_dev)');
console.error('');
console.error('To fix this:');
console.error(' 1. Run: npm run test:run');
console.error(' 2. Or set: DATABASE_URL environment variable to dev database URL');
console.error(' 3. Or load .env.development: source .env.development && npm run test:run');
console.error('');
console.error('Aborting test execution to prevent data corruption.');
console.error('');
process.exit(1);
}
if (isDevDatabase) {
console.log('✓ Tests running against development database (euchre_camp_dev)');
}
} else {
console.warn('⚠ No DATABASE_URL set - tests may fail or use unexpected database');
}