refactor: No Reset DB in Backend Unit Tests

This commit is contained in:
Moriz Wahl 2022-03-14 10:10:57 +01:00
parent 93e70c0aac
commit 85ca10f000
2 changed files with 32 additions and 30 deletions

View File

@ -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', () => {

View File

@ -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 } })