-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvitest.global-setup.ts
More file actions
65 lines (55 loc) · 1.99 KB
/
vitest.global-setup.ts
File metadata and controls
65 lines (55 loc) · 1.99 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
61
62
63
64
65
import { PostgreSqlContainer } from '@testcontainers/postgresql';
/**
* Minimal contract that allows Vitest to pass shared setup values between modules.
*/
interface GlobalSetupContextLike {
provide: (key: string, value: string) => void;
}
/**
* Container image tag used for the shared Postgres fixture during local runs.
*/
const POSTGRES_IMAGE = 'postgres:16-alpine';
/**
* Runtime type returned when a PostgreSqlContainer instance has been started.
*/
type StartedPostgresContainer = Awaited<ReturnType<PostgreSqlContainer['start']>>;
/**
* Detects whether TestContainers failed because no container runtime strategy was found.
*/
function isMissingRuntimeError(error: unknown): boolean {
return (
error instanceof Error &&
error.message.includes('Could not find a working container runtime strategy')
);
}
/**
* Ensures a shared Postgres fixture is available for Vitest and exposes the URI globally.
*/
export default async function setupGlobalPostgres({ provide }: GlobalSetupContextLike) {
if (process.env.TEST_PG_URI) {
// Respect externally supplied connections and skip container creation.
return async () => {};
}
let cleanup = async () => {};
const container = new PostgreSqlContainer(POSTGRES_IMAGE);
let startedContainer: StartedPostgresContainer | undefined;
try {
// Start a shared Postgres container for the entire Vitest run.
startedContainer = await container.start();
const connectionUri = startedContainer.getConnectionUri();
provide('TEST_PG_URI', connectionUri);
process.env.TEST_PG_URI = connectionUri;
cleanup = async () => {
// Ensure the container is stopped after all suites complete.
await startedContainer?.stop();
};
} catch (error) {
if (isMissingRuntimeError(error)) {
// Fall back when no container runtime is available on the host.
console.warn('Skipping Postgres fixture because no container runtime is available.');
return cleanup;
}
throw error;
}
return cleanup;
}