Skip to content

Commit

Permalink
added function that clears database after each test
Browse files Browse the repository at this point in the history
  • Loading branch information
dzgierski19 committed Jun 15, 2024
1 parent 5b8f69c commit d3a61b2
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions test/app.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -20,6 +22,10 @@ describe('AppController (e2e)', () => {
}
});

afterEach(async () => {
await clearDatabase();
});

afterAll(async () => {
await app.close();
closeKnex();
Expand All @@ -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 () => {
Expand All @@ -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);
});
});

0 comments on commit d3a61b2

Please sign in to comment.