jest-allure vs jasmine-allure-reporter vs mocha-allure-reporter
JavaScript 测试框架的 Allure 报告集成方案
jest-allurejasmine-allure-reportermocha-allure-reporter类似的npm包:

JavaScript 测试框架的 Allure 报告集成方案

jasmine-allure-reporter、jest-allure 和 mocha-allure-reporter 均为 Allure 测试报告框架的适配器,用于将不同 JavaScript 测试运行器(Jasmine、Jest、Mocha)的执行结果转换为 Allure 可识别的格式,从而生成结构化、可视化的 HTML 测试报告。这些包通过注入全局或模块化的 allure 对象,允许开发者在测试用例中添加步骤、附件、标签(如 feature、story)、严重级别等元数据,最终由 Allure CLI 工具聚合生成交互式报告。值得注意的是,jasmine-allure-reporter 已被官方标记为废弃,不再推荐用于新项目。

npm下载趋势

3 年

GitHub Stars 排名

统计详情

npm包名称
下载量
Stars
大小
Issues
发布时间
License
jest-allure53,20311722.8 kB35-MIT
jasmine-allure-reporter5,19734-239 年前ISC
mocha-allure-reporter04526.1 kB0-Apache-2.0

Allure 报告集成方案深度对比:Jasmine、Jest 与 Mocha

在前端测试体系中,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 API
  • mocha-allure-reporter 仅用于 Mocha 测试套件,通过 Mocha 的 reporter 配置项启用

这意味着你的选择首先取决于项目当前使用的测试框架。

📝 报告生成方式:配置与 API 差异

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 形式略有差异:

添加 Feature / Story

// 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(React 官方推荐、开箱即用、快照测试)→ 选 jest-allure
  • 若使用 Mocha + Chai(灵活、插件丰富、常用于 Node.js 或 Cypress)→ 选 mocha-allure-reporter
  • 避免使用 Jasmine 生态,因其在现代前端工程中已逐渐边缘化

场景二:遗留 Jasmine 项目需升级报告能力

  • 不要使用 jasmine-allure-reporter,因其已废弃
  • 考虑迁移到 @shelex/cypress-allure-plugin(若使用 Cypress)
  • 或重构测试框架至 Jest/Mocha,再集成对应 Allure 包

场景三:需要深度定制报告内容

  • jest-allure 提供 TypeScript 类型支持,开发体验更佳
  • mocha-allure-reporter 社区示例丰富,适合快速上手
  • 两者均支持自定义标签、参数、附件等高级功能

✅ 总结:关键差异一览

特性jasmine-allure-reporterjest-alluremocha-allure-reporter
维护状态❌ 已废弃✅ 活跃维护✅ 活跃维护
适用框架JasmineJestMocha
API 风格全局 allure 对象显式导入 allure全局 allure 对象
步骤定义createStep()()step()createStep()()
新项目推荐度⛔ 禁止使用✅✅✅✅✅✅

💡 最终建议

  • Jest 用户:直接采用 jest-allure,配置简单,类型安全,社区支持良好。
  • Mocha 用户:选用 mocha-allure-reporter,无需额外导入,与 Mocha 生命周期无缝集成。
  • 所有团队:彻底避开已废弃的 jasmine-allure-reporter,即使现有项目在用,也应制定迁移计划。

Allure 的价值在于提升测试结果的可读性与可追溯性,而选择正确的集成包是发挥其威力的第一步。根据你的测试框架现状,做出务实且面向未来的选择。

如何选择: jest-allure vs jasmine-allure-reporter vs mocha-allure-reporter

  • jest-allure:

    如果你的项目使用 Jest 作为测试运行器(例如 React 项目),应选择 jest-allure。它通过 Jest 的自定义环境和 setup 文件集成,提供类型安全的 API,支持显式导入 allure 对象来添加步骤、标签和附件,配置清晰且与现代前端工具链兼容良好。

  • jasmine-allure-reporter:

    不要在任何新项目中使用 jasmine-allure-reporter,因为它已被官方明确标记为废弃(deprecated)。即使在维护旧有 Jasmine 项目时,也应优先考虑迁移到 Jest 或 Mocha 生态,并采用对应的 Allure 集成方案,以确保长期可维护性和功能支持。

  • mocha-allure-reporter:

    当你的测试套件基于 Mocha(常见于 Node.js 后端测试、Cypress E2E 测试或使用 Chai 断言库的项目)时,应选用 mocha-allure-reporter。它通过 Mocha 的 reporter 机制自动注入全局 allure 对象,无需额外导入,使用 allure.feature() 或 allure.createStep() 即可标注测试元数据,集成简单且社区示例丰富。

jest-allure的README

Jest-Allure reporting plugin

Add more power to your tests using Jest-Allure. Easily generate nice reports at the end of the execution.

Awesome License: MIT PRs Welcome

GitHub followers GitHub stars GitHub watchers

Examples


Allure Report

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.

Installation

yarn add -D jest-allure

or

npm install --save-dev jest-allure

jest -v >24 ?

Then add jest-allure/dist/setup to setupFilesAfterEnv section of your config.

setupFilesAfterEnv: ["jest-allure/dist/setup"]

jest -v < 24 ?

add reporter to jest.config.js

reporters: ["default", "jest-allure"],

Run your tests and enjoy 🥤🚀


How to get a report

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

Advanced features

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");
        });
        
        ...
    }
);

What's next

  • Generate report from Jest results
  • Add steps support
  • Add labels support
  • Add attachments support
  • Add more examples

Additional projects

visual-unit-tests

jest-allure-image-snapshot

Warning

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.

Contributors


Denis Artyuhovich

Dmitry Bogomya