From 55551154f011bc2b955723676bac5d5176f0bca3 Mon Sep 17 00:00:00 2001 From: Cristian Greco Date: Sun, 22 Feb 2026 11:09:26 +0000 Subject: [PATCH] Fix Vitest global setup quickstart for Redis module --- docs/quickstart/global-setup.md | 56 ++++++++++++++++++++------------- 1 file changed, 34 insertions(+), 22 deletions(-) diff --git a/docs/quickstart/global-setup.md b/docs/quickstart/global-setup.md index 50152381d..9de8bb7a5 100644 --- a/docs/quickstart/global-setup.md +++ b/docs/quickstart/global-setup.md @@ -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. @@ -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"); +}); ```