import { Test, TestingModule } from '@nestjs/testing'; import { HealthCheckError, HealthIndicatorResult } from '@nestjs/terminus'; import { RepositoriesHealthIndicatorUseCase } from '../../domain/usecases/repositories.health-indicator.usecase'; import { AdRepository } from '../../../ad/adapters/secondaries/ad.repository'; const mockAdRepository = { healthCheck: jest .fn() .mockImplementationOnce(() => { return Promise.resolve(true); }) .mockImplementation(() => { throw new Error('an error occured in the repository'); }), }; describe('RepositoriesHealthIndicatorUseCase', () => { let repositoriesHealthIndicatorUseCase: RepositoriesHealthIndicatorUseCase; beforeAll(async () => { const module: TestingModule = await Test.createTestingModule({ providers: [ RepositoriesHealthIndicatorUseCase, { provide: AdRepository, useValue: mockAdRepository, }, ], }).compile(); repositoriesHealthIndicatorUseCase = module.get( RepositoriesHealthIndicatorUseCase, ); }); it('should be defined', () => { expect(repositoriesHealthIndicatorUseCase).toBeDefined(); }); describe('execute', () => { it('should check health successfully', async () => { const healthIndicatorResult: HealthIndicatorResult = await repositoriesHealthIndicatorUseCase.isHealthy('repositories'); expect(healthIndicatorResult['repositories'].status).toBe('up'); }); it('should throw an error if database is unavailable', async () => { await expect( repositoriesHealthIndicatorUseCase.isHealthy('repositories'), ).rejects.toBeInstanceOf(HealthCheckError); }); }); });