mirror of
https://github.com/IT4Change/gradido.git
synced 2025-12-13 07:45:54 +00:00
Merge pull request #1616 from gradido/no-reset-db-in-unit-tests
refactor: No Reset DB in Backend Unit Tests
This commit is contained in:
commit
1a8ef0085b
5
.github/workflows/test.yml
vendored
5
.github/workflows/test.yml
vendored
@ -509,16 +509,13 @@ jobs:
|
|||||||
##########################################################################
|
##########################################################################
|
||||||
# UNIT TESTS BACKEND #####################################################
|
# UNIT TESTS BACKEND #####################################################
|
||||||
##########################################################################
|
##########################################################################
|
||||||
- name: backend | docker-compose
|
- name: backend | docker-compose mariadb
|
||||||
run: docker-compose -f docker-compose.yml -f docker-compose.test.yml up --detach --no-deps mariadb
|
run: docker-compose -f docker-compose.yml -f docker-compose.test.yml up --detach --no-deps mariadb
|
||||||
- name: Sleep for 30 seconds
|
- name: Sleep for 30 seconds
|
||||||
run: sleep 30s
|
run: sleep 30s
|
||||||
shell: bash
|
shell: bash
|
||||||
- name: backend | docker-compose database
|
- name: backend | docker-compose database
|
||||||
run: docker-compose -f docker-compose.yml -f docker-compose.test.yml up --detach --no-deps 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
|
- name: backend Unit tests | test
|
||||||
run: cd database && yarn && yarn build && cd ../backend && yarn && yarn 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
|
# run: docker-compose -f docker-compose.yml -f docker-compose.test.yml exec -T backend yarn test
|
||||||
|
|||||||
@ -1,11 +1,10 @@
|
|||||||
/* eslint-disable @typescript-eslint/no-explicit-any */
|
/* eslint-disable @typescript-eslint/no-explicit-any */
|
||||||
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
|
/* 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 { createUserMutation, setPasswordMutation } from '@test/graphql'
|
||||||
import gql from 'graphql-tag'
|
import gql from 'graphql-tag'
|
||||||
import { GraphQLError } from 'graphql'
|
import { GraphQLError } from 'graphql'
|
||||||
import { resetDB } from '@dbTools/helpers'
|
|
||||||
import { LoginEmailOptIn } from '@entity/LoginEmailOptIn'
|
import { LoginEmailOptIn } from '@entity/LoginEmailOptIn'
|
||||||
import { User } from '@entity/User'
|
import { User } from '@entity/User'
|
||||||
import CONFIG from '@/config'
|
import CONFIG from '@/config'
|
||||||
@ -30,29 +29,18 @@ 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
|
let mutate: any, query: any, con: any
|
||||||
|
|
||||||
beforeAll(async () => {
|
beforeAll(async () => {
|
||||||
const testEnv = await testEnvironment(context)
|
const testEnv = await testEnvironment()
|
||||||
mutate = testEnv.mutate
|
mutate = testEnv.mutate
|
||||||
query = testEnv.query
|
query = testEnv.query
|
||||||
con = testEnv.con
|
con = testEnv.con
|
||||||
|
await cleanDB()
|
||||||
})
|
})
|
||||||
|
|
||||||
afterAll(async () => {
|
afterAll(async () => {
|
||||||
await resetDB(true)
|
await cleanDB()
|
||||||
await con.close()
|
await con.close()
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -75,7 +63,7 @@ describe('UserResolver', () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
afterAll(async () => {
|
afterAll(async () => {
|
||||||
await resetEntities([User, LoginEmailOptIn])
|
await cleanDB()
|
||||||
})
|
})
|
||||||
|
|
||||||
it('returns success', () => {
|
it('returns success', () => {
|
||||||
@ -213,7 +201,7 @@ describe('UserResolver', () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
afterAll(async () => {
|
afterAll(async () => {
|
||||||
await resetEntities([User, LoginEmailOptIn])
|
await cleanDB()
|
||||||
})
|
})
|
||||||
|
|
||||||
it('sets email checked to true', () => {
|
it('sets email checked to true', () => {
|
||||||
@ -256,7 +244,7 @@ describe('UserResolver', () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
afterAll(async () => {
|
afterAll(async () => {
|
||||||
await resetEntities([User, LoginEmailOptIn])
|
await cleanDB()
|
||||||
})
|
})
|
||||||
|
|
||||||
it('throws an error', () => {
|
it('throws an error', () => {
|
||||||
@ -282,7 +270,7 @@ describe('UserResolver', () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
afterAll(async () => {
|
afterAll(async () => {
|
||||||
await resetEntities([User, LoginEmailOptIn])
|
await cleanDB()
|
||||||
})
|
})
|
||||||
|
|
||||||
it('throws an error', () => {
|
it('throws an error', () => {
|
||||||
@ -323,7 +311,7 @@ describe('UserResolver', () => {
|
|||||||
let result: User
|
let result: User
|
||||||
|
|
||||||
afterAll(async () => {
|
afterAll(async () => {
|
||||||
await resetEntities([User, LoginEmailOptIn])
|
await cleanDB()
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('no users in database', () => {
|
describe('no users in database', () => {
|
||||||
@ -353,7 +341,7 @@ describe('UserResolver', () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
afterAll(async () => {
|
afterAll(async () => {
|
||||||
await resetEntities([User, LoginEmailOptIn])
|
await cleanDB()
|
||||||
})
|
})
|
||||||
|
|
||||||
it('returns the user object', () => {
|
it('returns the user object', () => {
|
||||||
|
|||||||
@ -7,8 +7,28 @@ import { resetDB, initialize } from '@dbTools/helpers'
|
|||||||
import { createUserMutation, setPasswordMutation } from './graphql'
|
import { createUserMutation, setPasswordMutation } from './graphql'
|
||||||
import { LoginEmailOptIn } from '@entity/LoginEmailOptIn'
|
import { LoginEmailOptIn } from '@entity/LoginEmailOptIn'
|
||||||
import { User } from '@entity/User'
|
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 server = await createServer(context)
|
||||||
const con = server.con
|
const con = server.con
|
||||||
const testClient = createTestClient(server.apollo)
|
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) => {
|
export const createUser = async (mutate: any, user: any) => {
|
||||||
await mutate({ mutation: createUserMutation, variables: user })
|
await mutate({ mutation: createUserMutation, variables: user })
|
||||||
const dbUser = await User.findOne({ where: { email: user.email } })
|
const dbUser = await User.findOne({ where: { email: user.email } })
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user