Skip to content

Commit 2135dd3

Browse files
Fix Vitest global setup quickstart for Redis module (#1245)
1 parent 00155ce commit 2135dd3

File tree

1 file changed

+34
-22
lines changed

1 file changed

+34
-22
lines changed

docs/quickstart/global-setup.md

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
If you have many tests that require the same container, you may not want to spin up one per test.
44

55
!!! info
6-
There is a misconception that containers are heavyweight.
6+
There is a misconception that containers are heavyweight.
77

88
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.
99

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

1616
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:
1717

18-
```ts title="setup.js"
19-
import { createClient } from "redis";
20-
import { RedisContainer } from "testcontainers";
21-
22-
export async function setup() {
23-
const container = await new RedisContainer("redis:8").start();
24-
const client = createClient({ url: container.getConnectionUrl() });
25-
await client.connect();
26-
27-
globalThis.redisContainer = container;
28-
globalThis.redisClient = client;
18+
```js title="setup.js"
19+
import { RedisContainer } from "@testcontainers/redis";
20+
21+
let redisContainer;
22+
23+
export async function setup(project) {
24+
redisContainer = await new RedisContainer("redis:8").start();
25+
project.provide("redisUrl", redisContainer.getConnectionUrl());
2926
}
3027

3128
export async function teardown() {
32-
await globalThis.redisClient.disconnect();
33-
await globalThis.redisContainer.stop();
29+
await redisContainer?.stop();
3430
}
3531
```
3632
37-
```ts title="vite.config.js"
38-
import { defineConfig } from "vite";
33+
```js title="vitest.config.js"
34+
import { defineConfig } from "vitest/config";
3935

4036
export default defineConfig({
4137
test: {
42-
setupFiles: "./setup.js",
43-
}
38+
globalSetup: "./setup.js",
39+
},
4440
});
4541
```
4642
47-
And to use the container/client in your tests:
43+
`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`:
44+
45+
```js
46+
import { afterAll, beforeAll, expect, inject, test } from "vitest";
47+
import { createClient } from "redis";
48+
49+
const redisClient = createClient({ url: inject("redisUrl") });
4850

49-
```ts
50-
await globalThis.redisClient.set("key", "test-value");
51-
const result = await globalThis.redisClient.get("key");
51+
beforeAll(async () => {
52+
await redisClient.connect();
53+
});
54+
55+
afterAll(async () => {
56+
await redisClient.disconnect();
57+
});
58+
59+
test("stores and reads a value", async () => {
60+
await redisClient.set("key", "test-value");
61+
const result = await redisClient.get("key");
62+
expect(result).toBe("test-value");
63+
});
5264
```

0 commit comments

Comments
 (0)