splitting unit + integration testing apart attempt #1
All checks were successful
Deploy to Web Server flyer-crawler.projectium.com / deploy (push) Successful in 3m56s
All checks were successful
Deploy to Web Server flyer-crawler.projectium.com / deploy (push) Successful in 3m56s
This commit is contained in:
34
src/tests/setup/mock-db.ts
Normal file
34
src/tests/setup/mock-db.ts
Normal file
@@ -0,0 +1,34 @@
|
||||
// src/tests/setup/mock-db.ts
|
||||
import { vi } from 'vitest';
|
||||
|
||||
/**
|
||||
* This file provides a centralized mock for the 'pg' module for all unit tests.
|
||||
* It exports the mock functions (spies) so that they can be imported into
|
||||
* individual test files for making assertions (e.g., `expect(mockQuery).toHaveBeenCalled()`).
|
||||
*/
|
||||
|
||||
// Define independent spies that can be imported by tests for assertions
|
||||
export const mockPoolQuery = vi.fn();
|
||||
export const mockClientQuery = vi.fn();
|
||||
export const mockRelease = vi.fn();
|
||||
export const mockConnect = vi.fn().mockResolvedValue({
|
||||
query: mockClientQuery,
|
||||
release: mockRelease,
|
||||
});
|
||||
|
||||
// Define the mock Pool instance that will be returned by `new Pool()`.
|
||||
export const mockPool = {
|
||||
connect: mockConnect,
|
||||
query: mockPoolQuery,
|
||||
end: vi.fn(),
|
||||
on: vi.fn(),
|
||||
totalCount: 10,
|
||||
idleCount: 5,
|
||||
waitingCount: 0,
|
||||
};
|
||||
|
||||
// Mock the 'pg' module. Using vi.fn(() => ...) ensures 'new Pool()' works as a constructor.
|
||||
vi.mock('pg', () => ({
|
||||
Pool: vi.fn(() => mockPool),
|
||||
types: { setTypeParser: vi.fn() },
|
||||
}));
|
||||
@@ -2,6 +2,7 @@
|
||||
import { vi, afterEach } from 'vitest';
|
||||
import { cleanup } from '@testing-library/react';
|
||||
import '@testing-library/jest-dom/vitest';
|
||||
import './mock-db'; // Import the centralized DB mock to ensure it's applied before all tests
|
||||
|
||||
// Mock the GeolocationPositionError global that exists in browsers but not in JSDOM.
|
||||
// This is necessary for tests that simulate geolocation failures.
|
||||
@@ -51,39 +52,6 @@ afterEach(cleanup);
|
||||
// By placing mocks here, they are guaranteed to be hoisted and applied
|
||||
// before any test files are executed, preventing initialization errors.
|
||||
|
||||
/**
|
||||
* Mocks the `pg` module. Any file that imports `pg` will get this mocked version.
|
||||
* This is the central point for mocking the database connection pool for unit tests.
|
||||
*/
|
||||
vi.mock('pg', () => {
|
||||
// Define the mock pool methods that can be controlled in tests.
|
||||
const mockQuery = vi.fn();
|
||||
const mockRelease = vi.fn();
|
||||
const mockConnect = vi.fn().mockResolvedValue({
|
||||
query: vi.fn(),
|
||||
release: mockRelease,
|
||||
});
|
||||
|
||||
// This is the mock pool instance that will be returned by `new Pool()`.
|
||||
const mockPool = {
|
||||
connect: mockConnect,
|
||||
query: mockQuery,
|
||||
end: vi.fn(),
|
||||
on: vi.fn(),
|
||||
totalCount: 10,
|
||||
idleCount: 5,
|
||||
waitingCount: 0,
|
||||
};
|
||||
|
||||
return {
|
||||
__esModule: true, // This is important for ES module interoperability.
|
||||
Pool: vi.fn(() => mockPool),
|
||||
types: {
|
||||
setTypeParser: vi.fn(),
|
||||
},
|
||||
};
|
||||
});
|
||||
|
||||
/**
|
||||
* Mocks the Google Generative AI package. This prevents real API calls during tests.
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user