From d3a61b2fae79793f4ac13ebe794f1a11bf8f43fe Mon Sep 17 00:00:00 2001 From: dzgierski19 Date: Sat, 15 Jun 2024 16:54:28 +0200 Subject: [PATCH] added function that clears database after each test --- test/app.e2e-spec.ts | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/test/app.e2e-spec.ts b/test/app.e2e-spec.ts index 47dac5a..4c2a26e 100644 --- a/test/app.e2e-spec.ts +++ b/test/app.e2e-spec.ts @@ -4,7 +4,9 @@ import * as request from 'supertest'; import { AppModule } from './../src/app.module'; import { ANIMALTYPE } from './../db/types'; import { Knex } from 'knex'; -import { closeKnex } from './../db/database'; +import { clearDatabase, closeKnex } from './../db/database'; +import { AnimalsDatabaseAdapter } from './../src/animals/animals.db.adapter'; +import { AnimalsModule } from './../src/animals/animals.module'; describe('AppController (e2e)', () => { let app: INestApplication; @@ -20,6 +22,10 @@ describe('AppController (e2e)', () => { } }); + afterEach(async () => { + await clearDatabase(); + }); + afterAll(async () => { await app.close(); closeKnex(); @@ -35,7 +41,7 @@ describe('AppController (e2e)', () => { it('/POST animal', async () => { return await request(app.getHttpServer()) .post('/animals') - .send({ name: 'FAKE_ANIMAL', type: ANIMALTYPE.BIRD }) + .send([{ name: 'FAKE_ANIMAL', type: ANIMALTYPE.BIRD }]) .expect(HttpStatus.CREATED); }); it('/UPDATE animal', async () => { @@ -48,10 +54,12 @@ describe('AppController (e2e)', () => { }); it('/DELETE animal', async () => { - const response = await request(app.getHttpServer()).get('/animals'); - const id = response.body.results.at(-1).id; + const animalDbAdapter = app.get('IDatabaseAdapter'); + await animalDbAdapter.addOne({ name: 'FAKE_NAME', type: 'bird' }); + const animals = await animalDbAdapter.getAll(); + console.log(animals); return await request(app.getHttpServer()) - .delete(`/animals/${id}`) + .delete(`/animals/${animals[0].id}`) .expect(204); }); });