import { RepositoriesHealthIndicatorUseCase } from '@modules/health/core/aplication/usecases/repositories.health-indicator.usecase'; import { HealthGrpcController, ServingStatus, } from '@modules/health/interface/grpc-controllers/health.grpc.controller'; import { Test, TestingModule } from '@nestjs/testing'; const mockRepositoriesHealthIndicatorUseCase = { isHealthy: jest .fn() .mockImplementationOnce(() => ({ repositories: { status: 'up', }, })) .mockImplementationOnce(() => ({ repositories: { status: 'down', }, })), }; describe('Health Grpc Controller', () => { let healthGrpcController: HealthGrpcController; beforeAll(async () => { const module: TestingModule = await Test.createTestingModule({ providers: [ { provide: RepositoriesHealthIndicatorUseCase, useValue: mockRepositoriesHealthIndicatorUseCase, }, HealthGrpcController, ], }).compile(); healthGrpcController = module.get(HealthGrpcController); }); afterEach(async () => { jest.clearAllMocks(); }); it('should be defined', () => { expect(healthGrpcController).toBeDefined(); }); it('should return a Serving status ', async () => { jest.spyOn(mockRepositoriesHealthIndicatorUseCase, 'isHealthy'); const servingStatus: { status: ServingStatus } = await healthGrpcController.check(); expect(servingStatus).toEqual({ status: ServingStatus.SERVING, }); expect( mockRepositoriesHealthIndicatorUseCase.isHealthy, ).toHaveBeenCalledTimes(1); }); it('should return a Not Serving status ', async () => { jest.spyOn(mockRepositoriesHealthIndicatorUseCase, 'isHealthy'); const servingStatus: { status: ServingStatus } = await healthGrpcController.check(); expect(servingStatus).toEqual({ status: ServingStatus.NOT_SERVING, }); expect( mockRepositoriesHealthIndicatorUseCase.isHealthy, ).toHaveBeenCalledTimes(1); }); });