This repository was archived by the owner on Apr 6, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathvitest.setup.ts
More file actions
60 lines (53 loc) · 1.68 KB
/
vitest.setup.ts
File metadata and controls
60 lines (53 loc) · 1.68 KB
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
import {getRedis, seedRedis} from "~~/tests/core/redis";
import {getPostgres, seedDatabase} from "~~/tests/core/database";
import c from "tinyrainbow"
import {AbstractStartedContainer} from "testcontainers";
import {TestProject} from "vitest/node";
const PREFIX = c.bgBlue(c.white(" SETUP "))
let containers: AbstractStartedContainer[] = []
export const setup = async (p: TestProject) => {
if (containers.length)
return
console.log(`${PREFIX} Starting containers...`)
const redis = await getRedis().start()
const postgres = await getPostgres().start()
console.log(`${PREFIX} Containers started. Waiting 5s for them to be ready...`)
await new Promise(resolve => setTimeout(resolve, 5000))
console.log(`${PREFIX} Seeding redis...` )
await seedRedis(redis)
console.log(`${PREFIX} Seeding database...`)
await seedDatabase(postgres)
containers = [redis, postgres]
p.provide("config", {
host: redis.getHost(),
port: redis.getPort(),
password: redis.getPassword()
})
p.provide("database", {
host: postgres.getHost(),
port: postgres.getPort(),
user: postgres.getUsername(),
password: postgres.getPassword()
})
}
export const teardown = async () => {
console.log(`${PREFIX} Stopping containers...`)
for (const container of containers) {
await container.stop()
}
}
declare module 'vitest' {
export interface ProvidedContext {
config: {
host: string,
port: number,
password: string
},
database: {
host: string,
port: number,
user: string,
password: string
}
}
}