# @blueground/jest-testcontainers 中文文档

规范地址：https://npmcn.com/package/%40blueground/jest-testcontainers/

版本：3\.0\.6
npm 元数据更新时间：2025-09-05T15:49:40.233Z
中文译文更新时间：2026-11-08T08:31:40.460Z
软件包许可：MIT

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/%40blueground/jest-testcontainers
原文对照：https://npmcn.com/package/%40blueground/jest-testcontainers/?lang=original
项目主页：https://github.com/bluegroundltd/jest-testcontainers#readme
源码仓库：https://github.com/bluegroundltd/jest-testcontainers

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：29 kB，17 个文件
- 模块格式：CommonJS
- TypeScript 类型：包内自带
- 运行依赖：2 个
- peer 依赖：jest-environment-node \>=25
- 首次发布：2022-11-10T14:54:51.500Z
- 最新版本发布：2025-07-09T13:09:01.391Z
- 近 30 天下载量：3565（2026-10-09 至 2026-11-07，npm 官方下载统计）
- 已发布版本数：7（近 12 个月 0 个）

## 中文简介

开始装填容器的预置程序 继续测试.

## 安装

```sh
npm install @blueground/jest-testcontainers
```

也可以使用：`pnpm add @blueground/jest-testcontainers`、`yarn add @blueground/jest-testcontainers`、`bun add @blueground/jest-testcontainers`

## README

# 测试容器

_从 [趋势醇/检测容器](https://github.com/trendyol/jest-testcontainers)_

### 免责声明

伪造的主要原因是先前的回购不再维持,导致 `testcontainers` API为已过时. 此外,我们需要支持多管多管多克 图像,在以后版本中支持 `testcontainers-node`。 。 。 .

测试时预置了套装容器 主要目的是在整合测试中使用任何数据库。 由于它使用道克图像,所以在集成测试中可以使用不同插件/配置的自定义数据库图像. 使用 [测试容器-节点](https://github.com/testcontainers/testcontainers-node) 在引擎盖下。 灵感来自 [@shelf/jest-mongodb (英语)](https://github.com/shelfio/jest-mongodb)。 。 。 .

[![Test Jest-Containers](https://github.com/bluegroundltd/jest-testcontainers/actions/workflows/publish.yml/badge.svg?branch=master)](https://github.com/bluegroundltd/jest-testcontainers/actions/workflows/test.yml) [![npm version](https://badge.fury.io/js/@blueground%2Fjest-testcontainers.svg)](https://badge.fury.io/js/@blueground%2Fjest-testcontainers)

## 使用量

### 安装

应该安装在您的系统中 。 如果在CI管道内运行,见 [财务问题](https://npmcn.com/package/%40blueground/jest-testcontainers/FAQ.md)。 。 。 .

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

### 编辑配置

在你身边 `jest.config.js` 添加工程为预设.

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

### 宣布要运行的容器

创建名为的文件 `jest-testcontainers-config.js` 并把它放在你的项目的根,你运行的地方 _npm 测试_如果您想把这个文件放在别的地方,你可以使用 _(原始内容存档于2019-10-21) (中文(中国大陆) ). JEST TESTCONERS CONFIG PATH_ 用于定义相对路径或绝对路径的环境变量。 样本配置文件;

```js
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
//   },
};
```

### 连接到容器

每个容器IP和Port信息都被注册到全局变量中,供您测试使用.

```js
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');
  });
});
```

## 文档

联合国 `jest-testcontainers-config.js` 可见于 [DOC. m.d (英语)](https://npmcn.com/package/%40blueground/jest-testcontainers/DOC.md)。 。 。 .

## 实例

-   [例/01 - 基本内容](https://npmcn.com/package/%40blueground/jest-testcontainers/examples/01-basic-redis) 展示针对 Redis 的写作集成测试。 你用得着 `npm run build && npm run example:redis` 命令可在本地运行此示例.
-   [例/ 02 类型script- redis](https://npmcn.com/package/%40blueground/jest-testcontainers/examples/02-typescript-redis) 试验案例与例一相同. 然而使用Typescript而不是JavaScript. 你用得着 `npm run build && npm run example:redis-typescript` 以本地操作此示例.
-   [实例/ 03- docker- 构成](https://npmcn.com/package/%40blueground/jest-testcontainers/examples/03-docker-compose) 与例1和例2相同的测试案例,然而使用多克编译来构建容器. 你用得着 `npm run build && npm run example:docker-compose` 以本地操作此示例.
-   [延加/Nodejs-postgresql-测试容器](https://github.com/Yengas/nodejs-postgresql-testcontainers) 显示针对 PostgreSQL 的集成测试, 并使用 schema 迁移和 Typescription 。 您可以查看项目页面了解更多详情.

## 监视模式支持

如果 Jest 以监视模式启动, 则2. 0.0 版本的容器将不会停止 。 如果您的容器的启动时间( 如 Elasticsearch) 较慢, 则会大大提高开发生产率 。 这需要付出的代价是,你必须注意,容器会在表试死刑之间被再利用,而且你需要在钩后进行适当的清理.

如果您想要禁用此行为, 您可以设置 `JEST_TESTCONTAINERS_RESTART_ON_WATCH` 环境变量.

> 想知道当Jest退出时这些容器没有被停止时会发生什么 [测试容器的叫声](https://github.com/testcontainers/testcontainers-node#ryuk) 我会照顾他们.

## 许可证

这个项目根据麻省理工学院的许可证颁发

## 常见问题

### @blueground/jest-testcontainers 是什么？

开始装填容器的预置程序 继续测试\.

### 如何安装 @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 和提交记录判断。

