improve tests
This commit is contained in:
		
							parent
							
								
									427238839a
								
							
						
					
					
						commit
						dc97aa9970
					
				|  | @ -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" | ||||
|  |  | |||
|  | @ -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: | ||||
|  |  | |||
|  | @ -22,6 +22,6 @@ async function bootstrap() { | |||
|   }); | ||||
| 
 | ||||
|   await app.startAllMicroservices(); | ||||
|   await app.listen(6001); | ||||
|   await app.listen(process.env.HEALTH_SERVICE_PORT); | ||||
| } | ||||
| bootstrap(); | ||||
|  |  | |||
|  | @ -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 { | ||||
|  |  | |||
|  | @ -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); | ||||
|   }); | ||||
| }); | ||||
|  |  | |||
|  | @ -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>( | ||||
|       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); | ||||
|     }); | ||||
|   }); | ||||
| }); | ||||
		Loading…
	
		Reference in New Issue