@bonniernews/node-test-bdd

中文文档
0.0.4发布于 2025/04/25MITTypeScriptCommonJS零依赖近 30 天下载 2210

BDD syntax for node test

原始使用文档
文档目录8 节
  1. 🚀 Quick Start
  2. Installation
  3. Usage
  4. Register the BDD test functions globally
  5. API
  6. Scenario Hooks
  7. Migrating from Mocha
  8. Mocha vs. Node.js Test Runner Options

@bonniernews/node-test-bdd

Node Test BDD is a Gherkin/Cucumber-style testing framework for Node.js, designed to work seamlessly with the Node test runner.

It serves as a drop-in replacement for Mocha Cakes 2 for users transitioning from Mocha to Node's native test runner.

🚀 Quick Start

Install the package:

npm install --save-dev @bonniernews/node-test-bdd

Write your first test:

import assert from "node:assert";
import { Feature, Scenario, Given, When, Then } from "@bonniernews/node-test-bdd";

Feature("Basic Math", () => {
  Scenario("Incrementing a number", () => {
    let number = 1;

    Given("I have a number 1", () => {
      assert.strictEqual(number, 1);
    });

    When("I add 1", () => {
      number += 1;
    });

    Then("The number should be 2", () => {
      assert.strictEqual(number, 2);
    });
  });
});

Run the test using Node's built-in test runner:

node --test

The output should look like this:

Test output

Installation

Install as a development dependency:

npm install --save-dev @bonniernews/node-test-bdd

Usage

Node Test BDD provides Gherkin-style syntax for defining tests:

import assert from "node:assert";
import { Feature, Scenario, Given, When, Then, And } from "@bonniernews/node-test-bdd";

Feature("Example feature", () => {
  Scenario("Example scenario", () => {
    let number;
    Given("I have a number", () => {
      number = 1;
    });
    And("That number is 1", () => {
      assert.ok(number === 1);
    });
    When("Adding 1 to that number", () => {
      number += 1;
    });
    Then("The number should be 2", () => {
      assert.ok(number === 2);
    });
  });
});

Register the BDD test functions globally

To avoid importing BDD functions in every test file, you can register them globally:

Option 1: Import in your test setup file

import "@bonniernews/node-test-bdd/register-bdd";

Option 2: Import dynamically when running tests

node --test --import '@bonniernews/node-test-bdd/register-bdd'

API

Node Test BDD provides the following test functions:

  • Feature(description, fn)
    • Scenario(description, fn)
      • Given(description, fn)
      • When(description, fn)
      • Then(description, fn)
      • And(description, fn)
      • But(description, fn)

Scenario Hooks

You can use hooks to set up and clean up before and after each scenario.

Feature("Some feature", () => {
  beforeEachScenario(() => {
    someSetup();
  });

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

  Scenario("First scenario", () => {
    // Test logic...
  });

  Scenario("Second scenario", () => {
    // Test logic...
  });
});

Migrating from Mocha

If your Mocha test suite runs sequentially (without parallelism), you should consider setting the --experimental-test-isolation flag to none.

This ensures that:

  • All test files run in the same process as the test runner.
  • The --test-concurrency flag is automatically set to 1, mimicking Mocha’s default behavior.

Mocha vs. Node.js Test Runner Options

Mocha OptionEquivalent in Node.js Test Runner
--exit--test-force-exit
--grep--test-name-pattern
--reporter--test-reporter
--require--import
--timeout--test-timeout

版本与发布记录

@bonniernews/node-test-bdd 共发布 4 个版本,首次发布于 2025/03/14;过去 12 个月发布了 0 个版本。

版本发布日期距今
0.0.4latest2025/04/251.5 年前
0.0.32025/04/241.5 年前
0.0.22025/03/271.6 年前
0.0.12025/03/141.7 年前

关于 @bonniernews/node-test-bdd 的常见问题

@bonniernews/node-test-bdd 是什么?

BDD syntax for node test

如何安装 @bonniernews/node-test-bdd?

在项目目录运行 npm install @bonniernews/node-test-bdd,或使用 pnpm add / yarn add / bun add。

@bonniernews/node-test-bdd 支持 TypeScript 吗?

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

@bonniernews/node-test-bdd 支持 ESM 还是 CommonJS?

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

@bonniernews/node-test-bdd 可以商用吗?

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

@bonniernews/node-test-bdd 的下载量有多少?

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

@bonniernews/node-test-bdd 还在维护吗?

最新版本 0.0.4 发布于 2025/04/25(1.5 年前);至今共发布 4 个版本,其中过去 12 个月发布了 0 个;首次发布于 2025/03/14。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。