Usage
As an example, let's spin up and test a Redis container.
First, install the dependencies:
| npm install testcontainers --save-dev
npm install redis
|
Next, we'll write a test that starts a Redis container, connects to it, and performs an operation:
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 | import { createClient, RedisClientType } from "redis";
import { GenericContainer, StartedTestContainer } from "testcontainers";
describe("Redis", () => {
let container: StartedTestContainer;
let redisClient: RedisClientType;
beforeAll(async () => {
container = await new GenericContainer("redis:8")
.withExposedPorts(6379)
.start();
redisClient = createClient({
url: `redis://${container.getHost()}:${container.getMappedPort(6379)}`
});
await redisClient.connect();
});
afterAll(async () => {
await redisClient.disconnect();
await container.stop();
});
it("works", async () => {
await redisClient.set("key", "val");
expect(await redisClient.get("key")).toBe("val");
});
});
|
Run the test, and after a few seconds, it passes!
Note
Why did it take a few seconds?
Because your container runtime first had to pull the image. If you run the test again, it'll run faster.
The complexity of configuring a container varies.
For Redis, it's pretty simple, we just expose a port. But for example, to define a GenericContainer
for PostgreSQL, you'd need to configure multiple ports, environment variables for credentials, custom wait strategies, and more. For this reason there exists a catalogue of pre-defined modules, which abstract away this complexity.
If a module exists for the container you want to use, it's highly recommended to use it.
For example, using the Redis module, the example above can be simplified:
| npm install @testcontainers/redis --save-dev
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 | import { createClient, RedisClientType } from "redis";
import { RedisContainer, StartedRedisContainer } from "@testcontainers/redis";
describe("Redis", () => {
let container: StartedRedisContainer;
let redisClient: RedisClientType;
beforeAll(async () => {
container = await new StartedRedisContainer("redis:8").start();
redisClient = createClient({ url: container.getConnectionUrl() });
await redisClient.connect();
});
afterAll(async () => {
await redisClient.disconnect();
await container.stop();
});
it("works", async () => {
await redisClient.set("key", "val");
expect(await redisClient.get("key")).toBe("val");
});
});
|