@bedrock/test

中文文档
10.0.0发布于 2026/09/03Apache-2.0ES Module近 30 天下载 2311

Bedrock test

原始使用文档
文档目录8 节
  1. Overview
  2. Requirements
  3. Mocha Testing
  4. Setup
  5. Tests
  6. Quick Test Example
  7. Profiling Tests
  8. License

@bedrock/test

Overview

Bedrock back-end testing uses Mocha while front-end testing uses Karma. To run multiple module tests, the karma tests and mocha tests can both be initialized and populated from this module. However, it's also possible to run tests on a single module at a time.

Requirements

This software requires and supports maintained recent versions of Node.js. Updates may remove support for older unmaintained platform versions. Please use dependency version lock files and testing to ensure compatibility with this software.

Mocha Testing

The tests for modules are designed to run independent of a full bedrock development environment. To run tests on a module, only the module needs to be cloned. The test environment can be built, and tests run, locally.

For more information on Mocha, see https://mochajs.org/#getting-started

Setup

This section describes the test environment. A later section will describe the actual tests.

Quick Setup Examples

To run tests for a single module, clone the module:

npm install bedrock-module

Populate required Bedrock modules in the test directory at bedrock-modules/test:

npm install

Run the tests from the same location:

npm test

Doing an npm install inside the module test directory populates Bedrock modules in the test directory based on the dependencies contained in bedrock-module/test/package.json.

How it Works

  • The test environment for a module is set up and executed within that module's test directory. A test script is set up in the package.json file to run the test suite when npm test is entered on the command line.

    "scripts": {
      "test": "node --preserve-symlinks test.js test"
    }
    

    This will run the test file test.js.

  • test.js will load the required modules, including @bedrock/test and start Bedrock.

  • index.js in the module's ./lib directory will be run and any required modules are loaded from the test/node_modules directory.

  • The module's configuration file, config.js, is loaded first, but it's important to note that the last config file will override previous configurations. For example, in our case config.test.js will override config.js since the test configuration file is loaded later.

  • bedrock.events.on('bedrock.test.configure') is a listener that will execute on bedrock.test.configure, which is set in the @bedrock/test module. This will then load test.config.js.

  • test.config.js sets up the database and creates permission roles. It also loads up all the test files contained in the mocha directory through this line:

    config.mocha.tests.push(path.join(__dirname, 'mocha'));
    

    An array is created with all of the *.js files in that test/mocha folder, and the files are executed in order. A common practice is to number these files in the order you wish the tests to execute.

  • Helper functions, mock data, and other support code can be included in other files in the test/mocha directory. For example, mock.data.js and helpers.js are common extra files in the bedrock module tests.

Tests

This section describes an actual test. Tests use mocha and chai and are found in the test/mocha directory. Some highlights to point out about the tests follow.

Quick Test Example

The following is an example of a typical test. This one tests a regular user adding a public key through the bedrock-key addPublicKey API.

The test does some data preparation at the very beginning, then clears some of the test data between each test. The general flow for many tests is similar to this one:

  1. Set up data and identities as needed to perform the test.
  2. Execute the test.
  3. Compare actual results to expected results.

Most of the work, and examples below, focuses on step 1: The set-up of the data and identities.

***NOTE: THESE EXAMPLES ARE OLD; bedrock identities have been removed ***

Inside the test, set-up preloads data from mock.data.js into the database using a helper function:

var mockData = require('./mock.data');
before(done => {
  helpers.prepareDatabase(mockData, done);
});

As is often the case, this data is set up to represent different identities with a particular set of system resource roles (permissions). Within mock.data.js, a helper function is used to create the identity:

var mock = {};
module.exports = mock;

var identities = mock.identities = {};
var userName;

userName = 'regularUser';
identities[userName] = {};
identities[userName].identity = helpers.createIdentity(userName);

System resource roles are added as part of the identity set-up. The pair is usually the permission (sysRole) and the resource or resources that the permissions apply to (generateResource):

identities[userName].identity.sysResourceRole.push({
  sysRole: 'bedrock-key.test',
  generateResource: 'id'
});

In this case, 'bedrock-key.test' is a system role that is defined in test.config.js to allow the user to access, remove, create, or edit a public key for each resource:

roles['bedrock-key.test'] = {
  id: 'bedrock-key.test',
  label: 'Key Test Role',
  comment: 'Role for Test User',
  sysPermission: [
    permissions.PUBLIC_KEY_REMOVE.id,
    permissions.PUBLIC_KEY_ACCESS.id,
    permissions.PUBLIC_KEY_CREATE.id,
    permissions.PUBLIC_KEY_EDIT.id
  ]
};

Before each test, or in-between tests, test-specific data is cleared from the database within the test:

beforeEach(function(done) {
  helpers.removeCollection('publicKey', done);
});

Identities can be set up for a group of tests to avoid repetition. In this case, an identity is set up for a regular user with the associated resource roles. Several tests will be run using this identity:

describe('authenticated as regularUser', () => {
  var mockIdentity = mockData.identities.regularUser;
  var actor;
  before(done => {
    brIdentity.get(null, mockIdentity.identity.id, (err, result) => {
      actor = result;
      done(err);
    });
  });
});

Finally, a test is executed and data checked:

it('should add a valid public key with no private key', done => {
  var samplePublicKey = {};

  samplePublicKey.publicKeyPem = mockData.goodKeyPair.publicKeyPem;
  samplePublicKey.owner = actor.id;

  async.auto({
    insert: function(callback) {
      brKey.addPublicKey(actor, samplePublicKey, callback);
    },
    test: ['insert', function(callback, results) {
      database.collections.publicKey.find({
        'publicKey.owner': actor.id
      }).toArray(function(err, result) {
        should.not.exist(err);
        should.exist(result);
        result[0].publicKey.publicKeyPem.should.equal(
          samplePublicKey.publicKeyPem);
        callback();
      });
    }],
  }, done);
});

This test uses the regular user identity to test the API. The brKey.addPublicKey function call will execute the test. The comparison of expected and actual results is done using the should mocha directives. If successful, the test will continue; If there is a failure, the testing will stop.

Profiling Tests

How to profile a bedrock test suite:

Make these local modifications to any mocha test file:

// do not include this in package.json; it will be installed by npx
import nsolid from 'nsolid';

describe.only('some suite', function() {
  before(async () => {
    // wait for nsolid to be ready
    await delay(5000);
    // or without `delay` library
    // await new Promise(r => setTimeout(r, 5000));

    // start nsolid profile and run for up to 60000ms
    nsolid.profile(60000);
  });

  after(() => {
    // stop the profile if it hasn't already timed out
    nsolid.profileEnd();
  });

  // it(...)
});

Then run:

npx nsolid-quickstart --npm test

Once the profile completes, your browser should open a page to nsolid's UI and you can login using your GitHub ID. Then look under assets to see the generated profile.

License

Apache License, Version 2.0 Copyright 2011-2024 Digital Bazaar, Inc.

Other Bedrock libraries are available under a non-commercial license for uses such as self-study, research, personal projects, or for evaluation purposes. See the Bedrock Non-Commercial License v1.0 for details.

Commercial licensing and support are available by contacting Digital Bazaar [email protected].

版本与发布记录

@bedrock/test 共发布 17 个版本,首次发布于 2022/04/01;过去 12 个月发布了 4 个版本。

版本发布日期距今
10.0.0latest2026/09/032 个月前
9.0.02026/04/077 个月前
8.3.02026/04/077 个月前
8.2.12026/03/107 个月前
8.2.02024/02/292 年前
8.0.52022/05/144 年前

关于 @bedrock/test 的常见问题

@bedrock/test 是什么?

Bedrock test

如何安装 @bedrock/test?

在项目目录运行 npm install @bedrock/test,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:@bedrock/core(^6.3.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@bedrock/test 支持 TypeScript 吗?

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

@bedrock/test 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@bedrock/test 需要什么版本的 Node.js?

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

@bedrock/test 可以商用吗?

@bedrock/test 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@bedrock/test 的下载量有多少?

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

@bedrock/test 还在维护吗?

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