add health tests; move utils to libs
This commit is contained in:
parent
e407e915fa
commit
6f2305ae6a
|
@ -118,8 +118,7 @@
|
||||||
"moduleNameMapper": {
|
"moduleNameMapper": {
|
||||||
"^@libs(.*)": "<rootDir>/libs/$1",
|
"^@libs(.*)": "<rootDir>/libs/$1",
|
||||||
"^@modules(.*)": "<rootDir>/modules/$1",
|
"^@modules(.*)": "<rootDir>/modules/$1",
|
||||||
"^@src(.*)": "<rootDir>$1",
|
"^@src(.*)": "<rootDir>$1"
|
||||||
"^@utils(.*)": "<rootDir>utils/$1"
|
|
||||||
},
|
},
|
||||||
"testEnvironment": "node"
|
"testEnvironment": "node"
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
import { RpcExceptionCode } from '@libs/exceptions/rpc-exception.codes.enum';
|
||||||
import { Injectable, ValidationPipe } from '@nestjs/common';
|
import { Injectable, ValidationPipe } from '@nestjs/common';
|
||||||
import { RpcException } from '@nestjs/microservices';
|
import { RpcException } from '@nestjs/microservices';
|
||||||
|
|
||||||
|
@ -6,7 +7,7 @@ export class RpcValidationPipe extends ValidationPipe {
|
||||||
createExceptionFactory() {
|
createExceptionFactory() {
|
||||||
return (validationErrors = []) => {
|
return (validationErrors = []) => {
|
||||||
return new RpcException({
|
return new RpcException({
|
||||||
code: 3,
|
code: RpcExceptionCode.INVALID_ARGUMENT,
|
||||||
message: this.flattenValidationErrors(validationErrors),
|
message: this.flattenValidationErrors(validationErrors),
|
||||||
});
|
});
|
||||||
};
|
};
|
|
@ -7,7 +7,7 @@ import { AggregateID } from '@libs/ddd';
|
||||||
import { AdAlreadyExistsException } from '../../core/ad.errors';
|
import { AdAlreadyExistsException } from '../../core/ad.errors';
|
||||||
import { IdResponse } from '@libs/api/id.response.dto';
|
import { IdResponse } from '@libs/api/id.response.dto';
|
||||||
import { RpcExceptionCode } from '@libs/exceptions/rpc-exception.codes.enum';
|
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(
|
@UsePipes(
|
||||||
new RpcValidationPipe({
|
new RpcValidationPipe({
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
import { Controller, UsePipes } from '@nestjs/common';
|
import { Controller, UsePipes } from '@nestjs/common';
|
||||||
import { QueryBus } from '@nestjs/cqrs';
|
import { QueryBus } from '@nestjs/cqrs';
|
||||||
import { GrpcMethod, RpcException } from '@nestjs/microservices';
|
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 { 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 { FindAdByIdQuery } from '@modules/ad/core/queries/find-ad-by-id/find-ad-by-id.query';
|
||||||
import { AdResponseDto } from '../dtos/ad.response.dto';
|
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 { AdMapper } from '@modules/ad/ad.mapper';
|
||||||
import { NotFoundException } from '@libs/exceptions';
|
import { NotFoundException } from '@libs/exceptions';
|
||||||
import { RpcExceptionCode } from '@libs/exceptions/rpc-exception.codes.enum';
|
import { RpcExceptionCode } from '@libs/exceptions/rpc-exception.codes.enum';
|
||||||
|
import { RpcValidationPipe } from '@libs/utils/pipes/rpc.validation-pipe';
|
||||||
|
|
||||||
@UsePipes(
|
@UsePipes(
|
||||||
new RpcValidationPipe({
|
new RpcValidationPipe({
|
||||||
|
|
|
@ -1,6 +1,10 @@
|
||||||
|
import { RepositoriesHealthIndicatorUseCase } from '@modules/health/core/usecases/repositories.health-indicator.usecase';
|
||||||
import { Controller, Get } from '@nestjs/common';
|
import { Controller, Get } from '@nestjs/common';
|
||||||
import { HealthCheckService, HealthCheck } from '@nestjs/terminus';
|
import {
|
||||||
import { RepositoriesHealthIndicatorUseCase } from '../../core/usecases/repositories.health-indicator.usecase';
|
HealthCheckService,
|
||||||
|
HealthCheck,
|
||||||
|
HealthCheckResult,
|
||||||
|
} from '@nestjs/terminus';
|
||||||
|
|
||||||
@Controller('health')
|
@Controller('health')
|
||||||
export class HealthHttpController {
|
export class HealthHttpController {
|
||||||
|
@ -11,7 +15,7 @@ export class HealthHttpController {
|
||||||
|
|
||||||
@Get()
|
@Get()
|
||||||
@HealthCheck()
|
@HealthCheck()
|
||||||
async check() {
|
async check(): Promise<HealthCheckResult> {
|
||||||
try {
|
try {
|
||||||
return await this.healthCheckService.check([
|
return await this.healthCheckService.check([
|
||||||
async () =>
|
async () =>
|
||||||
|
|
|
@ -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);
|
||||||
|
});
|
||||||
|
});
|
|
@ -21,7 +21,6 @@
|
||||||
"@libs/*": ["src/libs/*"],
|
"@libs/*": ["src/libs/*"],
|
||||||
"@modules/*": ["src/modules/*"],
|
"@modules/*": ["src/modules/*"],
|
||||||
"@ports/*": ["src/ports/*"],
|
"@ports/*": ["src/ports/*"],
|
||||||
"@utils/*": ["src/utils/*"],
|
|
||||||
"@src/*": ["src/*"],
|
"@src/*": ["src/*"],
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue