Skip to content

Commit bcf862e

Browse files
Add more examples to quickstart
1 parent 92a6764 commit bcf862e

File tree

1 file changed

+65
-3
lines changed

1 file changed

+65
-3
lines changed

docs/quickstart.md

Lines changed: 65 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,14 @@ describe("Redis", () => {
2727
let redisClient: RedisClientType;
2828

2929
beforeAll(async () => {
30-
container = await new GenericContainer("redis").withExposedPorts(6379).start();
31-
redisClient = createClient({ url: `redis://${container.getHost()}:${container.getMappedPort(6379)}` });
30+
container = await new GenericContainer("redis")
31+
.withExposedPorts(6379)
32+
.start();
33+
34+
redisClient = createClient({
35+
url: `redis://${container.getHost()}:${container.getMappedPort(6379)}`
36+
});
37+
3238
await redisClient.connect();
3339
});
3440

@@ -46,7 +52,63 @@ describe("Redis", () => {
4652

4753
Run the test, and after a few seconds, it passes!
4854

49-
Why did it take a few seconds? Because your container runtime likely had to pull the image first. If you run the test again, it'll likely pass much faster.
55+
Why did it take a few seconds? Because your container runtime likely had to pull the image first. If you run the test again, it'll run faster.
56+
57+
## Global setup
58+
59+
If you have a lot of tests that require a Redis container, you might not want to spin up 100s of Redis containers.
60+
61+
In this case a common pattern is to set the container up globally, and reuse it in your tests. Here's an example using Vitest:
62+
63+
```ts
64+
// setup.js
65+
66+
import { createClient, RedisClientType } from "redis";
67+
import { GenericContainer, StartedTestContainer } from "testcontainers";
68+
69+
export async function setup() {
70+
globalThis.redisContainer = await new GenericContainer("redis")
71+
.withExposedPorts(6379)
72+
.start();
73+
74+
globalThis.redisClient = createClient({
75+
url: `redis://${redisContainer.getHost()}:${redisContainer.getMappedPort(6379)}`
76+
});
77+
78+
await redisClient.connect();
79+
}
80+
81+
export async function teardown() {
82+
await globalThis.redisClient.disconnect();
83+
await globalThis.redisContainer.stop();
84+
}
85+
```
86+
87+
The Vite configuration:
88+
89+
```ts
90+
// vite.config.js
91+
92+
import { defineConfig } from "vite";
93+
94+
export default defineConfig({
95+
test: {
96+
setupFiles: "./setup.js",
97+
}
98+
});
99+
```
100+
101+
And how to reference the container/client in your tests:
102+
103+
```ts
104+
it("should set and retrieve a value from Redis", async () => {
105+
await globalThis.redisClient.set("key", "test-value");
106+
const result = await globalThis.redisClient.get("key");
107+
expect(result).toBe("test-value");
108+
});
109+
```
110+
111+
## Logging
50112

51113
It would be nice to see what Testcontainers is doing while the test is running. You can enable all debug logging by setting the `DEBUG` environment variable. For example:
52114

0 commit comments

Comments
 (0)