From 8694f81970680566a2fbebe37177c2c670f65fea Mon Sep 17 00:00:00 2001 From: AmauryD Date: Sun, 12 Nov 2023 22:05:32 +0100 Subject: [PATCH] teardown tests --- tests/src/integration/controllers/auth/auth.test.ts | 11 +++++++++-- .../controllers/documents/documents.test.ts | 6 ++++++ tests/src/integration/controllers/users/users.test.ts | 10 ++++++++-- tests/utils/setup-integration-test.ts | 5 +++++ 4 files changed, 28 insertions(+), 4 deletions(-) diff --git a/tests/src/integration/controllers/auth/auth.test.ts b/tests/src/integration/controllers/auth/auth.test.ts index 9f64c193..30d58edf 100644 --- a/tests/src/integration/controllers/auth/auth.test.ts +++ b/tests/src/integration/controllers/auth/auth.test.ts @@ -1,6 +1,6 @@ import 'reflect-metadata'; import { container } from '@triptyk/nfw-core'; -import { beforeAll, expect } from 'vitest'; +import { afterAll, beforeAll, expect } from 'vitest'; import { MikroORM } from '@mikro-orm/core'; import { AuthControllerTestSeeder } from './seed.js'; @@ -9,11 +9,18 @@ import { testCtx } from '../../../../utils/it-request-context.js'; import { AuthController } from '../../../../../src/features/auth/controllers/auth.controller.js'; import { DatabaseConnectionImpl } from '../../../../../src/database/connection.js'; import { RefreshTokenModel } from '../../../../../src/features/auth/models/refresh-token.model.js'; +import type { Application } from '../../../../../src/application.js'; + +let application: Application; beforeAll(async () => { - await setupIntegrationTest(AuthControllerTestSeeder); + application = await setupIntegrationTest(AuthControllerTestSeeder); }) +afterAll(async () => { + await application.stop(); +}); + testCtx('Login creates a refresh token', () => container.resolve(MikroORM), async () => { const authController = container.resolve(AuthController); diff --git a/tests/src/integration/controllers/documents/documents.test.ts b/tests/src/integration/controllers/documents/documents.test.ts index 9f3ab439..8817e6d1 100644 --- a/tests/src/integration/controllers/documents/documents.test.ts +++ b/tests/src/integration/controllers/documents/documents.test.ts @@ -9,7 +9,9 @@ import { DocumentsController } from '../../../../../src/features/users/controlle import { MimeTypes } from '../../../../../src/features/users/enums/mime-type.enum.js'; import { Roles } from '../../../../../src/features/users/enums/roles.enum.js'; import { UserModel } from '../../../../../src/features/users/models/user.model.js'; +import type { Application } from '../../../../../src/application.js'; +let application: Application; let documentsController: DocumentsController; beforeAll(async () => { @@ -17,6 +19,10 @@ beforeAll(async () => { documentsController = container.resolve(DocumentsController); }) +afterAll(async () => { + await application.stop(); +}); + function createAdminUser () { const adminUser = new UserModel(); adminUser.role = Roles.ADMIN; diff --git a/tests/src/integration/controllers/users/users.test.ts b/tests/src/integration/controllers/users/users.test.ts index 2e158503..b34c81b9 100644 --- a/tests/src/integration/controllers/users/users.test.ts +++ b/tests/src/integration/controllers/users/users.test.ts @@ -1,20 +1,26 @@ import { MikroORM } from '@mikro-orm/core'; import { container } from '@triptyk/nfw-core'; -import { beforeAll, expect } from 'vitest'; +import { afterAll, beforeAll, expect } from 'vitest'; import { UsersController } from '../../../../../src/features/users/controllers/users.controller.js'; import { Roles } from '../../../../../src/features/users/enums/roles.enum.js'; import { UserModel } from '../../../../../src/features/users/models/user.model.js'; import { testCtx } from '../../../../utils/it-request-context.js'; import { setupIntegrationTest } from '../../../../utils/setup-integration-test.js'; import { UsersControllerTestSeeder } from './seed.js'; +import type { Application } from '../../../../../src/application.js'; let usersController: UsersController; +let application: Application; beforeAll(async () => { - await setupIntegrationTest(UsersControllerTestSeeder); + application = await setupIntegrationTest(UsersControllerTestSeeder); usersController = container.resolve(UsersController); }) +afterAll(async () => { + await application.stop(); +}); + function createAdminUser () { const adminUser = new UserModel(); adminUser.role = Roles.ADMIN; diff --git a/tests/utils/setup-integration-test.ts b/tests/utils/setup-integration-test.ts index 852be108..2d27c653 100644 --- a/tests/utils/setup-integration-test.ts +++ b/tests/utils/setup-integration-test.ts @@ -10,4 +10,9 @@ export async function setupIntegrationTest (...seed: Constructor[]) { const orm = container.resolve(DatabaseConnectionImpl); await orm.connection.getSchemaGenerator().clearDatabase(); await orm.connection.getSeeder().seed(...seed); + return application; +} + +export async function teardownIntegrationTest (application: Application) { + await application.stop(); }