Some checks failed
Deploy to Web Server flyer-crawler.projectium.com / deploy (push) Failing after 37s
29 lines
936 B
TypeScript
29 lines
936 B
TypeScript
import { vi } from 'vitest';
|
|
import pg from 'pg';
|
|
import dotenv from 'dotenv';
|
|
|
|
// Load test-specific environment variables
|
|
dotenv.config({ path: '.env.test' });
|
|
|
|
const { Pool } = pg;
|
|
|
|
vi.mock('pg', async (importOriginal) => {
|
|
const pg = await importOriginal<typeof import('pg')>();
|
|
|
|
// Create the singleton test pool instance *before* mocking the Pool constructor.
|
|
// This instance will be shared across all tests that import the db services.
|
|
const testPool = new pg.Pool({
|
|
connectionString: process.env.DATABASE_URL,
|
|
});
|
|
|
|
return {
|
|
...pg,
|
|
// Override the Pool constructor. Now, any module that calls `new Pool()`
|
|
// will receive our singleton `testPool` instance.
|
|
// We use `mockImplementation` to return the instance, which is the correct
|
|
// way to mock a class constructor that should return a specific instance.
|
|
Pool: vi.fn().mockImplementation(() => {
|
|
return testPool;
|
|
}),
|
|
};
|
|
}); |