Repository navigation
Expand file tree
/
Copy pathglobalSetup.ts
More file actions
60 lines (50 loc) · 1.49 KB
/
Copy pathglobalSetup.ts
File metadata and controls
60 lines (50 loc) · 1.49 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 { PostgreSqlContainer } from "@testcontainers/postgresql";
import { Network } from "testcontainers";
import type { TestProject } from "vitest/node";
const create = async (project: TestProject) => {
const network = await new Network().start();
const postgres = await new PostgreSqlContainer("postgres:18")
// set a much larger max_connections
.withCommand(["postgres", "-c", `max_connections=1000`])
.withNetwork(network)
.start();
project.provide("NETWORK_ID", network.getId());
project.provide("NETWORK_NAME", network.getName());
project.provide("POSTGRES_ID", postgres.getId());
project.provide("POSTGRES_AUTH", {
host: postgres.getHost(),
port: postgres.getPort(),
database: postgres.getDatabase(),
user: postgres.getUsername(),
password: postgres.getPassword(),
hostname: postgres.getHostname(),
});
return { postgres, network };
};
export default async (project: TestProject) => {
let res = await create(project);
project.onTestsRerun(async () => {
await res.postgres.stop({ removeVolumes: true });
await res.network.stop().catch(console.error);
res = await create(project);
});
return async () => {
await res.postgres.stop({ removeVolumes: true });
await res.network.stop();
};
};
declare module "vitest" {
export interface ProvidedContext {
NETWORK_ID: string;
NETWORK_NAME: string;
POSTGRES_ID: string;
POSTGRES_AUTH: {
host: string;
port: number;
database: string;
user: string;
password: string;
hostname: string;
};
}
}