# @async-fn/jest 中文文档

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

版本：1\.8\.0
npm 元数据更新时间：2026-05-04T06:00:08.344Z
中文译文更新时间：2026-11-07T01:33:41.482Z
软件包许可：MIT

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

原始 npm 页面：https://www.npmjs.com/package/%40async-fn/jest
原文对照：https://npmcn.com/package/%40async-fn/jest/?lang=original
项目主页：https://github.com/team-igniter-from-houston-inc/async-fn#readme
源码仓库：https://github.com/team-igniter-from-houston-inc/async-fn

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

- 包体积（解压后）：64.6 kB，11 个文件
- 模块格式：CommonJS
- TypeScript 类型：包内自带
- 运行依赖：0 个
- peer 依赖：jest \>= 24\.9\.0
- 首次发布：2020-06-29T07:34:13.725Z
- 最新版本发布：2026-05-04T06:00:08.201Z
- 近 30 天下载量：4194（2026-10-07 至 2026-11-05，npm 官方下载统计）
- 已发布版本数：27（近 12 个月 3 个）

## 中文简介

通过模拟函数返回的承诺的"后解"引入"后解"的附加方法. jest.fn. 这样可以进行按时间顺序读取的测试,就像故事一样.

## 安装

```sh
npm install @async-fn/jest
```

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

## README

# @ async-fn/ 欢呼

![Test status](https://github.com/team-igniter-from-houston-inc/async-fn/workflows/Jest/badge.svg) [![npm version](https://badge.fury.io/js/%40async-fn%2Fjest.svg)](https://badge.fury.io/js/%40async-fn%2Fjest) [![Gitter](https://badges.gitter.im/async-fn/community.svg)](https://gitter.im/async-fn/community?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge)

![asyncFn](https://raw.githubusercontent.com/team-igniter-from-houston-inc/async-fn/master/logo.png)

**asyncFn 数据** jest 提供了引入 jest.fn 的补充方法 "**迟解**" 因为承诺还原.

通过允许按时间顺序读取的测试来简化合成单元测试, **像故事一样**,并且不需要过度的测试设置来事先知道在每种情景下,aync mops应该如何表现.

它已经 **100%单位测试覆盖率**,和3+年的重点 **生产用途** 高级 **开发者满意度**。 。 。 .

asyncFn 也为 **综合测试** 用于组合最近的OS-,节点-和嘲笑的框架-版本.

## 如何安装

```
$ npm install --save-dev @async-fn/jest
```

## 教学

参见教程 [这里](https://medium.com/houston-io/how-to-unit-test-asynchronous-code-for-javascript-in-2020-41c124be2552)。 。 。 .

## 实例

### 迟到的发号施令 将测试看成故事

```javascript
import asyncFn from '@async-fn/jest';

it('given called, a result can be resolved *after* the mock is called', async () => {
  const mockFunction = asyncFn();
  const promise = mockFunction();

  // Note how we resolve the returned promise *after* it is called.
  // This permits us to write our tests so that they read like a story.
  await mockFunction.resolve('some-value');

  const actual = await promise;
  expect(actual).toBe('some-value');
});
```

### 迟到下定决心,多次呼吁嘲弄

```javascript
import asyncFn from '@async-fn/jest';

it('given called multiple times, the results can be resolved *after* the mock was called', async () => {
  const mockFunction = asyncFn();

  const promise = Promise.all([mockFunction(), mockFunction(), mockFunction()]);

  await mockFunction.resolve('some-first-value');
  await mockFunction.resolve('some-second-value');
  await mockFunction.resolve('some-third-value');

  const actual = await promise;

  expect(actual).toEqual([
    'some-first-value',
    'some-second-value',
    'some-third-value',
  ]);
});
```

### 等待解决的电话的巧合

```javascript
import asyncFn from '@async-fn/jest';

it('can be awaited to test the coincidences of resolve', async () => {
  const mockFunction = asyncFn();

  let coincidenceHasHappened = false;

  mockFunction()
    .then()
    .then()
    .then(() => {
      coincidenceHasHappened = true;
    });

  await mockFunction.resolve();

  expect(coincidenceHasHappened).toBe(true);
});
```

### 逾期拒绝

```javascript
import asyncFn from '@async-fn/jest';

it('can be rejected with a rejection', () => {
  const mockFunction = asyncFn();
  const promise = mockFunction();

  mockFunction.reject('some-rejection');

  return expect(promise).rejects.toBe('some-rejection');
});
```

### 其他的笑话,fn做

```javascript
import asyncFn from '@async-fn/jest';

it('does what jest.fn does', () => {
  const mockFunction = asyncFn();

  mockFunction('some-argument', 'some-other-argument');

  expect(mockFunction).toHaveBeenCalledWith(
    'some-argument',
    'some-other-argument',
  );
});
```

## 更多实例

看看这个 [单位测试](https://github.com/team-igniter-from-houston-inc/async-fn/blob/master/packages/core/test-utils/itWorksAsAsyncFn.js)。 。 。 .

## 可用性

目前AsyncFn也可用于 [鼻涕虫](https://www.npmjs.com/package/@async-fn/sinon)。 。 。 .

## 我们是谁?

asyncFn 被你的朋友精心设计 **团队: Igniter 来自 [休斯敦咨询公司](https://houston-inc.com/)**。 。 。 .

我们是软件开发团队 **朋友们**具有经证明的职业优秀传统。 我们专门从事整体的快速部署,而不牺牲质量.

来打个招呼 [减肥](https://gitter.im/async-fn/community), (中文(简体) ), [电子邮件](mailto:igniter@houston-inc.com) 我们,或者看看 [团队网站](https://team.igniter.houston.io/)我们可能只是开放 招聘

## 常见问题

### @async-fn/jest 是什么？

通过模拟函数返回的承诺的"后解"引入"后解"的附加方法\. jest\.fn\. 这样可以进行按时间顺序读取的测试,就像故事一样\.

### 如何安装 @async-fn/jest？

在项目目录运行 npm install @async-fn/jest，或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖：jest（\>= 24\.9\.0）。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖，Yarn 需要手动安装。

### @async-fn/jest 支持 TypeScript 吗？

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

### @async-fn/jest 支持 ESM 还是 CommonJS？

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

### @async-fn/jest 可以商用吗？

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

### @async-fn/jest 的下载量有多少？

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

### @async-fn/jest 还在维护吗？

最新版本 1\.8\.0 发布于 2026/05/04（6 个月前）；至今共发布 27 个版本，其中过去 12 个月发布了 3 个；首次发布于 2020/06/29。发布频率只是参考信号：功能稳定的工具可能很少需要更新，建议结合源码仓库的 issue 和提交记录判断。

