From 774555adc74c2606ef3ab76ac618c9b0f81389e8 Mon Sep 17 00:00:00 2001 From: Torben Sorensen Date: Sat, 22 Nov 2025 11:05:43 -0800 Subject: [PATCH] unit tests fixin --- server.ts | 12 +++++++++++- src/components/auth.integration.test.ts | 9 +++++++++ src/services/db.integration.test.ts | 6 +----- src/services/shopping-list.integration.test.ts | 6 +----- src/tests/setup/global-setup.ts | 9 +++++---- src/tests/setup/integration-global-setup.ts | 4 ++++ 6 files changed, 31 insertions(+), 15 deletions(-) diff --git a/server.ts b/server.ts index 46e2d443..ae9bd91b 100644 --- a/server.ts +++ b/server.ts @@ -1,5 +1,4 @@ import express, { Request, Response, NextFunction } from 'express'; -import dotenv from 'dotenv'; import cookieParser from 'cookie-parser'; import passport from './src/routes/passport'; @@ -15,6 +14,17 @@ import aiRouter from './src/routes/ai'; // Environment variables are now loaded by the `tsx` command in package.json scripts. // This ensures the correct .env file is used for development vs. testing. +// --- START DEBUG LOGGING --- +// Log the database connection details as seen by the SERVER PROCESS. +// This will confirm if the `--env-file` flag is working as expected. +logger.info('--- [SERVER PROCESS LOG] DATABASE CONNECTION ---'); +logger.info(` NODE_ENV: ${process.env.NODE_ENV}`); +logger.info(` Host: ${process.env.DB_HOST}`); +logger.info(` Port: ${process.env.DB_PORT}`); +logger.info(` User: ${process.env.DB_USER}`); +logger.info(` Database: ${process.env.DB_DATABASE}`); +logger.info('-----------------------------------------------\n'); + const app = express(); // --- Core Middleware --- diff --git a/src/components/auth.integration.test.ts b/src/components/auth.integration.test.ts index 6ef13ee0..0fee0d70 100644 --- a/src/components/auth.integration.test.ts +++ b/src/components/auth.integration.test.ts @@ -12,6 +12,15 @@ import { loginUser } from '../services/apiClient'; * To run only these tests: `vitest run src/tests/auth.integration.test.ts` */ describe('Authentication API Integration', () => { + // --- START DEBUG LOGGING --- + // Log the database connection details as seen by an individual TEST FILE. + console.log('\n\n--- [AUTH.INTEGRATION.TEST LOG] DATABASE CONNECTION ---'); + console.log(` Host: ${process.env.DB_HOST}`); + console.log(` Port: ${process.env.DB_PORT}`); + console.log(` User: ${process.env.DB_USER}`); + console.log(` Database: ${process.env.DB_DATABASE}`); + console.log('-----------------------------------------------------\n'); + // --- END DEBUG LOGGING --- // This test migrates the logic from the old DevTestRunner.tsx component. it('should successfully log in the admin user', async () => { diff --git a/src/services/db.integration.test.ts b/src/services/db.integration.test.ts index 6ea29a1c..a13488c5 100644 --- a/src/services/db.integration.test.ts +++ b/src/services/db.integration.test.ts @@ -1,4 +1,4 @@ -import { describe, it, expect, afterEach, afterAll } from 'vitest'; +import { describe, it, expect, afterEach } from 'vitest'; import * as db from './db'; import bcrypt from 'bcrypt'; import { pool } from './db/connection'; // Import the main pool @@ -11,10 +11,6 @@ describe('Database Service Integration Tests', () => { await pool.query('TRUNCATE public.users RESTART IDENTITY CASCADE'); }); - afterAll(async () => { - await pool.end(); // Close the connection pool after all tests in this file - }); - it('should create a new user and be able to find them by email', async () => { // Arrange: Define the new user's data const email = 'test.user@example.com'; diff --git a/src/services/shopping-list.integration.test.ts b/src/services/shopping-list.integration.test.ts index 4654958a..592d4ce0 100644 --- a/src/services/shopping-list.integration.test.ts +++ b/src/services/shopping-list.integration.test.ts @@ -1,4 +1,4 @@ -import { describe, it, expect, beforeEach, afterEach, afterAll } from 'vitest'; +import { describe, it, expect, beforeEach, afterEach } from 'vitest'; import * as db from './db'; import { pool } from './db/connection'; // Import the main pool import bcrypt from 'bcrypt'; @@ -22,10 +22,6 @@ describe('Shopping List DB Service Tests', () => { await pool.query('TRUNCATE public.users, public.profiles, public.shopping_lists, public.shopping_list_items RESTART IDENTITY CASCADE'); }); - afterAll(async () => { - await pool.end(); // Close the connection pool after all tests in this file - }); - it('should create and retrieve a shopping list for a user', async () => { // Arrange: The `beforeEach` hook already creates a user, which in turn // triggers the creation of a "Main Shopping List". We then create two more. diff --git a/src/tests/setup/global-setup.ts b/src/tests/setup/global-setup.ts index 500613b2..19229a48 100644 --- a/src/tests/setup/global-setup.ts +++ b/src/tests/setup/global-setup.ts @@ -22,14 +22,15 @@ const getPool = () => { */ export async function setup() { // --- START DEBUG LOGGING --- - // Log the database connection details being used by this setup script. - // This will help us verify if it matches the server's connection details. - console.log('--- [TEST SETUP] Database Connection Details ---'); + // Log the database connection details being used by the Vitest GLOBAL SETUP process. + // These variables are inherited from the CI environment. + console.log('\n\n--- [GLOBAL SETUP LOG] DATABASE CONNECTION ---'); console.log(` Host: ${process.env.DB_HOST}`); console.log(` Port: ${process.env.DB_PORT}`); console.log(` User: ${process.env.DB_USER}`); console.log(` Database: ${process.env.DB_DATABASE}`); - console.log('-------------------------------------------------'); + console.log('-------------------------------------------\n'); + // --- END DEBUG LOGGING --- console.log('\nResetting test database schema...'); const pool = getPool(); diff --git a/src/tests/setup/integration-global-setup.ts b/src/tests/setup/integration-global-setup.ts index 8e44c7aa..00a1a141 100644 --- a/src/tests/setup/integration-global-setup.ts +++ b/src/tests/setup/integration-global-setup.ts @@ -2,6 +2,7 @@ import { exec } from 'child_process'; import { setup as globalSetup } from './global-setup'; import { pingBackend } from '../../services/apiClient'; import { logger } from '../../services/logger'; +import { pool } from '../../services/db/connection'; export async function setup() { console.log('\n--- Running Integration Test Setup ---'); @@ -59,4 +60,7 @@ export async function setup() { export async function teardown() { console.log('--- Integration Test Teardown ---'); + // Close the database connection pool after all integration tests have run. + // This prevents the "hanging-process" error. + await pool.end(); } \ No newline at end of file