# @bigtest/mocha 中文文档

规范地址：https://npmcn.com/package/%40bigtest/mocha/

版本：0\.5\.2
npm 元数据更新时间：2026-09-30T21:07:27.112Z
中文译文更新时间：2026-11-08T02:23:36.133Z
软件包许可：MIT

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

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

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

- 包体积（解压后）：40.9 kB，9 个文件
- 模块格式：ESM + CJS
- TypeScript 类型：未声明
- 运行依赖：1 个
- peer 依赖：mocha \>= 4\.0\.0
- 首次发布：2018-01-08T20:40:18.955Z
- 最新版本发布：2018-10-23T20:12:36.756Z
- 近 30 天下载量：3981（2026-10-08 至 2026-11-06，npm 官方下载统计）
- 已发布版本数：14（近 12 个月 0 个）

## 中文简介

Mocha大测试的助手

## 安装

```sh
npm install @bigtest/mocha
```

也可以使用：`pnpm add @bigtest/mocha`、`yarn add @bigtest/mocha`、`bun add @bigtest/mocha`

## README

# @ bigtest/mocha (英语) [![CircleCI](https://circleci.com/gh/bigtestjs/mocha.svg?style=svg)](https://circleci.com/gh/bigtestjs/mocha)

用于同步状态测试的同步 Mocha 函数

## 概要

```javascript
import { describe, beforeEach, it } from '@bigtest/mocha';
import { expect } from 'chai';

describe('clicking my button', () => {
  beforeEach(() => $button.click());

  // repeatedly asserts until passing
  it('shows a loading indicator', () => {
    expect($button.className).to.include('is-loading');
  });

  // repeatedly asserts it is passing until the timeout
  it.always('does not navigate away', () => {
    expect(app.location).to.equal('/')
  }).timeout(200);
})
```

## 同源请求

通常,当测试同步状态时(例如制取的内容) 您的测试需要在正确的时间进行 在正确的上下文中执行。 如果你的测试也成功 很快,他们就会失败,如果他们跑得太慢, 那么,你只是有 慢测试.

汇合在一个国家中,意味着反对一个国家,直到 ,在这种情况下,你已成功聚合 那个州!

此软件包使用 [`@bigtest/convergence`](https://github.com/thefrontside/bigtest/tree/master/packages/convergence) 重复陈述,直到他们通过或暂停 过期。 以这种方式进行测试 让他们能通过瞬间 达到理想状态。 这导致了非常快的测试 当 测试一个同步的东西.

_读取 `@bigtest/convergence` [文档在 趋同](https://github.com/thefrontside/bigtest/tree/master/packages/convergence#why-convergence) 更多的信息,为什么在理想状态上聚合比 试着好好把握时间._

## 怎么会这样?

大部分情况下,你写测试的方式 与你一样 常与摩诃迦叶作作是书. 唯一的区别是这个 包包 Mocha 软件包 `it` 在交汇器中 您使用 `it` 成为一致的说法 允许您轻松测试同步状态 .

此套件还包了 Mocha 钩子 `before`, (中文(简体) ), `after`, (中文(简体) ), `beforeEach`,以及 `afterEach` 自动支持时间和 运行返回 `Convergence` 来自 `@bigtest/convergence`。 。 。 .

### 写入测试

因为一致的断言是反复运行 直到他们通过,它 实值 _高度建议_ 那个你 **别** 执行任何副作用 在你的断言。 这将导致你的副作用运行 多次,也许甚至数百次.

为此,你应该把副作用生成代码保存在 钩子和 **从你的说法.** 这些"纯粹的断言"也 帮助您的测试更加清晰易读.

#### : no entry sign:不这样做:

```javascript
describe('my button', () => {
  it('shows a loading indicator after clicking', () => {
    // this will be called every time the assertion runs
    $button.click();

    // it might take a few milliseconds for any side-effects to
    // happen, so this might fail the first time and cause this entire
    // assertion to run again, thus clicking the button again
    expect($button.className).to.include('is-loading');
  });
});
```

#### : white check mark:做到这一点:

```javascript
describe('clicking my button', () => {
  // keep side-effects inside hooks
  beforeEach(() => $button.click());

  // a pure assertion has no side-effects; even if it fails, it can
  // be run again and again without consequence
  it('shows a loading indicator', () => {
    expect($button.className).to.include('is-loading');
  });
});
```

#### 讨人喜欢 _未发生_

另一种常见的情况是断言 **没有** 发生了。 如果你通常要测试这个(或者甚至用 以上趋同的断言) 测试可能通过 在副作用出现之前就成功.

在这些情景中,你想在状态遇到一个 期望 _特定期间_。换句话说,"如果 在X 时间长度上,此测试依然正确 "即将经过"

`@bigtest/mocha` 提供 `it.always` 就是这个方法 这个 方法将贯穿整个期间 确保它永远不会失败。 当断言失败时,测试 失败。 如果这个断言从未失败过,它就会在紧接着 暂停期.

```javascript
describe('clicking my button', () => {
  beforeEach(() => $button.click());

  // the default timeout for it.always is 100ms
  it.always('does not navigate away for at least 1 second', () => {
    expect(app.location).to.equal('/');
  }).timeout(1000);
});
```

### 牵引钩

有时你可能试图执行同步任务以发现它失败 由于未达到预想状态。 举个例子,你不能 如果DOM中不存在,请单击按钮。 你可以用这个 `@bigtest/convergence` 与这些州汇合并返回 在你的钩子内会合 钩子由 `@bigtest/mocha` 将自动设置超时并返回运行 `Convergence` 实例.

```javascript
describe('clicking my button', () => {
  // @bigtest/mocha will wait for a returned Convergence to converge
  // before continuing with the assertions
  beforeEach(() => new Convergence()
    .once(() => expect($button).to.exist)
    .do(() => $button.click()));

  it('shows a loading indicator', () => {
    expect($button.className).to.include('is-loading');
  });
});
```

看看这个 `@bigtest/convergence` [API 密码 文档](https://github.com/thefrontside/bigtest/tree/master/packages/convergence#using-convergence) 与《公约》 `Convergence` 课.

### 暂停测试

暂停测试在调试时非常有用. 它允许你 在测试的关键时刻调查您的申请 套间。 莫查没有停止测试的说服力,但 `@bigtest/mocha` 通过提供 `it.pause` 方法。 它通过设置当前超时到 `0` 并赐予莫查 永远无法解脱的诺言 这有效地暂停了整个 套房直到您删除 `it.pause` 并重新开始测试.

```javascript
describe('clicking my button', () => {
  beforeEach(() => $button.click());

  // if the class is never set, this test will fail; it.pause allows
  // us to investigate the app at this point in the test suite
  it.pause('shows a loading indicator', () => {
    expect($button.className).to.include('is-loading');
  });
});
```

## 常见问题

### @bigtest/mocha 是什么？

Mocha大测试的助手

### 如何安装 @bigtest/mocha？

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

### @bigtest/mocha 支持 TypeScript 吗？

该包的 package\.json 没有声明类型入口。需要类型提示时，可以在 npm 上查找社区维护的 @types/bigtest\_\_mocha 是否存在。

### @bigtest/mocha 支持 ESM 还是 CommonJS？

同时提供 ES Module 与 CommonJS 入口，import 和 require\(\) 都可以使用，打包工具会自动选择合适的版本。

### @bigtest/mocha 可以商用吗？

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

### @bigtest/mocha 的下载量有多少？

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

### @bigtest/mocha 还在维护吗？

最新版本 0\.5\.2 发布于 2018/10/24（8 年前）；至今共发布 14 个版本，其中过去 12 个月发布了 0 个；首次发布于 2018/01/09。发布频率只是参考信号：功能稳定的工具可能很少需要更新，建议结合源码仓库的 issue 和提交记录判断。

