cucumber-html-reporter、jest-html-reporters 和 mochawesome 是三个分别针对 Cucumber.js、Jest 和 Mocha 测试框架的 HTML 报告生成工具。它们将测试执行结果转化为结构清晰、视觉友好的网页报告,便于开发者和团队快速定位问题、评估测试覆盖率并追踪质量趋势。每个工具紧密集成其对应的测试生态,提供从基础结果展示到高级上下文嵌入(如截图、日志)的不同功能集,适用于 BDD 验收测试、单元测试和端到端测试等多样化场景。
在前端工程化体系中,测试报告不仅是 CI/CD 流水线的关键输出,更是团队协作和质量保障的重要依据。cucumber-html-reporter、jest-html-reporters 和 mochawesome 分别服务于 BDD(行为驱动开发)、单元测试和通用测试场景,它们在数据源、配置方式、报告结构和扩展能力上存在显著差异。本文将从实战角度深入剖析三者的异同,帮助你做出合理的技术选型。
这三个包的核心区别在于它们所依赖的测试框架和原始数据格式。
cucumber-html-reporter 专为 Cucumber.js 设计,依赖其生成的 JSON 报告文件。Cucumber 采用 Gherkin 语法(Given-When-Then)编写可读性极强的业务场景,因此报告天然具备业务语义。
// cucumber.js 配置示例
const { AfterAll } = require('@cucumber/cucumber');
const reporter = require('cucumber-html-reporter');
AfterAll(() => {
const options = {
theme: 'bootstrap',
jsonFile: 'reports/cucumber_report.json',
output: 'reports/cucumber_report.html',
reportSuiteAsScenarios: true,
};
reporter.generate(options);
});
jest-html-reporters 仅适用于 Jest,通过 Jest 的 testResultsProcessor 或 reporters 配置项直接消费 Jest 的内部测试结果对象,无需中间文件。
// jest.config.js
module.exports = {
reporters: [
'default',
['jest-html-reporters', {
publicPath: './reports',
filename: 'jest-report.html',
expand: true
}]
]
};
mochawesome 是 Mocha 的专属报告器,需在 Mocha 运行时通过 --reporter 参数指定,并可配合 marge(mochawesome-report-generator)生成最终 HTML。
# 命令行使用
npx mocha --reporter mochawesome --reporter-options reportDir=reports,reportFilename=report
⚠️ 注意:这三个包不能跨框架混用。例如,你无法用
mochawesome解析 Jest 的输出,也无法让jest-html-reporters处理 Cucumber 的 JSON。
Cucumber 报告以 Feature → Scenario → Step 为组织单位,每个步骤显示执行状态(passed/failed/skipped),并支持嵌入截图、日志等附件。非常适合非技术人员(如产品经理、QA)理解测试覆盖范围。
# features/login.feature
Feature: 用户登录
Scenario: 有效凭据登录
Given 我打开登录页面
When 我输入用户名 "alice" 和密码 "secret"
Then 我应被重定向到仪表盘
生成的 HTML 报告会高亮显示每个步骤,并在失败时展示错误堆栈和上下文信息。
Jest 报告聚焦于 测试套件(describe)→ 测试用例(it/test),突出显示代码覆盖率(需配合 collectCoverage)、测试耗时、失败断言详情。对开发者调试非常实用,但缺乏业务语言抽象。
// __tests__/login.test.js
describe('Login API', () => {
test('should return 200 for valid credentials', async () => {
const res = await request.post('/login').send({ user: 'alice', pass: 'secret' });
expect(res.statusCode).toBe(200);
});
});
报告会清晰列出每个测试的执行时间、预期值与实际值的差异,并支持展开查看完整堆栈。
Mochawesome 默认提供干净、现代的 UI,支持折叠/展开测试树、查看控制台日志、下载报告为独立 HTML 文件(含内联资源)。它还允许通过 addContext API 在测试中动态附加额外信息(如截图 URL、API 响应体)。
// test/login.spec.js
const addContext = require('mochawesome/addContext');
describe('Login', () => {
it('should login successfully', function () {
// 测试逻辑...
addContext(this, 'http://example.com/screenshot.png');
addContext(this, { title: 'Response', value: responseBody });
});
});
这些上下文信息会直接嵌入到对应测试用例的报告中,极大提升故障排查效率。
cucumber-html-reporter:通过 JavaScript 对象传参,支持主题(theme)、是否将套件显示为场景(reportSuiteAsScenarios)、自定义 CSS 等。jest-html-reporters:通过 Jest 配置中的 reporters 数组传入选项,支持自定义 Logo、禁用覆盖率、设置报告标题等。mochawesome:通过命令行参数或 mochawesome.json 配置文件,选项最丰富,包括是否自动打开报告、是否保存 JSON 中间文件、是否内联 CSS/JS 等。jest-stare 等替代方案。addContext API 是其最大亮点,允许在运行时动态注入任意上下文,非常适合 E2E 测试(如 Cypress、Playwright)集成。如果你的团队采用 Cucumber + WebdriverIO/Cypress 编写端到端测试,并希望向业务方展示可读性强的验收报告,cucumber-html-reporter 是唯一选择。它能将 Gherkin 场景直接转化为可视化报告,弥合技术与业务之间的鸿沟。
对于基于 Jest 的组件测试、工具函数测试或 API 集成测试,jest-html-reporters 提供开箱即用的体验。它无缝集成 Jest 生态,无需额外构建步骤,且能直接展示代码覆盖率热力图,非常适合日常开发调试和 CI 报告归档。
如果你仍在使用 Mocha + Chai(常见于较老的 Node.js 项目或特定框架如 Nightwatch),或者需要在测试中动态附加上下文(如 Selenium 截图、网络请求日志),mochawesome 凭借其强大的 addContext 能力成为首选。其生成的独立 HTML 文件也便于在无网络环境下分享。
| 维度 | cucumber-html-reporter | jest-html-reporters | mochawesome |
|---|---|---|---|
| 适用框架 | Cucumber.js | Jest | Mocha |
| 数据源 | JSON 文件 | Jest 内部结果对象 | Mocha 运行时事件 |
| 核心优势 | 业务语义清晰 | 开发者调试友好 | 动态上下文扩展 |
| 典型场景 | BDD 验收测试 | 单元/集成测试 | E2E / 复杂 Mocha 套件 |
| 配置方式 | JS 对象 | Jest 配置 | CLI 参数 / JSON 文件 |
jest-html-reporters 和 cucumber-html-reporter,分别生成不同维度的报告。选择哪个报告器,本质上取决于你的测试策略和团队协作模式 —— 是面向业务的验收测试,还是面向代码的单元验证,或是面向流程的端到端覆盖。明确这一点,答案自然浮现。
选择 cucumber-html-reporter 如果你使用 Cucumber.js 编写基于 Gherkin 语法(Given-When-Then)的行为驱动开发(BDD)测试,并需要生成面向业务人员可读的验收报告。它能将 Feature 和 Scenario 直接映射为 HTML 报告结构,适合需要向非技术人员展示测试覆盖范围的场景,尤其在端到端验收测试中价值突出。
选择 jest-html-reporters 如果你的项目基于 Jest 进行单元测试、集成测试或组件测试,并希望获得开箱即用的开发者友好型报告。它无缝集成 Jest 配置,能直接展示测试结果、代码覆盖率和失败断言详情,非常适合日常开发调试和 CI/CD 流水线中的质量门禁。
选择 mochawesome 如果你使用 Mocha 作为测试运行器(常见于较老的 Node.js 项目或特定 E2E 框架),并需要在测试报告中动态嵌入上下文信息(如截图、API 响应、日志)。其 addContext API 和独立 HTML 文件输出特性,使其在复杂端到端测试场景中具有独特优势。
Generate Cucumber HTML reports with pie charts
Available HTML themes:
['bootstrap', 'hierarchy', 'foundation', 'simple']
Provide Cucumber JSON report file created from your framework and this module will create pretty HTML reports. Choose your best suitable HTML theme and dashboard on your CI with available HTML reporter plugins.

npm install cucumber-html-reporter --save-dev
Notes:
[email protected] for cucumber version < Cucumber@8[email protected] for cucumber version < Cucumber@3[email protected] for cucumber version < Cucumber@2[email protected] for node version <0.12Let's get you started:
bootstrap theme:
var reporter = require('cucumber-html-reporter');
var options = {
theme: 'bootstrap',
jsonFile: 'test/report/cucumber_report.json',
output: 'test/report/cucumber_report.html',
reportSuiteAsScenarios: true,
scenarioTimestamp: true,
launchReport: true,
metadata: {
"App Version":"0.3.2",
"Test Environment": "STAGING",
"Browser": "Chrome 54.0.2840.98",
"Platform": "Windows 10",
"Parallel": "Scenarios",
"Executed": "Remote"
},
failedSummaryReport: true,
};
reporter.generate(options);
//more info on `metadata` is available in `options` section below.
//to generate consodilated report from multi-cucumber JSON files, please use `jsonDir` option instead of `jsonFile`. More info is available in `options` section below.
Please look at the Options section below for more options
node index.js
This module converts Cucumber's JSON format to HTML reports.
The code has to be separated from CucumberJS execution (after it).
In order to generate JSON formats, run the Cucumber to create the JSON format and pass the file name to the formatter as shown below,
$ cucumberjs test/features/ -f json:test/report/cucumber_report.json
Multiple formatter are also supported,
$ cucumberjs test/features/ -f summary -f json:test/report/cucumber_report.json
Are you using cucumber with other frameworks or running cucumber-parallel? Pass relative path of JSON file to the
optionsas shown here
themeAvailable: ['bootstrap', 'hierarchy', 'foundation', 'simple']
Type: String
Select the Theme for HTML report.
N.B: Hierarchy theme is best suitable if your features are organized under features-folder hierarchy. Each folder will be rendered as a HTML Tab. It supports up to 3-level of nested folder hierarchy structure.
jsonFileType: String
Provide path of the Cucumber JSON format file
jsonDirType: String
If you have more than one cucumber JSON files, provide the path of JSON directory. This module will create consolidated report of all Cucumber JSON files.
e.g. jsonDir: 'test/reports' //where reports directory contains valid *.json files
N.B.: jsonFile takes precedence over jsonDir. We recommend to use either jsonFile or jsonDir option.
outputType: String
Provide HTML output file path and name
reportSuiteAsScenariosType: Boolean
Supported in the Bootstrap theme.
true: Reports total number of passed/failed scenarios as HEADER.
false: Reports total number of passed/failed features as HEADER.
launchReportType: Boolean
Automatically launch HTML report at the end of test suite
true: Launch HTML report in the default browser
false: Do not launch HTML report at the end of test suite
ignoreBadJsonFileType: Boolean
Report any bad json files found during merging json files from directory option.
true: ignore any bad json files found and continue with remaining files to merge.
false: Default option. Fail report generation if any bad files found during merge.
nameType: String (optional)
Custom project name. If not passed, module reads the name from projects package.json which is preferable.
brandTitleType: String (optional)
Brand Title is the brand of your report, e.g. Smoke Tests Report, Acceptance Test Report etc as per your need. If not passed, it will be displayed as "Cucumberjs Report"
columnLayoutAvailable: [1, 2]
Type: Number
Default: 2
Select the Column Layout. One column or Two columns
1 = One Column layout (col-xx-12) 2 = Two Columns Layout (col-xx-6)
storeScreenshotsType: Boolean
Default: undefined
true: Stores the screenShots to the default directory. It creates a directory 'screenshot' if does not exists.
false or undefined : Does not store screenShots but attaches screenShots as a step-inline images to HTML report
screenshotsDirectoryType: String (optional)
Default: options.output/../screenshots
Applicable if storeScreenshots=true. Relative path for directory where screenshots should be saved. E.g. the below options should store the screenshots to the <parentDirectory>/screenshots/ where as the report would be at <parentDirectory>/report/cucumber_report.html
{
...
...
output: '/report/cucumber_report.html',
screenshotsDirectory: 'screenshots/',
storeScreenshots: true
}
noInlineScreenshotsType: Boolean
Default: undefined
true: Applicable if storeScreenshots=true. Avoids inlining screenshots, uses relative path to screenshots instead (i.e. enables lazy loading of images).
false or undefined: Keeps screenshots inlined.
scenarioTimestampType: Boolean
Default: undefined
true: Applicable if theme: 'bootstrap'. Shows the starting timestamp of each scenario within the title.
false or undefined: Does not show starting timestamp.
metadataType: JSON (optional)
Default: undefined
Print more data to your report, such as browser info, platform, app info, environments etc. Data can be passed as JSON key-value pair. Reporter will parse the JSON and will show the Key-Value under Metadata section on HTML report. Checkout the below preview HTML Report with Metadata.
Pass the Key-Value pair as per your need, as shown in below example,
metadata: {
"App Version":"0.3.2",
"Test Environment": "STAGING",
"Browser": "Chrome 54.0.2840.98",
"Platform": "Windows 10",
"Parallel": "Scenarios",
"Executed": "Remote"
}
failedSummaryReportType: Boolean
A summary report of all failed scenarios will be listed in a grid, which its scenario title, tags, failed step and exception.
true: Insert failed summary report.
false: Failed summary report will not be inserted.
Capture and Attach screenshots to the Cucumber Scenario and HTML report will render the screenshot image
for Cucumber V8
let world = this;
return driver.takeScreenshot().then((screenShot) => {
// screenShot is a base-64 encoded PNG
world.attach(screenShot, 'image/png');
});
for Cucumber V2 and V3
var world = this;
driver.takeScreenshot().then(function (buffer) {
return world.attach(buffer, 'image/png');
};
for Cucumber V1
driver.takeScreenshot().then(function (buffer) {
return scenario.attach(new Buffer(buffer, 'base64'), 'image/png');
};
Attach plain-texts/data to HTML report to help debug/review the results
scenario.attach('test data goes here');
Attach JSON to HTML report
scenario.attach(JSON.stringify(myJsonObject, undefined, 4));