Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 34 additions & 22 deletions docs/quickstart/global-setup.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
If you have many tests that require the same container, you may not want to spin up one per test.

!!! info
There is a misconception that containers are heavyweight.
There is a misconception that containers are heavyweight.

Sure, if your container has a slow startup time (e.g., a database, which on startup runs large migration scripts), it may be better to just start and manage one instance. But keep in mind that this limits your tests to run sequentially, and you may need to manage the state of the container between tests.

Expand All @@ -15,38 +15,50 @@ Many popular test frameworks like Jest and Vitest support global setup and teard

Here's an example which sets up a single Redis container globally, so it can be reused across tests. In this case we're using Vitest:

```ts title="setup.js"
import { createClient } from "redis";
import { RedisContainer } from "testcontainers";

export async function setup() {
const container = await new RedisContainer("redis:8").start();
const client = createClient({ url: container.getConnectionUrl() });
await client.connect();

globalThis.redisContainer = container;
globalThis.redisClient = client;
```js title="setup.js"
import { RedisContainer } from "@testcontainers/redis";

let redisContainer;

export async function setup(project) {
redisContainer = await new RedisContainer("redis:8").start();
project.provide("redisUrl", redisContainer.getConnectionUrl());
}

export async function teardown() {
await globalThis.redisClient.disconnect();
await globalThis.redisContainer.stop();
await redisContainer?.stop();
}
```

```ts title="vite.config.js"
import { defineConfig } from "vite";
```js title="vitest.config.js"
import { defineConfig } from "vitest/config";

export default defineConfig({
test: {
setupFiles: "./setup.js",
}
globalSetup: "./setup.js",
},
});
```

And to use the container/client in your tests:
`globalSetup` runs in a different global scope than test files. To share data with tests, provide serializable values in setup and read them with `inject`:

```js
import { afterAll, beforeAll, expect, inject, test } from "vitest";
import { createClient } from "redis";

const redisClient = createClient({ url: inject("redisUrl") });

```ts
await globalThis.redisClient.set("key", "test-value");
const result = await globalThis.redisClient.get("key");
beforeAll(async () => {
await redisClient.connect();
});

afterAll(async () => {
await redisClient.disconnect();
});

test("stores and reads a value", async () => {
await redisClient.set("key", "test-value");
const result = await redisClient.get("key");
expect(result).toBe("test-value");
});
```
Loading