@fluid-internal/mocha-test-setup

中文文档
3.12.0发布于 2026/11/11MITESM + CJS近 30 天下载 2716

Utilities for Fluid tests

原始使用文档
文档目录16 节
  1. Running tests
  2. Environment Variables
  3. MOCHA_SPEC
  4. FLUID_TEST_PERF_MODE
  5. FLUID_TEST_TIMEOUT
  6. FLUID_TEST_FORBID_ONLY
  7. FLUID_TEST_MODULE_SYSTEM
  8. FLUID_LOGGER_PROPS
  9. FLUID_TEST_LOGGER_PKG_SPECIFIER
  10. Base mocharc.cjs configuration
  11. Special behavior
  12. Suppress console output by default
  13. Telemetry events
  14. Contribution Guidelines
  15. Help
  16. Trademark

@fluid-internal/mocha-test-setup

IMPORTANT: Fluid Framework uses this package as an implementation detail. Do not use it as a public dependency. We do not guarantee the stability of this package or its APIs.

This package has a few main purposes:

  • Expose/generate a default .mocharc.cjs configuration for running mocha tests, which other packages can extend.
  • Add mocha beforeAll, beforeEach and afterEach root hook plugins to add some special behavior when we run tests.

Running tests

For packages using this configuration, build the package and its tests, then run pnpm exec mocha from the package directory.

Environment Variables

Set environment variables before running Mocha. For cross-platform scripts, use cross-env, for example pnpm exec cross-env MOCHA_SPEC=lib/test/example.spec.js mocha.

MOCHA_SPEC

Sets the compiled test file, directory, or glob to load, relative to the package directory. It replaces the default selection of lib/test (or dist/test with FLUID_TEST_MODULE_SYSTEM=CJS). Unlike MOCHA_SPEC, positional file arguments and --spec add to the configured selection because of Mocha's configuration merging. Name filtering alone still loads all configured test files; use MOCHA_SPEC to avoid loading unrelated files.

FLUID_TEST_PERF_MODE

Set to 1 to measure benchmarks instead of running them as correctness tests. Leave unset for correctness testing; do not use 0 to disable it.

This selects the benchmark reporter, sets a 1,000-second timeout per test, and writes results to ./benchmarkOutput.json. Override the timeout with FLUID_TEST_TIMEOUT and the report path with --reporterOptions reportFile=<path>.

The default @Benchmark filter is omitted when you supply --fgrep <text> or --grep <pattern> (use these long options with separate arguments). Your filter replaces, rather than intersects with, the default. See benchmark filtering for tags and combining constraints.

FLUID_TEST_TIMEOUT

Overrides Mocha's per-test timeout in milliseconds, including the performance-mode default.

FLUID_TEST_FORBID_ONLY

When present, enables Mocha's forbid-only option to reject exclusive tests.

FLUID_TEST_MODULE_SYSTEM

When set to "CJS", current run is setup to be for CommonJS coverage:

  • "CJS" is noted as a variant and shown as prefix to test case names.
  • The JUnit test-results file will be prefixed with "CJS-".
  • Default spec is changed from lib/test to dist/test.

FLUID_LOGGER_PROPS

Sets a JSON object serialized into the testEnvProps telemetry field. Its hostName property also overrides the event's hostName:

FLUID_LOGGER_PROPS='{ "hostName": "Benchmark" }'

FLUID_TEST_LOGGER_PKG_SPECIFIER

Specifies a module exporting createTestLogger, which returns an ITelemetryBufferedLogger. The setup calls this factory, wraps the logger with test context, and installs the global getTestLogger function. Use this variable rather than --require to configure a custom logger. Pipelines use it to send telemetry to internal engineering systems.

Base mocharc.cjs configuration

To leverage the base mocha configuration exposed by this package, first add it as a devDependency to your package.json (the rest of the file has been omitted):

{
    "devDependencies": {
        "@fluid-internal/mocha-test-setup": "version-that-matches-the-rest-of-the-release-group"
    }
}

Then put this in a .mocharc.cjs file at the root of your package:

"use strict";

const getFluidTestMochaConfig = require("@fluid-internal/mocha-test-setup/mocharc-common");

const config = getFluidTestMochaConfig(__dirname);
module.exports = config;

If you need extra configuration, make changes to the config object before assigning it to module.exports.

Mocha will use this file by default if no path to a configuration file is provided explicitly.

The following excerpt shows some of the settings generated by getFluidTestMochaConfig():

{
  "recursive": true,
  "require": ["@fluid-internal/mocha-test-setup", "source-map-support/register"], // can be extended by providing additionalRequiredModules to getFluidTestMochaConfig()
  "unhandled-rejections": "strict",
  "node-option": ["conditions=allow-ff-test-exports", "expose-gc"],
  "ignore": [ "**/*.tool.{js,cjs,mjs}" ], // Ignore "tools" which are scripts intended to be run, not part of the test suite.
  "spec": "lib/test" // Defaults to "lib/test" where esm tests typically are. Can be overridden via MOCHA_SPEC.
};

It is recommended that the source for tests files use a file name ending in ".spec.ts", but this is not enforced: no file named based filtering of test files is done by default, except for the ignoring of "**/*.tool.{js,cjs,mjs}" noted above.

Note: some packages do explicitly filter test files to *.spec.js: in such packages test files with other names will be silently ignored.

Special behavior

Suppress console output by default

console.log(), console.warn() and console.error() are suppressed during tests unless FLUID_TEST_VERBOSE is set, for example FLUID_TEST_VERBOSE=1.

Telemetry events

A couple of telemetry events fluid:telemetry:Test_start and fluid:telemetry:Test_end will be generated for each test. The Test_end event includes fields with the test's state (passing/failed), its duration, if it timed out. If a FLUID_TEST_VARIANT environment variable exists, the testVariant field in the events will have its value.

Contribution Guidelines

You can contribute to Fluid Framework in these ways:

For detailed instructions, read the repo documentation.

This project follows the Microsoft Open Source Code of Conduct. For more information, read the Code of Conduct frequently asked questions. For questions or comments, contact [email protected].

This project may contain Microsoft trademarks or logos for Microsoft projects, products, or services. Use of these trademarks or logos must follow Microsoft’s Trademark & Brand Guidelines. Use of Microsoft trademarks or logos in modified versions of this project must not cause confusion or imply Microsoft sponsorship.

Help

Read the Fluid Framework documentation for information about Fluid Framework concepts and APIs.

To request information that the documentation does not contain, create an issue.

Trademark

This project may contain Microsoft trademarks or logos for Microsoft projects, products, or services.

Use of these trademarks or logos must follow Microsoft's Trademark & Brand Guidelines.

Use of Microsoft trademarks or logos in modified versions of this project must not cause confusion or imply Microsoft sponsorship.

版本与发布记录

@fluid-internal/mocha-test-setup 共发布 198 个版本,首次发布于 2024/03/27;过去 12 个月发布了 48 个版本。

版本发布日期距今
3.12.0latest2026/11/115 天前
3.11.02026/11/0411 天前
3.10.02026/11/0412 天前
3.7.02026/10/2719 天前
3.6.02026/10/2026 天前
3.5.02026/10/131 个月前

其他发布通道:dev → 2.116.0-416006,rc → 2.118.1。

关于 @fluid-internal/mocha-test-setup 的常见问题

@fluid-internal/mocha-test-setup 是什么?

Utilities for Fluid tests

如何安装 @fluid-internal/mocha-test-setup?

在项目目录运行 npm install @fluid-internal/mocha-test-setup,或使用 pnpm add / yarn add / bun add。

@fluid-internal/mocha-test-setup 支持 TypeScript 吗?

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

@fluid-internal/mocha-test-setup 支持 ESM 还是 CommonJS?

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

@fluid-internal/mocha-test-setup 可以商用吗?

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

@fluid-internal/mocha-test-setup 的下载量有多少?

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

@fluid-internal/mocha-test-setup 还在维护吗?

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