@bonniernews/mocha-cakes-2

中文文档
1.0.0发布于 2026/10/23ISCTypeScriptCommonJS零依赖构建来源已验证近 30 天下载 3334

A Mocha BDD integration

原始使用文档
文档目录18 节
  1. Installation
  2. Requirements
  3. Usage
  4. Enable the @bonniernews/mocha-cakes-2 integration
  5. Test structure
  6. Upgrading from mocha-cakes-2 3.x
  7. Upgrading from version 1.x
  8. TypeScript
  9. API
  10. .skip
  11. .only
  12. beforeEachScenario and afterEachScenario
  13. beforeEachFeature and afterEachFeature
  14. Development
  15. Continuous integration
  16. Testing against Mocha 11
  17. Testing the CLI and API interfaces
  18. Acknowledgements

Mocha Cakes 2

Mocha Cakes is a Gherkin/Cucumber syntax integration for the Mocha testing framework.

CI Sponsored

Installation

NPM:

npm install --save-dev @bonniernews/mocha-cakes-2

Requirements

  • Node.js 22.12 or later
  • Mocha 11 or 12 (mocha is a peer dependency: mocha >= 11)

Mocha Cakes 2 is tested on Node.js 22, 24 and 26 with Mocha 11 and 12.

If you're on an older version of Node.js or Mocha, use [email protected].

Usage

Enable the @bonniernews/mocha-cakes-2 integration

To enable the Mocha integration you need to specify @bonniernews/mocha-cakes-2 in the ui option.

CLI

Either use the command line argument:

mocha --ui @bonniernews/mocha-cakes-2 path/to/my/tests

Or set it in your Mocha configuration file, e.g. .mocharc.json:

{
  "ui": "@bonniernews/mocha-cakes-2"
}

Or .mocharc.yml:

ui: "@bonniernews/mocha-cakes-2"

API

Either pass it in the options as you construct Mocha:

var Mocha = require('mocha');
// Mocha 12 is an ES module, so `require()` returns its namespace object
Mocha = Mocha.default || Mocha;

var mocha = new Mocha({
  ui: '@bonniernews/mocha-cakes-2'
});

Or set it after you've constructed Mocha:

var mocha = new Mocha();
mocha.ui('@bonniernews/mocha-cakes-2')

Test structure

require('chai').should();

Feature('Some feature', () => {

  Scenario('Some Scenario', () => {

    let number = 2;

    Given('a number', () => {
      number.should.exist;
    });
    And('that number is 2', () => {
      number.should.equal(2);
    });

    When('adding 40', () => {
      number += 40;
    });

    Then('the number should be 42', () => {
      number.should.equal(42);
    });
  });
});

The result will look something like this:

The common Mocha functions (describe, it, before, after, etc) are also available and can be used together with Mocha Cakes.

Upgrading from mocha-cakes-2 3.x

Replace the mocha-cakes-2 dependency with @bonniernews/mocha-cakes-2 and change the ui option to @bonniernews/mocha-cakes-2. The old mocha-cakes-2 ui name still works if the package is loaded with --require @bonniernews/mocha-cakes-2.

Upgrading from version 1.x

Replace the require('mocha-cakes-2') statement(s) with the --ui @bonniernews/mocha-cakes-2 option as described above.

TypeScript

The TypeScript definitions are bundled together with @bonniernews/mocha-cakes-2. To use mocha directly with TypeScript you need types for mocha and ts-node.

npm install --save-dev typescript ts-node @types/mocha

You should have a tsconfig.json in the root of your project like so

{
  "compilerOptions": {
    "module": "commonjs",
    "moduleResolution": "node"
  }
}

Now you can run it like so:

mocha -r ts-node/register --ui @bonniernews/mocha-cakes-2  ...

Your tests should look like this:

import '@bonniernews/mocha-cakes-2';

Feature('Some feature', () => {

  Scenario('Some Scenario', () => {

    let number = 2;

    Given('a number', () => {

    });
    And('that number is 2', () => {

    });

    When('adding 40', () => {

    });

    Then('the number should be 42', () => {

    });
  });
});

API

The Mocha Cakes integration adds the following functions to the global scope:

  • Feature | feature
    • Scenario | scenario
      • Given | given
      • When | when
      • Then | then
      • And | and
      • But | but

.skip

Skips a test clause. Works on all test functions.

Mocha's --forbid-pending option is respected.

Feature('Some feature', () => {

  Scenario.skip('Skipped scenario', () => {
    // ...
  });

  Scenario('Ordinary', () => {
    // ...
  });
});

.only

Only run the specified test clause. Works on all test functions.

Multiple .only clauses are combined, just like Mocha's describe.only and it.only, and Mocha's --forbid-only and --grep options are respected.

Feature('Some feature', () => {

  Scenario('First scenario', () => {
    // ...
  });

  Scenario('Second scenario', () => {
    // ...
  });

  Scenario.only('Only I will run!', () => {
    // ...
  });

  // ...
});

beforeEachScenario and afterEachScenario

Executes the provided function only once for each of the scenarios under the current scope.

Feature('Some feature', () => {

  beforeEachScenario( () => {
    someSetup();
  });

  afterEachScenario( () => {
    doCleanup();
  });

  Scenario('First scenario', () => {
    // ...
  });

  Scenario('Second scenario', () => {
    // ...
  });

  // ...
});

beforeEachFeature and afterEachFeature

Executes the provided function only once for each of the features under the current scope.

beforeEachFeature( () => {
  someSetup();
});

afterEachFeature( () => {
  doCleanup();
});

Feature('Some feature', () => {
  // ...
});

Feature('Another feature', () => {
  // ...
});

// ...

Development

Run the tests with:

npm test

This runs the test suite twice, once using Mocha's CLI and once using Mocha's API (see below).

Continuous integration

The tests are run by GitHub Actions on Node.js 22, 24 and 26 with both Mocha 11 and Mocha 12.

Testing against Mocha 11

Mocha 12 is installed as a development dependency. To run the tests against Mocha 11 locally, install it without saving it to package.json:

npm install --no-save mocha@11 && npm test

Run npm install to go back to Mocha 12.

Testing the CLI and API interfaces

If you use Mocha directly to run the tests you can set the MOCHA_INTERFACE environment variable to either cli or api to choose which Mocha interface to run the tests with. The api interface runs the sample tests in the same process, so it also needs --require ./mocha-cakes.js:

MOCHA_INTERFACE=cli npx mocha test/feature/tests.js
MOCHA_INTERFACE=api npx mocha --require ./mocha-cakes.js test/feature/tests.js

MOCHA_INTERFACE will default to cli if no value is set.

When you run npm run test:cli or npm run test:api (or npm test to run them both), MOCHA_INTERFACE is set automatically to the appropriate value.

Acknowledgements

Mocha Cakes 2 is heavily influenced by quangv's mocha-cakes.

版本与发布记录

@bonniernews/mocha-cakes-2 共发布 3 个版本,首次发布于 2026/10/23;过去 12 个月发布了 3 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
1.0.0latest2026/10/2316 天前
1.0.0-alpha.0next2026/10/2316 天前
0.0.0-stage2026/10/2316 天前

关于 @bonniernews/mocha-cakes-2 的常见问题

@bonniernews/mocha-cakes-2 是什么?

A Mocha BDD integration

如何安装 @bonniernews/mocha-cakes-2?

在项目目录运行 npm install @bonniernews/mocha-cakes-2,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:mocha(>=11)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@bonniernews/mocha-cakes-2 支持 TypeScript 吗?

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

@bonniernews/mocha-cakes-2 支持 ESM 还是 CommonJS?

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

@bonniernews/mocha-cakes-2 需要什么版本的 Node.js?

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

@bonniernews/mocha-cakes-2 可以商用吗?

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

@bonniernews/mocha-cakes-2 的下载量有多少?

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

@bonniernews/mocha-cakes-2 还在维护吗?

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