setup testClient for seeding

This commit is contained in:
Moriz Wahl 2022-03-15 12:30:02 +01:00
parent 00c70311a1
commit e45e580627
4 changed files with 61 additions and 2 deletions

View File

@ -13,7 +13,8 @@
"start": "node build/src/index.js",
"dev": "nodemon -w src --ext ts --exec ts-node src/index.ts",
"lint": "eslint --max-warnings=0 --ext .js,.ts .",
"test": "TZ=UTC NODE_ENV=development jest --runInBand --coverage --forceExit --detectOpenHandles"
"test": "TZ=UTC NODE_ENV=development jest --runInBand --coverage --forceExit --detectOpenHandles",
"seed": "TZ=UTC ts-node src/seeds/index.ts"
},
"dependencies": {
"@types/jest": "^27.0.2",

View File

@ -1,7 +1,7 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
import { testEnvironment, headerPushMock, cleanDB, resetToken } from '@test/helpers'
import { testEnvironment, headerPushMock, resetToken, cleanDB } from '@test/helpers'
import { createUserFactory } from '@/seeds/factory/user'
import { bibiBloxberg } from '@/seeds/users/bibi-bloxberg'
import { createUser, setPassword } from '@/seeds/graphql/mutations'

View File

@ -0,0 +1,51 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
import createServer from '../server/createServer'
import { createTestClient } from 'apollo-server-testing'
import { users } from './users/index'
import { createUserFactory } from './factory/user'
import { entities } from '@entity/index'
const context = {
token: '',
setHeaders: {
push: (value: string): void => {
context.token = value
},
// eslint-disable-next-line @typescript-eslint/no-empty-function
forEach: (): void => {},
},
}
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])
}
}
const resetEntity = async (entity: any) => {
const items = await entity.find({ withDeleted: true })
if (items.length > 0) {
const ids = items.map((i: any) => i.id)
await entity.delete(ids)
}
}
const run = async () => {
const server = await createServer(context)
const testClient = createTestClient(server.apollo)
const { mutate } = testClient
const { con } = server
await cleanDB()
for (let i = 0; i < users.length; i++) {
await createUserFactory(mutate, users[i])
}
await con.close()
}
run()

View File

@ -0,0 +1,7 @@
import { peterLustig } from './peter-lustig'
import { bibiBloxberg } from './bibi-bloxberg'
import { bobBaumeister } from './bob-baumeister'
import { raeuberHotzenplotz } from './raeuber-hotzenplotz'
import { stephenHawking } from './stephen-hawking'
export const users = [peterLustig, bibiBloxberg, bobBaumeister, raeuberHotzenplotz, stephenHawking]