-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjest.setup.ts
92 lines (77 loc) · 2.5 KB
/
jest.setup.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import { PGlite } from '@electric-sql/pglite'
import { Prisma, PrismaClient } from '@prisma/client'
import { readFile, readdir } from 'fs/promises'
import { MongoMemoryServer } from 'mongodb-memory-server'
import mongoose from 'mongoose'
import nock from 'nock'
import path from 'path'
import { PrismaPGlite } from 'pglite-prisma-adapter'
import connect from './src/helpers/db/initDatabase'
const client = new PGlite()
const adapter = new PrismaPGlite(client)
const prisma = new PrismaClient({ adapter })
const prismaMigrationDir = path.join(__dirname, 'prisma', 'migrations')
type DelegateNames = Prisma.TypeMap['meta']['modelProps']
type DelegateName = TuplifyUnion<DelegateNames>[0]
jest.mock('winston')
jest.mock('./src/features/authentication/authentication.service', () => ({
...jest.requireActual('./src/features/authentication/authentication.service'),
generateVerificationCodeAndExpiration: jest.fn(),
}))
jest.mock('./src/adapters/prisma/client', () => ({
prisma,
}))
const models = Prisma.dmmf.datamodel.models
.map(({ name }) => ({
name,
delegateKey: name.replace(/(?:^\w|[A-Z]|\b\w)/g, (word, index) =>
index === 0 ? word.toLowerCase() : word.toUpperCase()
) as DelegateName,
}))
.map(({ name, delegateKey }) => ({
name,
delegate: prisma[delegateKey],
}))
let mongod: MongoMemoryServer | undefined
beforeAll(async () => {
const mongoUrl = new URL(process.env.MONGO_URL || '')
mongod = await MongoMemoryServer.create({
instance: {
port: +mongoUrl.port,
},
binary: {
// version: '4.0.3', // Should be activated
},
})
await connect()
const migrationPaths = await readdir(prismaMigrationDir)
await migrationPaths
.filter((migrationPath) => migrationPath !== 'migration_lock.toml')
.map((migrationPath) =>
path.join(prismaMigrationDir, migrationPath, 'migration.sql')
)
.reduce(async (promise, filename) => {
await promise
const migration = await readFile(filename, 'utf8')
await client.exec(migration)
}, Promise.resolve())
})
afterAll(async () => {
await mongoose.disconnect()
await mongod?.stop()
})
beforeEach(() => nock.cleanAll())
afterEach(async () => {
expect(nock.isDone()).toBeTruthy()
await Promise.all(
models.map(async ({ delegate, name }) => {
try {
expect(await delegate.count({})).toBe(0)
} catch {
console.warn(
`${name} resources found after the test, please clean database after each test to avoid flaky tests`
)
}
})
)
})