unit tests fixin
Some checks failed
Deploy to Web Server flyer-crawler.projectium.com / deploy (push) Failing after 36s
Some checks failed
Deploy to Web Server flyer-crawler.projectium.com / deploy (push) Failing after 36s
This commit is contained in:
@@ -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<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,
|
||||
});
|
||||
// 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),
|
||||
};
|
||||
});
|
||||
@@ -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'],
|
||||
|
||||
@@ -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,
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user