-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathvitest.setup.ts
44 lines (38 loc) · 1.14 KB
/
vitest.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
import { readdirSync, readFileSync, statSync } from 'node:fs'
import { PGlite } from '@electric-sql/pglite'
import { PrismaClient } from '@prisma/client'
import { PrismaPGlite } from 'pglite-prisma-adapter'
import { vi } from 'vitest'
const client = new PGlite()
const adapter = new PrismaPGlite(client)
const prisma = new PrismaClient({ adapter })
vi.mock('./src/server/prisma', () => ({
prisma,
}))
export const resetDb = async () => {
try {
await client.exec(`DROP SCHEMA public CASCADE`)
await client.exec(`CREATE SCHEMA public`)
} catch (error) {
console.log({ error })
}
}
const applyMigrations = async () => {
const prismaMigrationDir = './prisma/migrations'
const directory = readdirSync(prismaMigrationDir).sort()
for (const file of directory) {
const name = `${prismaMigrationDir}/${file}`
if (statSync(name).isDirectory()) {
const migration = readFileSync(`${name}/migration.sql`, 'utf8')
await client.exec(migration)
}
}
}
// Apply migrations before each test
beforeEach(async () => {
await applyMigrations()
})
// Clean up the database after each test
afterEach(async () => {
await resetDb()
})