@async-fn/jest

中文文档
1.8.0发布于 2026/05/04MITTypeScriptCommonJS零依赖近 30 天下载 4194

Additional methods to jest.fn to introduce "late resolve" of promises returned by mock functions. This allows tests that read chronologically, like a story.

原始使用文档
文档目录11 节
  1. How to install
  2. Tutorial
  3. Examples
  4. Late resolve for calls to a mock to make tests read like a …
  5. Late resolve for multiple calls to a mock
  6. Awaiting for coincidences of resolved calls
  7. Late rejection
  8. The other stuff jest.fn does
  9. More examples
  10. Availability
  11. Who are we?

@async-fn/jest

Test status npm version Gitter

asyncFn

asyncFn for jest provides additional methods to jest.fn to introduce "late resolve" for the promises returned.

This simplifies async unit testing by allowing tests that read chronologically, like a story, and do not require excessive test setup to know beforehand how async mocks are supposed to behave in each scenario.

It has 100% unit test coverage, and 3+ years of focused production use with high developer satisfaction.

asyncFn is also integration-tested for combinations of recent OS-, node- and mocking framework -versions.

How to install

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

Tutorial

See tutorial here.

Examples

Late resolve for calls to a mock to make tests read like a story

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

Late resolve for multiple calls to a mock

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',
  ]);
});

Awaiting for coincidences of resolved calls

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);
});

Late rejection

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

The other stuff jest.fn does

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

More examples

Check out the unit tests.

Availability

Currently asyncFn is also available for sinon.

Who are we?

asyncFn is lovingly crafted by Your pals at Team: Igniter from Houston Inc. Consulting.

We are a software development team of friends, with proven tradition in professional excellence. We specialize in holistic rapid deployments without sacrificing quality.

Come say hi at Gitter, email us, or check out the team's website. We just might be open to hiring ;)

版本与发布记录

@async-fn/jest 共发布 27 个版本,首次发布于 2020/06/29;过去 12 个月发布了 3 个版本。

版本发布日期距今
1.8.0latest2026/05/046 个月前
1.7.02026/04/266 个月前
1.6.52026/02/258 个月前
1.6.42022/07/084 年前
1.6.32022/07/084 年前
1.6.22022/07/054 年前

关于 @async-fn/jest 的常见问题

@async-fn/jest 是什么?

Additional methods to jest.fn to introduce "late resolve" of promises returned by mock functions. This allows tests that read chronologically, like a story.

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