feat(prisma): setup prisma and route tests

This commit is contained in:
Matthieu Haineault 2025-07-15 11:46:12 -04:00
parent e22c33b29e
commit f9d77408f0
8 changed files with 101 additions and 3 deletions

View File

@ -6,7 +6,6 @@
generator client {
provider = "prisma-client-js"
output = "../generated/prisma"
}
datasource db {

View File

@ -1,10 +1,13 @@
import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { PrismaModule } from './prisma/prisma.module';
import { HealthModule } from './health/health.module';
import { HealthController } from './health/health.controller';
@Module({
imports: [],
controllers: [AppController],
imports: [PrismaModule, HealthModule],
controllers: [AppController, HealthController],
providers: [AppService],
})
export class AppModule {}

View File

@ -0,0 +1,18 @@
import { Test, TestingModule } from '@nestjs/testing';
import { HealthController } from './health.controller';
describe('HealthController', () => {
let controller: HealthController;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
controllers: [HealthController],
}).compile();
controller = module.get<HealthController>(HealthController);
});
it('should be defined', () => {
expect(controller).toBeDefined();
});
});

View File

@ -0,0 +1,13 @@
import { Controller, Get } from '@nestjs/common';
import { PrismaService } from '../prisma/prisma.service';
@Controller('health')
export class HealthController {
constructor(private readonly prisma: PrismaService) {}
@Get()
async check(): Promise<{ status: string }> {
await this.prisma.$queryRaw`SELECT 1`;
return { status: 'ok' };
}
}

View File

@ -0,0 +1,9 @@
import { Module } from '@nestjs/common';
import { HealthController } from './health.controller';
import { PrismaModule } from '../prisma/prisma.module';
@Module({
imports: [PrismaModule],
controllers: [HealthController],
})
export class HealthModule {}

View File

@ -0,0 +1,9 @@
import { Global, Module } from '@nestjs/common';
import { PrismaService } from './prisma.service';
@Global()
@Module({
providers: [PrismaService],
exports: [PrismaService],
})
export class PrismaModule {}

View File

@ -0,0 +1,18 @@
import { Injectable, OnModuleInit, OnModuleDestroy } from '@nestjs/common';
import { PrismaClient } from '@prisma/client';
//Gestion des connections à la DB
@Injectable()
export class PrismaService
extends PrismaClient
implements OnModuleInit, OnModuleDestroy
{
async onModuleInit() {
await this.$connect();
}
async onModuleDestroy() {
await this.$disconnect();
}
}

29
test/health.e2e-spec.ts Normal file
View File

@ -0,0 +1,29 @@
import { INestApplication } from '@nestjs/common';
import { Test, TestingModule } from '@nestjs/testing';
import * as request from 'supertest';
import { AppModule } from './../src/app.module';
describe('HealthController (e2e)', () => {
let app: INestApplication;
beforeAll(async () => {
const moduleFixture: TestingModule = await Test.createTestingModule({
imports: [AppModule],
}).compile();
app = moduleFixture.createNestApplication();
await app.init();
});
it('/health (GET) → 200 & { status: "ok" }', () => {
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
return request(app.getHttpServer())
.get('/health')
.expect(200)
.expect({ status: 'ok' });
});
afterAll(async () => {
await app.close();
});
});