-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetupTest.ts
More file actions
50 lines (44 loc) · 1.44 KB
/
setupTest.ts
File metadata and controls
50 lines (44 loc) · 1.44 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
import {
PostgreSqlContainer,
type StartedPostgreSqlContainer,
} from "@testcontainers/postgresql";
import { execSync } from "child_process";
import type { NodePgDatabase } from "drizzle-orm/node-postgres";
import { drizzle } from "drizzle-orm/node-postgres";
import { Redis } from "ioredis";
import { Client } from "pg";
import { GenericContainer, type StartedTestContainer } from "testcontainers";
let postgresContainer: StartedPostgreSqlContainer;
let postgresClient: Client;
let testDb: NodePgDatabase;
let redisContainer: StartedTestContainer;
let redisClient: Redis;
beforeAll(async () => {
postgresContainer = await new PostgreSqlContainer().start();
execSync("pnpm cross-env SKIP_ENV_VALIDATION='1' drizzle-kit push:pg", {
env: {
DATABASE_URL: postgresContainer.getConnectionUri(),
NODE_ENV: "test",
},
});
const postgresSql = new Client(postgresContainer.getConnectionUri());
testDb = drizzle(postgresSql);
redisContainer = await new GenericContainer("redis")
.withExposedPorts(6379)
.withStartupTimeout(10_000)
.start();
redisClient = new Redis({
host: redisContainer.getHost(),
port: redisContainer.getFirstMappedPort(),
});
});
afterAll(async () => {
if (postgresContainer) {
await postgresContainer.stop();
}
if (redisContainer) {
await redisContainer.stop();
}
});
// add some timeout until containers are up and working
export { postgresClient, testDb, redisClient as testRedis };