From dc97aa997085b2f92211ba553f69e5723cde220e Mon Sep 17 00:00:00 2001 From: sbriat Date: Tue, 4 Apr 2023 10:17:57 +0200 Subject: [PATCH] improve tests --- .env.dist | 1 + docker-compose.yml | 2 +- src/main.ts | 2 +- .../prisma.health-indicator.usecase.ts | 2 +- .../health/tests/unit/messager.spec.ts | 2 +- .../prisma.health-indicator.usecase.spec.ts | 58 +++++++++++++++++++ 6 files changed, 63 insertions(+), 4 deletions(-) create mode 100644 src/modules/health/tests/unit/prisma.health-indicator.usecase.spec.ts diff --git a/.env.dist b/.env.dist index a5eed6c..8c1344f 100644 --- a/.env.dist +++ b/.env.dist @@ -2,6 +2,7 @@ SERVICE_URL=0.0.0.0 SERVICE_PORT=5001 SERVICE_CONFIGURATION_DOMAIN=USER +HEALTH_SERVICE_PORT=6001 # PRISMA DATABASE_URL="postgresql://mobicoop:mobicoop@v3-db:5432/mobicoop?schema=user" diff --git a/docker-compose.yml b/docker-compose.yml index 9465d3c..719b6ee 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -14,7 +14,7 @@ services: command: npm run start:dev ports: - ${SERVICE_PORT:-5001}:${SERVICE_PORT:-5001} - - 6001:6001 + - ${HEALTH_SERVICE_PORT:-6001}:${HEALTH_SERVICE_PORT:-6001} networks: v3-network: aliases: diff --git a/src/main.ts b/src/main.ts index f29d579..5de5048 100644 --- a/src/main.ts +++ b/src/main.ts @@ -22,6 +22,6 @@ async function bootstrap() { }); await app.startAllMicroservices(); - await app.listen(6001); + await app.listen(process.env.HEALTH_SERVICE_PORT); } bootstrap(); diff --git a/src/modules/health/domain/usecases/prisma.health-indicator.usecase.ts b/src/modules/health/domain/usecases/prisma.health-indicator.usecase.ts index 912af2b..464e2c0 100644 --- a/src/modules/health/domain/usecases/prisma.health-indicator.usecase.ts +++ b/src/modules/health/domain/usecases/prisma.health-indicator.usecase.ts @@ -4,7 +4,7 @@ import { HealthIndicator, HealthIndicatorResult, } from '@nestjs/terminus'; -import { UsersRepository } from 'src/modules/user/adapters/secondaries/users.repository'; +import { UsersRepository } from '../../../user/adapters/secondaries/users.repository'; @Injectable() export class PrismaHealthIndicatorUseCase extends HealthIndicator { diff --git a/src/modules/health/tests/unit/messager.spec.ts b/src/modules/health/tests/unit/messager.spec.ts index 4a3c396..0331332 100644 --- a/src/modules/health/tests/unit/messager.spec.ts +++ b/src/modules/health/tests/unit/messager.spec.ts @@ -41,7 +41,7 @@ describe('Messager', () => { it('should publish a message', async () => { jest.spyOn(mockAmqpConnection, 'publish'); - messager.publish('user.create.info', 'my-test'); + messager.publish('test.create.info', 'my-test'); expect(mockAmqpConnection.publish).toHaveBeenCalledTimes(1); }); }); diff --git a/src/modules/health/tests/unit/prisma.health-indicator.usecase.spec.ts b/src/modules/health/tests/unit/prisma.health-indicator.usecase.spec.ts new file mode 100644 index 0000000..e62feb3 --- /dev/null +++ b/src/modules/health/tests/unit/prisma.health-indicator.usecase.spec.ts @@ -0,0 +1,58 @@ +import { Test, TestingModule } from '@nestjs/testing'; +import { PrismaHealthIndicatorUseCase } from '../../domain/usecases/prisma.health-indicator.usecase'; +import { UsersRepository } from '../../../user/adapters/secondaries/users.repository'; +import { PrismaClientKnownRequestError } from '@prisma/client/runtime'; +import { HealthCheckError, HealthIndicatorResult } from '@nestjs/terminus'; + +const mockUsersRepository = { + healthCheck: jest + .fn() + .mockImplementationOnce(() => { + return Promise.resolve(true); + }) + .mockImplementation(() => { + throw new PrismaClientKnownRequestError('Service unavailable', { + code: 'code', + clientVersion: 'version', + }); + }), +}; + +describe('PrismaHealthIndicatorUseCase', () => { + let prismaHealthIndicatorUseCase: PrismaHealthIndicatorUseCase; + + beforeAll(async () => { + const module: TestingModule = await Test.createTestingModule({ + providers: [ + { + provide: UsersRepository, + useValue: mockUsersRepository, + }, + PrismaHealthIndicatorUseCase, + ], + }).compile(); + + prismaHealthIndicatorUseCase = module.get( + PrismaHealthIndicatorUseCase, + ); + }); + + it('should be defined', () => { + expect(prismaHealthIndicatorUseCase).toBeDefined(); + }); + + describe('execute', () => { + it('should check health successfully', async () => { + const healthIndicatorResult: HealthIndicatorResult = + await prismaHealthIndicatorUseCase.isHealthy('prisma'); + + expect(healthIndicatorResult['prisma'].status).toBe('up'); + }); + + it('should throw an error if database is unavailable', async () => { + await expect( + prismaHealthIndicatorUseCase.isHealthy('prisma'), + ).rejects.toBeInstanceOf(HealthCheckError); + }); + }); +});