Home

Global setup

qianmoQqianmoQ· 更新于 2026-10-19· 阅读 4 分钟· 0 次阅读

登录后可跨设备保存划线和私人笔记登录

Global setup

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.

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.

In many cases it is far easier to start a new container for each test and run them in parallel. Of course, this depends on your specific use case.

Many popular test frameworks like Jest and Vitest support global setup and teardown scripts.


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:

setup.js

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
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 redisContainer?.stop();
}

vitest.config.js

1
2
3
4
5
6
7
import { defineConfig } from "vitest/config";

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

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:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
import { afterAll, beforeAll, expect, inject, test } from "vitest";
import { createClient } from "redis";

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

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");
});

评论

登录后参与评论

正在加载评论…