add health tests; move utils to libs

This commit is contained in:
sbriat 2023-06-27 14:02:10 +02:00
parent e407e915fa
commit 6f2305ae6a
8 changed files with 102 additions and 9 deletions

View File

@ -118,8 +118,7 @@
"moduleNameMapper": {
"^@libs(.*)": "<rootDir>/libs/$1",
"^@modules(.*)": "<rootDir>/modules/$1",
"^@src(.*)": "<rootDir>$1",
"^@utils(.*)": "<rootDir>utils/$1"
"^@src(.*)": "<rootDir>$1"
},
"testEnvironment": "node"
}

View File

@ -1,3 +1,4 @@
import { RpcExceptionCode } from '@libs/exceptions/rpc-exception.codes.enum';
import { Injectable, ValidationPipe } from '@nestjs/common';
import { RpcException } from '@nestjs/microservices';
@ -6,7 +7,7 @@ export class RpcValidationPipe extends ValidationPipe {
createExceptionFactory() {
return (validationErrors = []) => {
return new RpcException({
code: 3,
code: RpcExceptionCode.INVALID_ARGUMENT,
message: this.flattenValidationErrors(validationErrors),
});
};

View File

@ -7,7 +7,7 @@ import { AggregateID } from '@libs/ddd';
import { AdAlreadyExistsException } from '../../core/ad.errors';
import { IdResponse } from '@libs/api/id.response.dto';
import { RpcExceptionCode } from '@libs/exceptions/rpc-exception.codes.enum';
import { RpcValidationPipe } from '@utils/pipes/rpc.validation-pipe';
import { RpcValidationPipe } from '@libs/utils/pipes/rpc.validation-pipe';
@UsePipes(
new RpcValidationPipe({

View File

@ -1,7 +1,6 @@
import { Controller, UsePipes } from '@nestjs/common';
import { QueryBus } from '@nestjs/cqrs';
import { GrpcMethod, RpcException } from '@nestjs/microservices';
import { RpcValidationPipe } from '@utils/pipes/rpc.validation-pipe';
import { FindAdByIdRequestDto } from './dtos/find-ad-by-id.request.dto';
import { FindAdByIdQuery } from '@modules/ad/core/queries/find-ad-by-id/find-ad-by-id.query';
import { AdResponseDto } from '../dtos/ad.response.dto';
@ -9,6 +8,7 @@ import { AdEntity } from '@modules/ad/core/ad.entity';
import { AdMapper } from '@modules/ad/ad.mapper';
import { NotFoundException } from '@libs/exceptions';
import { RpcExceptionCode } from '@libs/exceptions/rpc-exception.codes.enum';
import { RpcValidationPipe } from '@libs/utils/pipes/rpc.validation-pipe';
@UsePipes(
new RpcValidationPipe({

View File

@ -1,6 +1,10 @@
import { RepositoriesHealthIndicatorUseCase } from '@modules/health/core/usecases/repositories.health-indicator.usecase';
import { Controller, Get } from '@nestjs/common';
import { HealthCheckService, HealthCheck } from '@nestjs/terminus';
import { RepositoriesHealthIndicatorUseCase } from '../../core/usecases/repositories.health-indicator.usecase';
import {
HealthCheckService,
HealthCheck,
HealthCheckResult,
} from '@nestjs/terminus';
@Controller('health')
export class HealthHttpController {
@ -11,7 +15,7 @@ export class HealthHttpController {
@Get()
@HealthCheck()
async check() {
async check(): Promise<HealthCheckResult> {
try {
return await this.healthCheckService.check([
async () =>

View 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);
});
});

View File

@ -21,7 +21,6 @@
"@libs/*": ["src/libs/*"],
"@modules/*": ["src/modules/*"],
"@ports/*": ["src/ports/*"],
"@utils/*": ["src/utils/*"],
"@src/*": ["src/*"],
}
}