首页 > 解决方案 > 如何在具有多报告器的量角器中使用 mocha 自定义报告器

问题描述

我想使用带有 mocha-json、https: //mochajs.org/#json 的自定义报告器,并将其包含在我的量角器配置文件中。我创建了一个报告文件,并通过 npm 模块安装它。我把它mochaOpts作为mocha-multi-reporter. 但是在运行测试时,我没有从记者那里得到任何结果或控制台日志。

conf.shared.ts

  var mochaJsonReporter = new Mocha({

    reporter: './node_modules/mocha-json-reporter'
  });

mochaOpts: {
        reporter: 'mocha-multi-reporters',
        reporterOptions: {
            reporterEnabled:
                'mocha-junit-reporter, mocha-json-reporter',

            mochaJunitReporterReporterOptions: {
           mochaFile: './reports/junit/junit.xml'
            },

 mochaJsonReporterOptions: {
               // what to include here?
                },

Reporter.js - 发布为 npm 包 - mocha-json-reporter

'use strict';
/**
 * @module JSON
 */
/**
 * Module dependencies.
 */

var Base = require('./base');

/**
 * Expose `JSON`.
 */

exports = module.exports = JSONReporter;

/**
 * Initialize a new `JSON` reporter.
 *
 * @public
 * @class JSON
 * @memberof Mocha.reporters
 * @extends Mocha.reporters.Base
 * @api public
 * @param {Runner} runner
 */
function JSONReporter(runner) {
  Base.call(this, runner);

  var self = this;
  var tests = [];
  var pending = [];
  var failures = [];
  var passes = [];

  runner.on('test end', function(test) {
    console.log('end ---- ', test.title);
    tests.push(test);
  });

  runner.on('pass', function(test) {
    console.log('pass ---- ', test.title);
    passes.push(test);
  });

  runner.on('fail', function(test, err) {
    console.log('fail ---- ', test.title, err.message);
    failures.push(test);
  });

  runner.on('pending', function(test) {
    console.log('pending --- ', test.title);
    pending.push(test);
  });

  runner.once('end', function() {
    var obj = {
      stats: self.stats,
      tests: tests.map(clean),
      pending: pending.map(clean),
      failures: failures.map(clean),
      passes: passes.map(clean)
    };

    runner.testResults = obj;

    process.stdout.write(JSON.stringify(obj, null, 2));
  });
}

标签: protractormocha.js

解决方案


推荐阅读