【问题标题】:How to generate multiple reports with mocha?如何使用 mocha 生成多个报告?
【发布时间】:2014-12-30 16:59:14
【问题描述】:

我想要以下报告:

  • 覆盖范围
  • 规格
  • xunit

我的 grunt 都在一个 mocha 执行中运行

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

所以我使用 grunt-mocha-test 和 2 个配置,其中只有报告器不同(一次是 xunit 文件,一次是规范)。

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

我尝试过使用

{ 
   options: {
        reporters : ['xunit-file', 'spec']
   }
}

对于grunt-mocha-test 至少将其降低到 2,但这也不起作用。

阅读grunt-mocha-istanbul 文档,我似乎找不到任何关于reporter 配置的信息。

我该如何解决这个问题?

【问题讨论】:

    标签: node.js gruntjs mocha.js


    【解决方案1】:

    也许这会有所帮助: https://github.com/glenjamin/mocha-multi

    AFAIK 这在 Mocha 中尚不支持,但正在开发中: https://github.com/mochajs/mocha/pull/1360

    希望这会有所帮助,

    吉尔吉斯

    【讨论】:

    【解决方案2】:

    我最近遇到了同样的问题,但在查看了 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;
    
    // .mocharc.js
    module.exports = {
        reporter: './junit-spec-reporter.js',
        reporterOptions: {
            mochaFile: './tests-results/results.xml'
        }
    };
    

    上面的例子展示了如何同时使用 spec 和 junit 报告器。

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

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


    14.9.2021 更新

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

    【讨论】:

      【解决方案3】:

      为了同时报告specx-unit,还有一个名为spec-xunit-file 的 NPM 包。

      咕哝着:

      grunt.initConfig({
          mochaTest: {
            test: {
              options: {
                reporter: 'spec-xunit-file',
                ...
              },
              ...
            }
          }
          ...
        });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-07-20
        • 1970-01-01
        • 2022-01-07
        • 2018-08-22
        • 2014-01-27
        • 2020-06-24
        • 1970-01-01
        相关资源
        最近更新 更多