@blueground/jest-testcontainers

中文文档
3.0.6发布于 2025/07/09MITTypeScriptCommonJS近 30 天下载 3565

Jest preset for starting docker containers that stay up whilist your tests run.

原始使用文档
文档目录10 节
  1. Disclaimer
  2. Usage
  3. Install
  4. Edit Jest Config
  5. Declare Containers To Run
  6. Connect To Containers
  7. Documentation
  8. Examples
  9. Watch mode support
  10. License

jest-testcontainers

Forked from trendyol/jest-testcontainers

Disclaimer

Main reason for forking is that the previous repo was not maintained anymore, causing the testcontainers API to be outdated. Additionally, we needed to support multi-arch Docker images, which has been supported in later versions of testcontainers-node.

Jest preset for running docker containers with your tests. Primary purpose is to make it possible to use any database in integration tests. Since it uses docker images, custom database images with different plugins/configurations can be used in the integration tests. Using testcontainers-node under the hood. Inspired by @shelf/jest-mongodb.

Test Jest-Containers npm version

Usage

Install

Docker should be installed on your system. If running inside a CI pipeline, see FAQ.md.

npm install --save-dev @blueground/jest-testcontainers

Edit Jest Config

On your jest.config.js add the project as the preset.

module.exports = {
  preset: '@blueground/jest-testcontainers'
};

Declare Containers To Run

Create a file called jest-testcontainers-config.js and put it to root of your project, where you run npm test. If you would like to put this file somewhere else, you can use the JEST_TESTCONTAINERS_CONFIG_PATH environment variable to define a relative or absolute path. A sample configuration file;

module.exports = {
  redis: {
    image: 'redis',
    tag: 'alpine3.12',
    ports: [6379],
    env: {
      EXAMPLE: 'env',
    },
    wait: {
      type: 'text',
      text: 'Ready to accept connections'
    }
  },
//   more: {
//     image: 'any-docker-image', // postgresql, mongodb, neo4j etc.
//     ports: [1234, 4567], // ports to make accessible in tests
//   },
};

Connect To Containers

Every containers IP and Port info is registered to global variables to be used by your tests.

const redis = require('redis');
const { promisify } = require('util');

describe('testcontainers example suite', () => {
  let redisClient;

  beforeAll(() => {
    const redisConnectionURI = `redis://${global.__TESTCONTAINERS_REDIS_IP__}:${global.__TESTCONTAINERS_REDIS_PORT_6379__}`;
    redisClient = redis.createClient(redisConnectionURI);
    
    // if you have declared multiple containers, they will be available to access as well. e.g.
    // `global.__TESTCONTAINERS_${CONFIG_KEY}_IP__`
    // `global.__TESTCONTAINERS_${CONFIG_KEY}_PORT_${CONFIG_PORT}__`
  });

  afterAll(() => {
    redisClient.quit();
  });
  
  it('write should be ok', async () => {
    // Arrange
    const setAsync = promisify(redisClient.set).bind(redisClient);

    // Act
    const setResult = await setAsync('test', 73);

    // Assert
    expect(setResult).toEqual('OK');
  });
});

Documentation

Detailed documentation of the jest-testcontainers-config.js can be found at DOC.md.

Examples

  • examples/01-basic-redis showcases writing integration tests against Redis. You can use npm run build && npm run example:redis command to run this example locally.
  • examples/02-typescript-redis same test cases as in example #1. However using Typescript instead of JavaScript. You can use npm run build && npm run example:redis-typescript to run this example locally.
  • examples/03-docker-compose same test cases as examples #1 and #2, however using Docker Compose to build the container. You can use npm run build && npm run example:docker-compose to run this example locally.
  • Yengas/nodejs-postgresql-testcontainers showcases writing integration tests against PostgreSQL with schema migration and Typescript. You can check out the project page for more details.

Watch mode support

Starting with version 2.0.0 containers will not be stopped if Jest is started in watch mode. This greatly improves development productivity if your containers have a slow startup time (ex. Elasticsearch). This comes with the price that you have to be mindful that containers will be reused between watch test executions and you need to do proper cleanup in your after hooks.

If you want to disable this behavior you can set the JEST_TESTCONTAINERS_RESTART_ON_WATCH environment variable.

Wondering what will happen when those containers are not stopped when Jest is exited - testcontainer's ryuk will take care of them.

License

This project is licensed under the MIT License

版本与发布记录

@blueground/jest-testcontainers 共发布 7 个版本,首次发布于 2022/11/10;过去 12 个月发布了 0 个版本。

版本发布日期距今
3.0.6latest2025/07/091.3 年前
3.0.52025/07/091.3 年前
3.0.42023/12/122 年前
3.0.32022/11/113 年前
3.0.22022/11/103 年前
3.0.12022/11/103 年前

关于 @blueground/jest-testcontainers 的常见问题

@blueground/jest-testcontainers 是什么?

Jest preset for starting docker containers that stay up whilist your tests run.

如何安装 @blueground/jest-testcontainers?

在项目目录运行 npm install @blueground/jest-testcontainers,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:jest-environment-node(>=25)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@blueground/jest-testcontainers 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@blueground/jest-testcontainers 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@blueground/jest-testcontainers 可以商用吗?

@blueground/jest-testcontainers 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@blueground/jest-testcontainers 的下载量有多少?

根据 npm 官方下载统计,@blueground/jest-testcontainers 在 2026/10/09 – 2026/11/07 这 30 天内被下载 3,565 次,日均约 119 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@blueground/jest-testcontainers 还在维护吗?

最新版本 3.0.6 发布于 2025/07/09(1.3 年前);至今共发布 7 个版本,其中过去 12 个月发布了 0 个;首次发布于 2022/11/10。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。