mirror of
https://gitlab.com/mobicoop/v3/service/ad.git
synced 2026-03-25 03:45:51 +00:00
add health tests; move utils to libs
This commit is contained in:
90
src/modules/health/tests/unit/health.http.controller.spec.ts
Normal file
90
src/modules/health/tests/unit/health.http.controller.spec.ts
Normal file
@@ -0,0 +1,90 @@
|
||||
import { RepositoriesHealthIndicatorUseCase } from '@modules/health/core/usecases/repositories.health-indicator.usecase';
|
||||
import { HealthHttpController } from '@modules/health/interface/http-controllers/health.http.controller';
|
||||
import { HealthCheckResult, HealthCheckService } from '@nestjs/terminus';
|
||||
import { Test, TestingModule } from '@nestjs/testing';
|
||||
|
||||
const mockHealthCheckService = {
|
||||
check: jest
|
||||
.fn()
|
||||
.mockImplementationOnce(() => ({
|
||||
status: 'ok',
|
||||
info: {
|
||||
repositories: {
|
||||
status: 'up',
|
||||
},
|
||||
},
|
||||
error: {},
|
||||
details: {
|
||||
repositories: {
|
||||
status: 'up',
|
||||
},
|
||||
},
|
||||
}))
|
||||
.mockImplementationOnce(() => ({
|
||||
status: 'error',
|
||||
info: {},
|
||||
error: {
|
||||
repository:
|
||||
"\nInvalid `prisma.$queryRaw()` invocation:\n\n\nCan't reach database server at `v3-db`:`5432`\n\nPlease make sure your database server is running at `v3-db`:`5432`.",
|
||||
},
|
||||
details: {
|
||||
repository:
|
||||
"\nInvalid `prisma.$queryRaw()` invocation:\n\n\nCan't reach database server at `v3-db`:`5432`\n\nPlease make sure your database server is running at `v3-db`:`5432`.",
|
||||
},
|
||||
})),
|
||||
};
|
||||
|
||||
const mockRepositoriesHealthIndicatorUseCase = {
|
||||
isHealthy: jest.fn(),
|
||||
};
|
||||
|
||||
describe('Health Http Controller', () => {
|
||||
let healthHttpController: HealthHttpController;
|
||||
|
||||
beforeAll(async () => {
|
||||
const module: TestingModule = await Test.createTestingModule({
|
||||
providers: [
|
||||
{
|
||||
provide: HealthCheckService,
|
||||
useValue: mockHealthCheckService,
|
||||
},
|
||||
{
|
||||
provide: RepositoriesHealthIndicatorUseCase,
|
||||
useValue: mockRepositoriesHealthIndicatorUseCase,
|
||||
},
|
||||
HealthHttpController,
|
||||
],
|
||||
}).compile();
|
||||
|
||||
healthHttpController =
|
||||
module.get<HealthHttpController>(HealthHttpController);
|
||||
});
|
||||
|
||||
afterEach(async () => {
|
||||
jest.clearAllMocks();
|
||||
});
|
||||
|
||||
it('should be defined', () => {
|
||||
expect(healthHttpController).toBeDefined();
|
||||
});
|
||||
|
||||
it('should return an HealthCheckResult with Ok status ', async () => {
|
||||
jest.spyOn(mockHealthCheckService, 'check');
|
||||
jest.spyOn(mockRepositoriesHealthIndicatorUseCase, 'isHealthy');
|
||||
|
||||
const healthCheckResult: HealthCheckResult =
|
||||
await healthHttpController.check();
|
||||
expect(healthCheckResult.status).toBe('ok');
|
||||
expect(mockHealthCheckService.check).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it('should return an HealthCheckResult with Error status ', async () => {
|
||||
jest.spyOn(mockHealthCheckService, 'check');
|
||||
jest.spyOn(mockRepositoriesHealthIndicatorUseCase, 'isHealthy');
|
||||
|
||||
const healthCheckResult: HealthCheckResult =
|
||||
await healthHttpController.check();
|
||||
expect(healthCheckResult.status).toBe('error');
|
||||
expect(mockHealthCheckService.check).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user