jasmine-allure-reporter、jest-allure 和 mocha-allure-reporter 均为 Allure 测试报告框架的适配器,用于将不同 JavaScript 测试运行器(Jasmine、Jest、Mocha)的执行结果转换为 Allure 可识别的格式,从而生成结构化、可视化的 HTML 测试报告。这些包通过注入全局或模块化的 allure 对象,允许开发者在测试用例中添加步骤、附件、标签(如 feature、story)、严重级别等元数据,最终由 Allure CLI 工具聚合生成交互式报告。值得注意的是,jasmine-allure-reporter 已被官方标记为废弃,不再推荐用于新项目。
在前端测试体系中,Allure 是一个功能强大的可视化报告工具,能将原始测试结果转化为结构清晰、交互丰富的 HTML 报告。然而,不同测试框架(如 Jasmine、Jest、Mocha)需要各自的适配器才能生成 Allure 兼容的数据。本文将深入比较 jasmine-allure-reporter、jest-allure 和 mocha-allure-reporter 三款主流集成包,帮助你在真实项目中做出合理选型。
首先必须明确:jasmine-allure-reporter 已被官方标记为废弃(deprecated)。根据其 npm 页面 的说明,该包不再维护,推荐用户迁移到 allure-js-commons 或其他现代替代方案。因此,新项目绝不应选用此包。
相比之下,jest-allure 和 mocha-allure-reporter 均处于活跃维护状态,并持续适配 Allure 的最新特性。
这三款包分别深度绑定于各自的测试运行器,不可互换:
jasmine-allure-reporter 仅适用于基于 Jasmine 的测试环境(如 Karma + Jasmine)jest-allure 专为 Jest 设计,依赖 Jest 的自定义环境和 Reporter APImocha-allure-reporter 仅用于 Mocha 测试套件,通过 Mocha 的 reporter 配置项启用这意味着你的选择首先取决于项目当前使用的测试框架。
jasmine-allure-reporter(已废弃)通过 Jasmine 的全局 reporter 注册机制初始化,并暴露 allure 全局对象用于添加元数据:
// karma.conf.js
config.set({
frameworks: ['jasmine'],
plugins: ['karma-jasmine', 'jasmine-allure-reporter'],
reporters: ['allure']
});
// spec file
allure.createStep('登录步骤', () => {
// 执行操作
});
由于已废弃,不建议在任何新项目中使用。
jest-allure通过 Jest 的 setupFilesAfterEnv 和自定义环境实现。需手动导入 allure 实例进行操作:
// jest.config.js
module.exports = {
setupFilesAfterEnv: ['jest-allure/dist/setup'],
testEnvironment: 'jest-allure/dist/AllureEnvironment'
};
// test file
import { allure } from 'jest-allure';
test('should login successfully', () => {
allure.feature('Authentication');
allure.step('输入用户名和密码', () => {
// 登录逻辑
});
});
注意:jest-allure 要求显式导入 allure 对象,无法使用全局变量。
mocha-allure-reporter通过 Mocha 的命令行参数或配置文件指定 reporter,并自动注入 allure 全局对象:
// mocha.opts 或 package.json
{
"mocha": {
"reporter": "mocha-allure-reporter"
}
}
// test file
it('should display user profile', function() {
allure.feature('User Profile');
allure.addAttachment('screenshot', screenshotBuffer, 'image/png');
});
Mocha 用户可直接使用 allure 全局变量,无需额外导入。
三者均支持 Allure 核心功能,如用例标题、描述、步骤、附件、标签(feature、story、severity)等,但 API 形式略有差异:
// jest-allure
import { allure } from 'jest-allure';
allure.feature('Payment');
allure.story('Credit Card Flow');
// mocha-allure-reporter
allure.feature('Payment');
allure.story('Credit Card Flow');
// jasmine-allure-reporter (废弃)
allure.feature('Payment');
allure.story('Credit Card Flow');
// jest-allure
allure.step('点击支付按钮', () => {
page.click('#pay');
});
// mocha-allure-reporter
allure.createStep('点击支付按钮', () => {
page.click('#pay');
})();
// jasmine-allure-reporter
allure.createStep('点击支付按钮', () => {
page.click('#pay');
})();
可见,jest-allure 使用更简洁的 step() 方法,而 Mocha 和 Jasmine 方案需调用 createStep() 并立即执行返回函数。
所有方案最终都生成 allure-results 目录下的 .json 文件,需配合 allure-commandline 工具生成 HTML 报告:
# 通用流程
npx allure generate ./allure-results --clean -o ./allure-report
npx allure open ./allure-report
区别在于结果文件的生成位置和命名规则,但对最终报告无实质影响。
jest-alluremocha-allure-reporterjasmine-allure-reporter,因其已废弃@shelex/cypress-allure-plugin(若使用 Cypress)jest-allure 提供 TypeScript 类型支持,开发体验更佳mocha-allure-reporter 社区示例丰富,适合快速上手| 特性 | jasmine-allure-reporter | jest-allure | mocha-allure-reporter |
|---|---|---|---|
| 维护状态 | ❌ 已废弃 | ✅ 活跃维护 | ✅ 活跃维护 |
| 适用框架 | Jasmine | Jest | Mocha |
| API 风格 | 全局 allure 对象 | 显式导入 allure | 全局 allure 对象 |
| 步骤定义 | createStep()() | step() | createStep()() |
| 新项目推荐度 | ⛔ 禁止使用 | ✅✅✅ | ✅✅✅ |
jest-allure,配置简单,类型安全,社区支持良好。mocha-allure-reporter,无需额外导入,与 Mocha 生命周期无缝集成。jasmine-allure-reporter,即使现有项目在用,也应制定迁移计划。Allure 的价值在于提升测试结果的可读性与可追溯性,而选择正确的集成包是发挥其威力的第一步。根据你的测试框架现状,做出务实且面向未来的选择。
如果你的项目使用 Jest 作为测试运行器(例如 React 项目),应选择 jest-allure。它通过 Jest 的自定义环境和 setup 文件集成,提供类型安全的 API,支持显式导入 allure 对象来添加步骤、标签和附件,配置清晰且与现代前端工具链兼容良好。
不要在任何新项目中使用 jasmine-allure-reporter,因为它已被官方明确标记为废弃(deprecated)。即使在维护旧有 Jasmine 项目时,也应优先考虑迁移到 Jest 或 Mocha 生态,并采用对应的 Allure 集成方案,以确保长期可维护性和功能支持。
当你的测试套件基于 Mocha(常见于 Node.js 后端测试、Cypress E2E 测试或使用 Chai 断言库的项目)时,应选用 mocha-allure-reporter。它通过 Mocha 的 reporter 机制自动注入全局 allure 对象,无需额外导入,使用 allure.feature() 或 allure.createStep() 即可标注测试元数据,集成简单且社区示例丰富。

Allure Framework is a flexible lightweight multi-language test report tool that not only shows a very concise representation of what have been tested in a neat web report form, but allows everyone participating in the development process to extract maximum of useful information from everyday execution of tests.
yarn add -D jest-allure
or
npm install --save-dev jest-allure
Then add jest-allure/dist/setup to setupFilesAfterEnv section of your config.
setupFilesAfterEnv: ["jest-allure/dist/setup"]
reporters: ["default", "jest-allure"],
Run your tests and enjoy 🥤🚀
You need to install the CLI in order to obtain a report.
To see a report in browser, run in console
allure serve
If you want to generate html version, run in console
allure generate
You can add description, screenshots, steps, severity and lots of other fancy stuff to your reports.
Global variable reporter available in your tests with such methods:
description(description: string): this;
severity(severity: Severity): this;
epic(epic: string): this;
feature(feature: string): this;
story(story: string): this;
startStep(name: string): this;
endStep(status?: Status): this;
addArgument(name: string): this;
addEnvironment(name: string, value: string): this;
addAttachment(name: string, buffer: any, type: string): this;
addLabel(name: string, value: string): this;
addParameter(paramName: string, name: string, value: string): this;
Example
import { Severity } from "jest-allure/dist/Reporter";
import { Feature } from "somwhere in your project";
describe("Fancy test", () => {
...
it("Test your amazing feature", async () => {
reporter
.description("Feature should work cool")
.severity(Severity.Critical)
.feature(Feature.Betting)
.story("BOND-007");
reporter.startStep("Check it's fancy");
// expect that it's fancy
reporter.endStep();
reporter.startStep("Check it's cool");
// expect that it's cool
reporter.endStep();
const screenshotBuffer = await page.screenshot();
reporter.addAttachment("Screenshot", screenshotBuffer, "image/png");
});
...
}
);
jest-allure reporter dynamically configure "setupTestFrameworkScriptFile" option in Jest configuration.
If you have your own setupTestFrameworkScriptFile file, you need to manually register allure reporter, for it you need to import jest-allure/dist/setup.
import "jest-allure/dist/setup";
In case if you have jest version > 24 just add jest-allure/dist/setup to setupFilesAfterEnv section of your config.
Denis Artyuhovich | Dmitry Bogomya |
|---|