improve tests

This commit is contained in:
sbriat 2023-04-04 10:17:57 +02:00
parent 427238839a
commit dc97aa9970
6 changed files with 63 additions and 4 deletions

View File

@ -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"

View File

@ -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:

View File

@ -22,6 +22,6 @@ async function bootstrap() {
});
await app.startAllMicroservices();
await app.listen(6001);
await app.listen(process.env.HEALTH_SERVICE_PORT);
}
bootstrap();

View File

@ -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 {

View File

@ -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);
});
});

View File

@ -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);
});
});
});