unit tests fixin
Some checks failed
Deploy to Web Server flyer-crawler.projectium.com / deploy (push) Failing after 1m9s
Some checks failed
Deploy to Web Server flyer-crawler.projectium.com / deploy (push) Failing after 1m9s
This commit is contained in:
@@ -3,13 +3,14 @@ import * as db from './db';
|
||||
import bcrypt from 'bcrypt';
|
||||
import { testPool } from '../tests/setup/test-db';
|
||||
|
||||
// Mock the source of the database connection. This tells Vitest that whenever
|
||||
// any module tries to import from './db/connection', it should instead get
|
||||
// the exports from our mock object defined here. This is the key to ensuring
|
||||
// all db service functions use the test database pool.
|
||||
vi.mock('./db/connection', async (importOriginal) => {
|
||||
const actual = await importOriginal<typeof import('./db/connection')>();
|
||||
return { ...actual, pool: testPool };
|
||||
// Mock the 'pg' package. Whenever 'new Pool()' is called anywhere in the code
|
||||
// under test (specifically in `src/services/db/connection.ts`), it will be
|
||||
// intercepted. Instead of creating a real production pool, it will return our
|
||||
// `testPool`. This is the most reliable way to ensure all database interactions
|
||||
// in tests use the test database.
|
||||
vi.mock('pg', async (importOriginal) => {
|
||||
const pg = await importOriginal<typeof import('pg')>();
|
||||
return { ...pg, Pool: vi.fn(() => testPool) };
|
||||
});
|
||||
|
||||
describe('Database Service Integration Tests', () => {
|
||||
|
||||
@@ -1,15 +0,0 @@
|
||||
// src/services/db/__mocks__/index.ts
|
||||
import { vi } from 'vitest';
|
||||
import { testPool } from '../../../tests/setup/test-db';
|
||||
|
||||
// This is the magic. We import the real module...
|
||||
const actualDbModule = await vi.importActual<typeof import('..')>('..');
|
||||
|
||||
// ...then we replace its exported `pool` with our dedicated test pool.
|
||||
actualDbModule.pool = testPool;
|
||||
|
||||
// Now, we re-export the entire module. Any test that imports from `src/services/db`
|
||||
// will now get the real functions, but those functions will be operating on the
|
||||
// test database pool instead of the production one.
|
||||
export const { pool, ...rest } = actualDbModule;
|
||||
export default { pool, ...rest };
|
||||
@@ -3,13 +3,14 @@ import * as db from './db';
|
||||
import { testPool } from '../tests/setup/test-db';
|
||||
import bcrypt from 'bcrypt';
|
||||
|
||||
// Mock the source of the database connection. This tells Vitest that whenever
|
||||
// any module tries to import from './db/connection', it should instead get
|
||||
// the exports from our mock object defined here. This ensures all db service
|
||||
// functions in this test file use the test database pool.
|
||||
vi.mock('./db/connection', async (importOriginal) => {
|
||||
const actual = await importOriginal<typeof import('./db/connection')>();
|
||||
return { ...actual, pool: testPool };
|
||||
// Mock the 'pg' package. Whenever 'new Pool()' is called anywhere in the code
|
||||
// under test (specifically in `src/services/db/connection.ts`), it will be
|
||||
// intercepted. Instead of creating a real production pool, it will return our
|
||||
// `testPool`. This ensures all database interactions in this test file use the
|
||||
// test database, resolving the circular dependency error.
|
||||
vi.mock('pg', async (importOriginal) => {
|
||||
const pg = await importOriginal<typeof import('pg')>();
|
||||
return { ...pg, Pool: vi.fn(() => testPool) };
|
||||
});
|
||||
|
||||
describe('Shopping List DB Service Tests', () => {
|
||||
|
||||
Reference in New Issue
Block a user