@automock/jest

中文文档
2.1.3发布于 2026/11/02MITTypeScriptCommonJS近 30 天下载 24.3万

Automock is no longer maintained. The project continues as Suites, an actively developed unit testing framework that builds on Automock's original design.

原始使用文档
文档目录8 节
  1. End of support
  2. Migration
  3. Recommendation
  4. Archived Automock documentation
  5. Core Features
  6. :package: Installation
  7. :computer: Quick Example
  8. :scroll: License

Automock is now Suites

Automock is no longer maintained. The project continues as Suites, an actively developed unit testing framework that builds on Automock's original design.

End of support

Support for Automock 2.x ended on June 30, 2026. Automock no longer receives bug fixes or security updates. Published versions remain available for existing projects. All new development continues in Suites.

Migration

Migrate to @suites/unit using the migration guide.

The official codemod automates many of the code changes needed to move from Automock v2 to Suites v3. Follow the guide to update your dependencies, review the changes, and run your tests.

Recommendation

  • New users: Install Suites.
  • Existing users: Migrate to Suites for continued maintenance and updates.

Archived Automock documentation

The instructions below describe Automock 2.x and are retained for existing projects.

Automock optimizes the unit testing process by providing a virtual, isolated environment and automated mock generation, enabling developers to create efficient test suites and enhance their overall testing experience.

Codecov Coverage ci

↗️ Legacy Documentation

Core Features

🚀 Zero-Setup Mocking - Automatically generate mock objects, eliminate manual setup, reduce boilerplate code.

🔍 Type-Safe Mocks - Leverage TypeScript's power with mocks that retain the same type as real objects.

📄 Consistent Tests Structure - Test suites will follow a consistent syntax and structure, making them easier to read and maintain.

📈 Optimized Performance - By bypassing the actual DI container, unit tests run significantly faster.

:package: Installation

To fully integrate Automock into your testing and dependency injection framework, you need to install two packages: @automock/jest, and the corresponding DI framework adapter.

  1. Install Automock's Jest package:
$ npm i -D @automock/jest
  1. And for your DI framework, install the appropriate Automock adapter (as a dev dependency):
DI FrameworkPackage Name
NestJS@automock/adapters.nestjs
Inversify@automock/adapters.inversify

For example:

$ npm i -D @automock/jest @automock/adapters.nestjs

No further configuration is required.

:computer: Quick Example

Take a look at the following example:

Consider the following UserService class:

export class Database {
  async getUsers(): Promise<User[]> { ... }
}

export class UserService {
  constructor(private database: Database) {}

  async getAllUsers(): Promise<User[]> {
    return this.database.getUsers();
  }
}

Let's create a unit test for this class:

import { TestBed } from '@automock/jest';
import { Database, UserService } from './user.service';

describe('User Service Unit Spec', () => {
  let userService: UserService;
  let database: jest.Mocked<Database>;

  beforeAll(() => {
    const { unit, unitRef } = TestBed.create(UserService).compile();
    userService = unit;
    database = unitRef.get(Database);
  });

  test('should return users from the database', async () => {
    const mockUsers: User[] = [{ id: 1, name: 'John' }, { id: 2, name: 'Jane' }];
    database.getUsers.mockResolvedValue(mockUsers);

    const users = await userService.getAllUsers();

    expect(database.getUsers).toHaveBeenCalled();
    expect(users).toEqual(mockUsers);
  });
});

With the use of the TestBed, an instance of the UserService class can be created with mock objects automatically generated for its dependencies. During the test, we have direct access to the automatically generated mock object for the Database dependency (database). By stubbing the getUsers() method of the database mock object, we can define its behavior and make sure it resolves with a specific set of mock users.

Automock improves upon the existing unit testing procedures of DI frameworks by creating a virtual DI container. There is an array of advantages to this change:

  • Speed: By simulating the actual DI container in the testing environment, Automock speeds up execution times.

  • Efficiency: Developers are therefore able to focus on writing the test logic instead of grappling with the complexities of test setup.

  • Isolation: Each test runs independently with mock implementations automatically provided, creating a streamlined and interference-free testing environment.

:scroll: License

Distributed under the MIT License. See LICENSE for more information.

版本与发布记录

@automock/jest 共发布 44 个版本,首次发布于 2022/02/26;过去 12 个月发布了 3 个版本。

版本发布日期距今
2.1.3latest2026/11/025 天前
2.1.22025/12/0311 个月前
2.1.12025/11/2911 个月前
2.1.02023/12/092 年前
2.1.0-alpha.0alpha2023/12/072 年前
2.0.02023/11/112 年前

其他发布通道:next → 1.2.0,beta → 2.0.0-beta.0,rc → 2.0.0-rc.0,dev → 2.0.0-dev.8,alpha → 2.1.0-alpha.0。

关于 @automock/jest 的常见问题

@automock/jest 是什么?

Automock is no longer maintained. The project continues as Suites, an actively developed unit testing framework that builds on Automock's original design.

如何安装 @automock/jest?

在项目目录运行 npm install @automock/jest,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:jest(^26 || ^27 || ^28 || ^29)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@automock/jest 支持 TypeScript 吗?

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

@automock/jest 支持 ESM 还是 CommonJS?

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

@automock/jest 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js ^16.10.0 || ^18.12.0 || >=20.0.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@automock/jest 可以商用吗?

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

@automock/jest 的下载量有多少?

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

@automock/jest 还在维护吗?

最新版本 2.1.3 发布于 2026/11/02(5 天前);至今共发布 44 个版本,其中过去 12 个月发布了 3 个;首次发布于 2022/02/26。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。