test(auth): add unit tests for various guards and services with mocked dependencies
This commit is contained in:
67
backend/src/database/database.service.spec.ts
Normal file
67
backend/src/database/database.service.spec.ts
Normal file
@@ -0,0 +1,67 @@
|
||||
import { ConfigService } from "@nestjs/config";
|
||||
import { Test, TestingModule } from "@nestjs/testing";
|
||||
import { DatabaseService } from "./database.service";
|
||||
|
||||
jest.mock("pg", () => {
|
||||
const mPool = {
|
||||
connect: jest.fn(),
|
||||
query: jest.fn(),
|
||||
end: jest.fn(),
|
||||
on: jest.fn(),
|
||||
};
|
||||
return { Pool: jest.fn(() => mPool) };
|
||||
});
|
||||
|
||||
jest.mock("drizzle-orm/node-postgres", () => ({
|
||||
drizzle: jest.fn().mockReturnValue({}),
|
||||
}));
|
||||
|
||||
describe("DatabaseService", () => {
|
||||
let service: DatabaseService;
|
||||
let _configService: ConfigService;
|
||||
|
||||
const mockConfigService = {
|
||||
get: jest.fn((key) => {
|
||||
const config = {
|
||||
POSTGRES_PASSWORD: "p",
|
||||
POSTGRES_USER: "u",
|
||||
POSTGRES_HOST: "h",
|
||||
POSTGRES_PORT: "5432",
|
||||
POSTGRES_DB: "db",
|
||||
NODE_ENV: "development",
|
||||
};
|
||||
return config[key];
|
||||
}),
|
||||
};
|
||||
|
||||
beforeEach(async () => {
|
||||
const module: TestingModule = await Test.createTestingModule({
|
||||
providers: [
|
||||
DatabaseService,
|
||||
{ provide: ConfigService, useValue: mockConfigService },
|
||||
],
|
||||
}).compile();
|
||||
|
||||
service = module.get<DatabaseService>(DatabaseService);
|
||||
_configService = module.get<ConfigService>(ConfigService);
|
||||
});
|
||||
|
||||
it("should be defined", () => {
|
||||
expect(service).toBeDefined();
|
||||
});
|
||||
|
||||
describe("onModuleInit", () => {
|
||||
it("should skip migrations in development", async () => {
|
||||
await service.onModuleInit();
|
||||
expect(mockConfigService.get).toHaveBeenCalledWith("NODE_ENV");
|
||||
});
|
||||
});
|
||||
|
||||
describe("onModuleDestroy", () => {
|
||||
it("should close pool", async () => {
|
||||
const pool = (service as any).pool;
|
||||
await service.onModuleDestroy();
|
||||
expect(pool.end).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user