import { Test, TestingModule } from '@nestjs/testing'; import { PrismaHealthIndicatorUseCase } from '../../domain/usecases/prisma.health-indicator.usecase'; import { HealthCheckError, HealthIndicatorResult } from '@nestjs/terminus'; import { AdRepository } from '../../../ad/adapters/secondaries/ad.repository'; import { PrismaClientKnownRequestError } from '@prisma/client/runtime/library'; const mockAdRepository = { 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: AdRepository, useValue: mockAdRepository, }, 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); }); }); });