From af5010bb5126ad61e764f43c051fa9e7eafa3a1a Mon Sep 17 00:00:00 2001 From: MatthieuTD <39524319+MatthieuTD@users.noreply.github.com> Date: Mon, 22 Sep 2025 10:38:08 +0200 Subject: [PATCH] Use spies instead of mock properties in e2e tests --- test/app.e2e-spec.ts | 64 +++++++++++++++++++++++++++----------------- 1 file changed, 39 insertions(+), 25 deletions(-) diff --git a/test/app.e2e-spec.ts b/test/app.e2e-spec.ts index f133d6f..550df86 100644 --- a/test/app.e2e-spec.ts +++ b/test/app.e2e-spec.ts @@ -175,6 +175,21 @@ class InMemoryPrismaService { return fn(this); } + reset() { + this.sites = []; + this.typeComposants = []; + this.typePieces = []; + this.typeMachines = []; + this.typeMachineComponentRequirements = []; + this.typeMachinePieceRequirements = []; + this.machines = []; + this.composants = []; + this.pieces = []; + this.customFields = []; + this.customFieldValues = []; + this.profiles = []; + } + site = { create: async ({ data }: any) => { const now = new Date(); @@ -898,6 +913,7 @@ class InMemoryPrismaService { describe('Inventory flow (e2e)', () => { let app: INestApplication; let prismaStub: InMemoryPrismaService; + let prisma: InMemoryPrismaService; beforeAll(async () => { prismaStub = new InMemoryPrismaService(); @@ -913,25 +929,19 @@ describe('Inventory flow (e2e)', () => { app = moduleFixture.createNestApplication(); await app.init(); - prisma = app.get(PrismaService); + prisma = prismaStub; }); afterAll(async () => { await app.close(); }); - beforeEach(async () => { - await prisma.$executeRawUnsafe( - 'TRUNCATE TABLE custom_field_values, documents, pieces, composants, machines, composant_models, piece_models, type_machine_component_requirements, type_machine_piece_requirements, custom_fields, type_machines, type_composants, type_pieces, constructeurs, sites RESTART IDENTITY CASCADE', - ); + beforeEach(() => { + prisma.reset(); }); - afterEach(async () => { - await app.close(); - }); - - afterAll(async () => { - await app.close(); + afterEach(() => { + jest.restoreAllMocks(); }); it('should create a type, create a machine with new component/piece selections, and edit technical fields', async () => { @@ -1090,17 +1100,17 @@ describe('Inventory flow (e2e)', () => { describe('POST /composants', () => { it('accepts creation when requirement matches the machine skeleton', async () => { - prisma.machine.findUnique.mockResolvedValue({ + jest.spyOn(prisma.machine, 'findUnique').mockResolvedValue({ id: 'machine-1', typeMachine: { componentRequirements: [ { id: 'req-1', typeComposantId: 'type-comp-1' }, ], }, - }); + } as any); const created = { id: 'component-1' }; - prisma.composant.create.mockResolvedValue(created); + const createSpy = jest.spyOn(prisma.composant, 'create').mockResolvedValue(created as any); const response = await request(app.getHttpServer()) .post('/composants') @@ -1113,18 +1123,20 @@ describe('Inventory flow (e2e)', () => { .expect(201); expect(response.body).toEqual(created); - expect(prisma.composant.create).toHaveBeenCalled(); + expect(createSpy).toHaveBeenCalled(); }); it('refuses creation when requirement is not part of the machine skeleton', async () => { - prisma.machine.findUnique.mockResolvedValue({ + jest.spyOn(prisma.machine, 'findUnique').mockResolvedValue({ id: 'machine-1', typeMachine: { componentRequirements: [ { id: 'req-1', typeComposantId: 'type-comp-1' }, ], }, - }); + } as any); + + const createSpy = jest.spyOn(prisma.composant, 'create'); await request(app.getHttpServer()) .post('/composants') @@ -1136,23 +1148,23 @@ describe('Inventory flow (e2e)', () => { }) .expect(400); - expect(prisma.composant.create).not.toHaveBeenCalled(); + expect(createSpy).not.toHaveBeenCalled(); }); }); describe('POST /pieces', () => { it('accepts creation when requirement matches the machine skeleton', async () => { - prisma.machine.findUnique.mockResolvedValue({ + jest.spyOn(prisma.machine, 'findUnique').mockResolvedValue({ id: 'machine-1', typeMachine: { pieceRequirements: [ { id: 'req-1', typePieceId: 'type-piece-1' }, ], }, - }); + } as any); const created = { id: 'piece-1' }; - prisma.piece.create.mockResolvedValue(created); + const createSpy = jest.spyOn(prisma.piece, 'create').mockResolvedValue(created as any); const response = await request(app.getHttpServer()) .post('/pieces') @@ -1165,18 +1177,20 @@ describe('Inventory flow (e2e)', () => { .expect(201); expect(response.body).toEqual(created); - expect(prisma.piece.create).toHaveBeenCalled(); + expect(createSpy).toHaveBeenCalled(); }); it('refuses creation when requirement is not part of the machine skeleton', async () => { - prisma.machine.findUnique.mockResolvedValue({ + jest.spyOn(prisma.machine, 'findUnique').mockResolvedValue({ id: 'machine-1', typeMachine: { pieceRequirements: [ { id: 'req-1', typePieceId: 'type-piece-1' }, ], }, - }); + } as any); + + const createSpy = jest.spyOn(prisma.piece, 'create'); await request(app.getHttpServer()) .post('/pieces') @@ -1188,7 +1202,7 @@ describe('Inventory flow (e2e)', () => { }) .expect(400); - expect(prisma.piece.create).not.toHaveBeenCalled(); + expect(createSpy).not.toHaveBeenCalled(); }); });