tests cannot connect to db
Some checks failed
Deploy to Web Server flyer-crawler.projectium.com / deploy (push) Failing after 1m8s

This commit is contained in:
2025-11-23 09:10:12 -08:00
parent 8df3fd0291
commit 93f5481f94
3 changed files with 20 additions and 5 deletions

View File

@@ -11,7 +11,7 @@
"test": "vitest run",
"test:coverage": "npm run clean && npm run test:unit -- --coverage && npm run test:integration -- --coverage",
"test:unit": "vitest run -c vite.config.ts",
"test:integration": "npm run db:reset:test && vitest run -c vitest.config.integration.ts",
"test:integration": "vitest run -c vitest.config.integration.ts",
"lint": "eslint . --ext ts,tsx --report-unused-disable-directives --max-warnings 0",
"clean": "rimraf coverage .coverage",
"start:dev": "tsx --env-file .env.test server.ts",

View File

@@ -65,7 +65,10 @@ const refreshToken = async (): Promise<string> => {
export const apiFetch = async (url: string, options: RequestInit = {}, tokenOverride?: string): Promise<Response> => {
// In the Node.js test environment, we must use the full URL.
// In the browser, relative URLs are fine.
const fullUrl = (typeof window === 'undefined' && !url.startsWith('http')) ? `${API_BASE_URL}${url.startsWith('/') ? '' : '/'}${url}` : url;
// This logic now correctly handles joining the base URL and the path.
const fullUrl = (typeof window === 'undefined' && !url.startsWith('http'))
? new URL(url, API_BASE_URL).href
: url;
logger.debug(`apiFetch: ${options.method || 'GET'} ${fullUrl}`);
@@ -127,7 +130,9 @@ export const apiFetch = async (url: string, options: RequestInit = {}, tokenOver
*/
export const apiFetchWithAuth = async (url: string, options: RequestInit): Promise<Response> => {
// In the Node.js test environment, we must use the full URL.
const fullUrl = (typeof window === 'undefined' && !url.startsWith('http')) ? `${API_BASE_URL}${url.startsWith('/') ? '' : '/'}${url}` : url;
const fullUrl = (typeof window === 'undefined' && !url.startsWith('http'))
? new URL(url, API_BASE_URL).href
: url;
logger.debug(`apiFetchWithAuth: ${options.method || 'GET'} ${fullUrl}`);

View File

@@ -1,13 +1,23 @@
import { exec, ChildProcess } from 'child_process';
import { setup as globalSetup } from './global-setup';
import { logger } from '../../services/logger';
import { getPool } from '../../services/db/connection'; // Import getPool
import { execSync } from 'child_process';
let serverProcess: ChildProcess;
export async function setup() {
console.log('\n--- Running Integration Test Setup ---');
await globalSetup();
// The integration setup is now the single source of truth for preparing the test DB.
// It runs the same seed script that `npm run db:reset:test` used.
try {
console.log('Resetting and seeding test database...');
execSync('npm run db:reset:test', { stdio: 'inherit' });
console.log('✅ Test database is ready.');
} catch (error) {
console.error('🔴 Failed to reset and seed the test database. Aborting tests.', error);
process.exit(1);
}
console.log('Starting backend server for integration tests...');