diff --git a/src/tests/setup/mock-db.ts b/src/tests/setup/mock-db.ts index 0f8b6b09..f5183fe6 100644 --- a/src/tests/setup/mock-db.ts +++ b/src/tests/setup/mock-db.ts @@ -1,29 +1,25 @@ 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(); - // 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, - }); + // This is the mock pool instance that will be returned by the Pool constructor. + // It mocks the methods that our application's db service uses. + const mPool = { + connect: vi.fn(), + query: vi.fn(), + on: vi.fn(), + end: vi.fn(), + }; 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; - }), + // The original mock was an arrow function, which cannot be called with `new`. + // Using vi.fn() creates a mock constructor that returns our mock pool instance. + Pool: vi.fn(() => mPool), }; }); \ No newline at end of file diff --git a/vite.config.ts b/vite.config.ts index 7f65a318..fc722bc0 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -29,8 +29,13 @@ export default defineConfig({ environment: 'jsdom', globalSetup: './src/tests/setup/global-setup.ts', setupFiles: ['./src/vitest.setup.ts'], - // Exclude integration tests, which are handled by a separate project. - exclude: ['**/*.integration.test.ts', '**/node_modules/**'], + // Exclude integration tests and other non-test files from the unit test runner. + exclude: [ + '**/node_modules/**', + '**/dist/**', + '**/*.integration.test.ts', + '**/*.e2e.test.ts' + ], coverage: { provider: 'v8', reporter: ['text', 'html'], diff --git a/vitest.config.integration.ts b/vitest.config.integration.ts index 2e72af0f..1f515551 100644 --- a/vitest.config.integration.ts +++ b/vitest.config.integration.ts @@ -13,7 +13,7 @@ export default defineConfig({ // This setup script starts the backend server before tests run. globalSetup: './src/tests/setup/integration-global-setup.ts', testTimeout: 15000, // Increased timeout for server startup and API calls. - // "singleThread: true" is deprecated in modern Vitest. + // "singleThread: true" is removed in modern Vitest. // Use fileParallelism: false to ensure test files run one by one to prevent port conflicts. fileParallelism: false, },