splitting unit + integration testing apart attempt #1
All checks were successful
Deploy to Web Server flyer-crawler.projectium.com / deploy (push) Successful in 3m51s

This commit is contained in:
2025-12-01 09:41:23 -08:00
parent 6a3b071881
commit 2c24489caf
3 changed files with 19 additions and 22 deletions

View File

@@ -2,13 +2,6 @@
import { Pool, PoolConfig } from 'pg';
import { logger } from '../logger';
// This is the singleton instance. It's defined at the module level.
// When Node.js caches this module, every `import` of this file across the
// entire application (including in different test files and the running server)
// will share this exact same `poolInstance` variable. This is the key to
// making the singleton pattern work reliably for our tests.
// Extend the Pool type to hold our custom ID for logging.
interface TrackedPool extends Pool {
poolId?: string;
}
@@ -42,19 +35,17 @@ const createPool = (): Pool => {
console.log(`[DEBUG] connection.ts: typeof Pool is "${typeof Pool}"`);
if (typeof Pool !== 'function') {
console.error('[DEBUG] CRITICAL: Pool is NOT a function/class!', Pool);
} else {
try {
// Check if it's a valid constructor by checking prototype or string representation
console.log('[DEBUG] connection.ts: Pool string repr:', Pool.toString().substring(0, 50));
} catch (e) {
console.log('[DEBUG] connection.ts: Could not stringify Pool');
}
}
// -----------------------------
try {
const newPool: TrackedPool = new Pool(poolConfig);
newPool.poolId = poolId;
// --- ADDED SUCCESS LOG ---
console.log('[DEBUG] connection.ts: Successfully instantiated Pool.');
// -------------------------
return newPool;
} catch (error) {
console.error('[DEBUG] connection.ts: "new Pool()" threw error:', error);
@@ -69,10 +60,10 @@ const createPool = (): Pool => {
export const getPool = (): Pool => {
// This function acts as a singleton accessor for the database pool.
if (!poolInstance) {
console.log('[DB POOL] Creating NEW pool instance.'); // DIAGNOSTIC LOGGING #2
console.log('[DB POOL] Creating NEW pool instance.');
poolInstance = createPool();
} else {
console.log(`[DB POOL] Returning EXISTING pool instance. ID: ${poolInstance.poolId}`); // DIAGNOSTIC LOGGING #2
console.log(`[DB POOL] Returning EXISTING pool instance. ID: ${poolInstance.poolId}`);
}
return poolInstance;
};

View File

@@ -24,8 +24,12 @@ export const mockPoolInstance = {
waitingCount: 0,
};
// Define the mock constructor that returns our stable instance.
export const MockPool = vi.fn(() => mockPoolInstance);
// FIX: Use a standard function for the constructor.
// An arrow function `() => {}` cannot be called with `new`, which was causing the
// "is not a constructor" TypeError across all database tests.
export const MockPool = vi.fn(function() {
return mockPoolInstance;
});
// This is the actual mock factory function for vi.mock.
export const pgMockFactory = () => ({

View File

@@ -56,7 +56,7 @@ afterEach(cleanup);
// 1. Define the mock pool instance and constructor *outside* vi.mock first
// We use vi.hoisted to ensure these variables are available to the mock factory
const { mockPoolInstance, MockPool } = vi.hoisted(() => {
console.log('[DEBUG] unit-setup.ts: Initializing hoisted mock variables'); // ADD THIS
console.log('[DEBUG] unit-setup.ts: Initializing hoisted mock variables');
const mockQuery = vi.fn().mockResolvedValue({ rows: [], rowCount: 0 });
const mockRelease = vi.fn();
const mockConnect = vi.fn().mockResolvedValue({
@@ -75,10 +75,12 @@ const { mockPoolInstance, MockPool } = vi.hoisted(() => {
};
// The constructor function
const Constructor = vi.fn(() => {
console.log('[DEBUG] unit-setup.ts: MockPool constructor called!'); // ADD THIS
// FIX: Use a standard function so 'new' works, and log success
const Constructor = vi.fn(function() {
console.log('[DEBUG] unit-setup.ts: MockPool constructor called successfully via "new"!');
return instance;
});
return { mockPoolInstance: instance, MockPool: Constructor };
});
@@ -87,7 +89,7 @@ const { mockPoolInstance, MockPool } = vi.hoisted(() => {
*/
// 2. Mock 'pg' using the hoisted variables
vi.mock('pg', () => {
console.log('[DEBUG] unit-setup.ts: vi.mock("pg") factory executing'); // ADD THIS
console.log('[DEBUG] unit-setup.ts: vi.mock("pg") factory executing');
return {
// Named export must be the constructor
Pool: MockPool,