splitting unit + integration testing apart attempt #1
All checks were successful
Deploy to Web Server flyer-crawler.projectium.com / deploy (push) Successful in 3m51s
All checks were successful
Deploy to Web Server flyer-crawler.projectium.com / deploy (push) Successful in 3m51s
This commit is contained in:
@@ -2,13 +2,6 @@
|
|||||||
import { Pool, PoolConfig } from 'pg';
|
import { Pool, PoolConfig } from 'pg';
|
||||||
import { logger } from '../logger';
|
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 {
|
interface TrackedPool extends Pool {
|
||||||
poolId?: string;
|
poolId?: string;
|
||||||
}
|
}
|
||||||
@@ -42,19 +35,17 @@ const createPool = (): Pool => {
|
|||||||
console.log(`[DEBUG] connection.ts: typeof Pool is "${typeof Pool}"`);
|
console.log(`[DEBUG] connection.ts: typeof Pool is "${typeof Pool}"`);
|
||||||
if (typeof Pool !== 'function') {
|
if (typeof Pool !== 'function') {
|
||||||
console.error('[DEBUG] CRITICAL: Pool is NOT a function/class!', Pool);
|
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 {
|
try {
|
||||||
const newPool: TrackedPool = new Pool(poolConfig);
|
const newPool: TrackedPool = new Pool(poolConfig);
|
||||||
newPool.poolId = poolId;
|
newPool.poolId = poolId;
|
||||||
|
|
||||||
|
// --- ADDED SUCCESS LOG ---
|
||||||
|
console.log('[DEBUG] connection.ts: Successfully instantiated Pool.');
|
||||||
|
// -------------------------
|
||||||
|
|
||||||
return newPool;
|
return newPool;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('[DEBUG] connection.ts: "new Pool()" threw error:', error);
|
console.error('[DEBUG] connection.ts: "new Pool()" threw error:', error);
|
||||||
@@ -69,10 +60,10 @@ const createPool = (): Pool => {
|
|||||||
export const getPool = (): Pool => {
|
export const getPool = (): Pool => {
|
||||||
// This function acts as a singleton accessor for the database pool.
|
// This function acts as a singleton accessor for the database pool.
|
||||||
if (!poolInstance) {
|
if (!poolInstance) {
|
||||||
console.log('[DB POOL] Creating NEW pool instance.'); // DIAGNOSTIC LOGGING #2
|
console.log('[DB POOL] Creating NEW pool instance.');
|
||||||
poolInstance = createPool();
|
poolInstance = createPool();
|
||||||
} else {
|
} 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;
|
return poolInstance;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -24,8 +24,12 @@ export const mockPoolInstance = {
|
|||||||
waitingCount: 0,
|
waitingCount: 0,
|
||||||
};
|
};
|
||||||
|
|
||||||
// Define the mock constructor that returns our stable instance.
|
// FIX: Use a standard function for the constructor.
|
||||||
export const MockPool = vi.fn(() => mockPoolInstance);
|
// 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.
|
// This is the actual mock factory function for vi.mock.
|
||||||
export const pgMockFactory = () => ({
|
export const pgMockFactory = () => ({
|
||||||
|
|||||||
@@ -56,7 +56,7 @@ afterEach(cleanup);
|
|||||||
// 1. Define the mock pool instance and constructor *outside* vi.mock first
|
// 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
|
// We use vi.hoisted to ensure these variables are available to the mock factory
|
||||||
const { mockPoolInstance, MockPool } = vi.hoisted(() => {
|
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 mockQuery = vi.fn().mockResolvedValue({ rows: [], rowCount: 0 });
|
||||||
const mockRelease = vi.fn();
|
const mockRelease = vi.fn();
|
||||||
const mockConnect = vi.fn().mockResolvedValue({
|
const mockConnect = vi.fn().mockResolvedValue({
|
||||||
@@ -75,10 +75,12 @@ const { mockPoolInstance, MockPool } = vi.hoisted(() => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
// The constructor function
|
// The constructor function
|
||||||
const Constructor = vi.fn(() => {
|
// FIX: Use a standard function so 'new' works, and log success
|
||||||
console.log('[DEBUG] unit-setup.ts: MockPool constructor called!'); // ADD THIS
|
const Constructor = vi.fn(function() {
|
||||||
|
console.log('[DEBUG] unit-setup.ts: MockPool constructor called successfully via "new"!');
|
||||||
return instance;
|
return instance;
|
||||||
});
|
});
|
||||||
|
|
||||||
return { mockPoolInstance: instance, MockPool: Constructor };
|
return { mockPoolInstance: instance, MockPool: Constructor };
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -87,7 +89,7 @@ const { mockPoolInstance, MockPool } = vi.hoisted(() => {
|
|||||||
*/
|
*/
|
||||||
// 2. Mock 'pg' using the hoisted variables
|
// 2. Mock 'pg' using the hoisted variables
|
||||||
vi.mock('pg', () => {
|
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 {
|
return {
|
||||||
// Named export must be the constructor
|
// Named export must be the constructor
|
||||||
Pool: MockPool,
|
Pool: MockPool,
|
||||||
|
|||||||
Reference in New Issue
Block a user