From 85ca10f000dc85da45fa97f7823e439cfbf1828e Mon Sep 17 00:00:00 2001 From: Moriz Wahl Date: Mon, 14 Mar 2022 10:10:57 +0100 Subject: [PATCH 1/8] refactor: No Reset DB in Backend Unit Tests --- .../src/graphql/resolver/UserResolver.test.ts | 34 ++++++------------- backend/test/helpers.ts | 28 +++++++++++---- 2 files changed, 32 insertions(+), 30 deletions(-) diff --git a/backend/src/graphql/resolver/UserResolver.test.ts b/backend/src/graphql/resolver/UserResolver.test.ts index 05ff2b302..ae4f39446 100644 --- a/backend/src/graphql/resolver/UserResolver.test.ts +++ b/backend/src/graphql/resolver/UserResolver.test.ts @@ -1,11 +1,10 @@ /* eslint-disable @typescript-eslint/no-explicit-any */ /* eslint-disable @typescript-eslint/explicit-module-boundary-types */ -import { testEnvironment, resetEntities, createUser } from '@test/helpers' +import { testEnvironment, createUser, headerPushMock, cleanDB } from '@test/helpers' import { createUserMutation, setPasswordMutation } from '@test/graphql' import gql from 'graphql-tag' import { GraphQLError } from 'graphql' -import { resetDB } from '@dbTools/helpers' import { LoginEmailOptIn } from '@entity/LoginEmailOptIn' import { User } from '@entity/User' import CONFIG from '@/config' @@ -30,30 +29,19 @@ jest.mock('@/apis/KlicktippController', () => { }) */ -let token: string - -// eslint-disable-next-line @typescript-eslint/no-unused-vars -const headerPushMock = jest.fn((t) => (token = t.value)) - -const context = { - setHeaders: { - push: headerPushMock, - forEach: jest.fn(), - }, -} - let mutate: any, query: any, con: any beforeAll(async () => { - const testEnv = await testEnvironment(context) + const testEnv = await testEnvironment() mutate = testEnv.mutate query = testEnv.query con = testEnv.con + await cleanDB() }) afterAll(async () => { - await resetDB(true) - await con.close() + await cleanDB() + con.close() }) describe('UserResolver', () => { @@ -75,7 +63,7 @@ describe('UserResolver', () => { }) afterAll(async () => { - await resetEntities([User, LoginEmailOptIn]) + await cleanDB() }) it('returns success', () => { @@ -213,7 +201,7 @@ describe('UserResolver', () => { }) afterAll(async () => { - await resetEntities([User, LoginEmailOptIn]) + await cleanDB() }) it('sets email checked to true', () => { @@ -256,7 +244,7 @@ describe('UserResolver', () => { }) afterAll(async () => { - await resetEntities([User, LoginEmailOptIn]) + await cleanDB() }) it('throws an error', () => { @@ -282,7 +270,7 @@ describe('UserResolver', () => { }) afterAll(async () => { - await resetEntities([User, LoginEmailOptIn]) + await cleanDB() }) it('throws an error', () => { @@ -323,7 +311,7 @@ describe('UserResolver', () => { let result: User afterAll(async () => { - await resetEntities([User, LoginEmailOptIn]) + await cleanDB() }) describe('no users in database', () => { @@ -353,7 +341,7 @@ describe('UserResolver', () => { }) afterAll(async () => { - await resetEntities([User, LoginEmailOptIn]) + await cleanDB() }) it('returns the user object', () => { diff --git a/backend/test/helpers.ts b/backend/test/helpers.ts index f3588cd43..1048b16b7 100644 --- a/backend/test/helpers.ts +++ b/backend/test/helpers.ts @@ -7,8 +7,28 @@ import { resetDB, initialize } from '@dbTools/helpers' import { createUserMutation, setPasswordMutation } from './graphql' import { LoginEmailOptIn } from '@entity/LoginEmailOptIn' import { User } from '@entity/User' +import { entities } from '@entity/index' -export const testEnvironment = async (context: any) => { +let token = '' + +export const headerPushMock = jest.fn((t) => (token = t.value)) + +const context = { + token, + setHeaders: { + push: headerPushMock, + forEach: jest.fn(), + }, +} + +export const cleanDB = async () => { + // this only works as lond we do not have foreign key constraints + for (let i = 0; i < entities.length; i++) { + await resetEntity(entities[i]) + } +} + +export const testEnvironment = async () => { const server = await createServer(context) const con = server.con const testClient = createTestClient(server.apollo) @@ -27,12 +47,6 @@ export const resetEntity = async (entity: any) => { } } -export const resetEntities = async (entities: any[]) => { - for (let i = 0; i < entities.length; i++) { - await resetEntity(entities[i]) - } -} - export const createUser = async (mutate: any, user: any) => { await mutate({ mutation: createUserMutation, variables: user }) const dbUser = await User.findOne({ where: { email: user.email } }) From dedf174f4faa5080690531867ff9d615b14cf53a Mon Sep 17 00:00:00 2001 From: Moriz Wahl Date: Mon, 14 Mar 2022 10:12:56 +0100 Subject: [PATCH 2/8] add dependency database_up to backend unit tests --- .github/workflows/test.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index c9fda7b2a..5a5b90979 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -489,7 +489,7 @@ jobs: unit_test_backend: name: Unit tests - Backend runs-on: ubuntu-latest - needs: [build_test_mariadb] + needs: [build_test_mariadb, build_test_database_up] steps: ########################################################################## # CHECKOUT CODE ########################################################## From ec1322c5ed12ec1d0177538bd277bf7ce38f505c Mon Sep 17 00:00:00 2001 From: Moriz Wahl Date: Mon, 14 Mar 2022 10:15:01 +0100 Subject: [PATCH 3/8] try backend unit tests in CI without sleeps --- .github/workflows/test.yml | 8 -------- 1 file changed, 8 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 5a5b90979..ecfb10fda 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -511,14 +511,6 @@ jobs: ########################################################################## - name: backend | docker-compose run: docker-compose -f docker-compose.yml -f docker-compose.test.yml up --detach --no-deps mariadb - - name: Sleep for 30 seconds - run: sleep 30s - shell: bash - - name: backend | docker-compose database - run: docker-compose -f docker-compose.yml -f docker-compose.test.yml up --detach --no-deps database - - name: Sleep for 30 seconds - run: sleep 30s - shell: bash - name: backend Unit tests | test run: cd database && yarn && yarn build && cd ../backend && yarn && yarn test # run: docker-compose -f docker-compose.yml -f docker-compose.test.yml exec -T backend yarn test From 4db0235738137a09d1248263f04ef8f07307e286 Mon Sep 17 00:00:00 2001 From: Moriz Wahl Date: Mon, 14 Mar 2022 10:19:19 +0100 Subject: [PATCH 4/8] await for the connection to close --- backend/src/graphql/resolver/UserResolver.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backend/src/graphql/resolver/UserResolver.test.ts b/backend/src/graphql/resolver/UserResolver.test.ts index ae4f39446..fd0936b9a 100644 --- a/backend/src/graphql/resolver/UserResolver.test.ts +++ b/backend/src/graphql/resolver/UserResolver.test.ts @@ -41,7 +41,7 @@ beforeAll(async () => { afterAll(async () => { await cleanDB() - con.close() + await con.close() }) describe('UserResolver', () => { From a503815829fe1cd30330891e7fcb1c8e1ec3e513 Mon Sep 17 00:00:00 2001 From: Moriz Wahl Date: Mon, 14 Mar 2022 10:19:36 +0100 Subject: [PATCH 5/8] wait again --- .github/workflows/test.yml | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index ecfb10fda..5a5b90979 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -511,6 +511,14 @@ jobs: ########################################################################## - name: backend | docker-compose run: docker-compose -f docker-compose.yml -f docker-compose.test.yml up --detach --no-deps mariadb + - name: Sleep for 30 seconds + run: sleep 30s + shell: bash + - name: backend | docker-compose database + run: docker-compose -f docker-compose.yml -f docker-compose.test.yml up --detach --no-deps database + - name: Sleep for 30 seconds + run: sleep 30s + shell: bash - name: backend Unit tests | test run: cd database && yarn && yarn build && cd ../backend && yarn && yarn test # run: docker-compose -f docker-compose.yml -f docker-compose.test.yml exec -T backend yarn test From d0df7002f903792ca581746ffcd135d1443cd278 Mon Sep 17 00:00:00 2001 From: Moriz Wahl Date: Mon, 14 Mar 2022 10:25:12 +0100 Subject: [PATCH 6/8] add dependency database_migration_test to backend unit tests, do not wait anymore --- .github/workflows/test.yml | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 5a5b90979..02e0bd2c7 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -489,7 +489,7 @@ jobs: unit_test_backend: name: Unit tests - Backend runs-on: ubuntu-latest - needs: [build_test_mariadb, build_test_database_up] + needs: [build_test_mariadb, database_migration_test, build_test_database_up] steps: ########################################################################## # CHECKOUT CODE ########################################################## @@ -510,15 +510,7 @@ jobs: # UNIT TESTS BACKEND ##################################################### ########################################################################## - name: backend | docker-compose - run: docker-compose -f docker-compose.yml -f docker-compose.test.yml up --detach --no-deps mariadb - - name: Sleep for 30 seconds - run: sleep 30s - shell: bash - - name: backend | docker-compose database - run: docker-compose -f docker-compose.yml -f docker-compose.test.yml up --detach --no-deps database - - name: Sleep for 30 seconds - run: sleep 30s - shell: bash + run: docker-compose -f docker-compose.yml -f docker-compose.test.yml up --detach --no-deps mariadb database - name: backend Unit tests | test run: cd database && yarn && yarn build && cd ../backend && yarn && yarn test # run: docker-compose -f docker-compose.yml -f docker-compose.test.yml exec -T backend yarn test From 0aa9e290d96313a630bc6e4e4ce3990e1f831d59 Mon Sep 17 00:00:00 2001 From: Moriz Wahl Date: Mon, 14 Mar 2022 13:52:52 +0100 Subject: [PATCH 7/8] wait for mariadb to start --- .github/workflows/test.yml | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 402b1a6ef..3778dae93 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -509,8 +509,13 @@ jobs: ########################################################################## # UNIT TESTS BACKEND ##################################################### ########################################################################## - - name: backend | docker-compose - run: docker-compose -f docker-compose.yml -f docker-compose.test.yml up --detach --no-deps mariadb database + - name: backend | docker-compose mariadb + run: docker-compose -f docker-compose.yml -f docker-compose.test.yml up --detach --no-deps mariadb + - name: Sleep for 30 seconds + run: sleep 30s + shell: bash + - name: backend | docker-compose database + run: docker-compose -f docker-compose.yml -f docker-compose.test.yml up --detach --no-deps database - name: backend Unit tests | test run: cd database && yarn && yarn build && cd ../backend && yarn && yarn test # run: docker-compose -f docker-compose.yml -f docker-compose.test.yml exec -T backend yarn test From 87bd89ec74d0c80b3d2dc2369472e38b1f4d54a6 Mon Sep 17 00:00:00 2001 From: Moriz Wahl Date: Mon, 14 Mar 2022 14:58:37 +0100 Subject: [PATCH 8/8] Update .github/workflows/test.yml Co-authored-by: Ulf Gebhardt --- .github/workflows/test.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 3778dae93..0739729b5 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -489,7 +489,7 @@ jobs: unit_test_backend: name: Unit tests - Backend runs-on: ubuntu-latest - needs: [build_test_mariadb, database_migration_test, build_test_database_up] + needs: [build_test_mariadb] steps: ########################################################################## # CHECKOUT CODE ##########################################################