-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathvitest.setup.ts
76 lines (63 loc) · 1.92 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
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
import { vi, beforeEach, afterAll } from 'vitest'
if (globalThis.window) {
Object.defineProperty(globalThis.window, 'matchMedia', {
writable: true,
value: vi.fn().mockImplementation(query => ({
matches: false,
media: query,
onchange: null,
addListener: vi.fn(),
removeListener: vi.fn(),
addEventListener: vi.fn(),
removeEventListener: vi.fn(),
dispatchEvent: vi.fn(),
})),
})
} else {
const { loadConfig } = await import('@faasjs/load')
const { readFileSync } = await import('node:fs')
const { default: knex } = await import('knex')
const { useHttp } = await import('@faasjs/http')
const { useKnex } = await import('@faasjs/knex')
if (!process.env.SECRET_HTTP_COOKIE_SESSION_SECRET)
process.env.SECRET_HTTP_COOKIE_SESSION_SECRET = 'secret'
const config = loadConfig(__dirname, '', 'testing')
let schema: string
let tables: string[]
function log(message: string) {
process.stdout.write(`${message}\n`)
}
beforeEach(async () => {
if (!schema) {
schema = readFileSync(`${__dirname}/schema.sql`).toString()
const db = knex({
client: 'pg',
connection: process.env.SECRET_KNEX_CONNECTION,
})
await db.raw('DROP SCHEMA IF EXISTS public CASCADE;CREATE SCHEMA public;')
await db.raw(schema)
await db.destroy()
await useHttp().mount()
await useKnex({
config: config.plugins.knex.config,
}).mount()
}
try {
const db = knex({
client: 'pg',
connection: process.env.SECRET_KNEX_CONNECTION,
})
if (!tables)
tables = await db('pg_tables')
.where('schemaname', '=', 'public')
.pluck('tablename')
await db.raw(tables.map(t => `TRUNCATE ${t} RESTART IDENTITY`).join(';'))
await db.destroy()
} catch (error: any) {
log(error?.message)
}
})
afterAll(async () => {
await useKnex().quit()
})
}