如何使用mocha生成多个报告?

guy*_*abi 13 mocha.js node.js gruntjs

我想要以下报告:

  • 覆盖
  • 规范
  • 的xUnit

所有都是从我的咕噜声中执行一次mocha执行

目前 - 我必须运行3次测试,每次都生成不同的报告(!).

所以我使用grunt-mocha-test2配置,只有记者是不同的(一旦xunit文件和一次规格).

然后我grunt-mocha-istanbul再次运行测试,并生成覆盖率报告.

我试过用

{ 
   options: {
        reporters : ['xunit-file', 'spec']
   }
}
Run Code Online (Sandbox Code Playgroud)

grunt-mocha-test至少把它降到2,但是,这并不正常工作.

阅读grunt-mocha-istanbul文档,我似乎无法找到任何有关reporter配置的信息.

我该如何解决这个问题?

Gyö*_*ssy 10

也许这可以帮助:https: //github.com/glenjamin/mocha-multi

Mocha尚未支持AFAIK,但它正在进行中:https: //github.com/mochajs/mocha/pull/1360

希望这可以帮助,

捷尔吉

  • 关于这个的最新信息似乎现在在这里:https://github.com/mochajs/mocha/pull/2184 (2认同)

Say*_*Pal 7

我最近遇到了同样的问题,在查看了 SO 和 GH 问题后没有发现任何问题。官方支持多名记者的话题似乎一再被推迟。

话虽如此,假设您想要组合的记者已经存在,那么拥有自定义解决方案非常容易。我所做的是创建一个小而幼稚的自定义报告器,并在.mocharc.js配置中使用该报告器。

// junit-spec-reporter.js
const mocha = require("mocha");
const JUnit = require("mocha-junit-reporter");
const Spec = mocha.reporters.Spec;
const Base = mocha.reporters.Base;

function JunitSpecReporter(runner, options) {
    Base.call(this, runner, options);
    this._junitReporter = new JUnit(runner, options);
    this._specReporter = new Spec(runner, options);
    return this;
}
JunitSpecReporter.prototype.__proto__ = Base.prototype;

module.exports = JunitSpecReporter;
Run Code Online (Sandbox Code Playgroud)
// .mocharc.js
module.exports = {
    reporter: './junit-spec-reporter.js',
    reporterOptions: {
        mochaFile: './tests-results/results.xml'
    }
};
Run Code Online (Sandbox Code Playgroud)

上面的示例展示了如何使用spec和junitreporter。

有关自定义报告器的更多信息:https ://mochajs.org/api/tutorial-custom-reporter.html

请注意,这只是一个概念证明,可以使用更通用的方法(和 TypeScript)使其变得更漂亮、更健壮。


更新 14.9.2021

我为此创建了一个实用程序包: https: //www.npmjs.com/package/@netatwork/mocha-utils