All files / lib MultiReporters.js

96.39% Statements 80/83
77.78% Branches 14/18
100% Functions 10/10
96.39% Lines 80/83
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181    1x             1x 1x 1x 1x 1x 1x     14x   14x   3x   3x   6x   6x   6x           6x           6x 1x 1x     1x   1x 1x                         6x 6x                           1x   1x   1x 3x   3x 1x 1x     2x 5x     2x   2x 1x     1x 1x     1x 2x       1x 9x 9x 9x 9x     1x 11x 11x     11x 9x 9x   9x 9x 1x     8x     8x     1x 1x       2x     10x     1x 11x 11x   11x 11x   11x 11x     1x 1x     10x     1x 11x 11x 11x   11x 11x 11x   11x 11x   11x     1x  
/* global require, module, console */
 
var _ = {
    get: require('lodash/get'),
    camelCase: require('lodash/camelCase'),
    size: require('lodash/size'),
    after: require('lodash/after'),
    merge: require('lodash/merge')
};
var debug = require('debug')('mocha:reporters:MultiReporters');
var fs = require('fs');
var util = require('util')
var mocha = require('mocha');
var Base = mocha.reporters.Base;
var path = require('path');
 
function MultiReporters(runner, options) {
    Base.call(this, runner);
 
    if (_.get(options, 'execute', true)) {
 
        options = this.getOptions(options);
 
        this._reporters = _.get(options, 'reporterEnabled', 'tap').split(',').map(
            function processReporterEnabled(name, index) {
                debug(name, index);
 
                name = name.trim();
 
                var reporterOptions = this.getReporterOptions(options, name);
 
                //
                // Mocha Reporters
                // https://github.com/mochajs/mocha/blob/master/lib/reporters/index.js
                //
                var Reporter = _.get(mocha, ['reporters', name], null);
 
                //
                // External Reporters.
                // Try to load reporters from process.cwd() and node_modules.
                //
                if (Reporter === null) {
                    try {
                        Reporter = require(name);
                    }
                    catch (err) {
                        Eif (err.message.indexOf('Cannot find module') !== -1) {
                            // Try to load reporters from a path (absolute or relative)
                            try {
                                Reporter = require(path.resolve(process.cwd(), name));
                            }
                            catch (_err) {
                                _err.message.indexOf('Cannot find module') !== -1 ? console.warn('"' + name + '" reporter not found')
                                    : console.warn('"' + name + '" reporter blew up with error:\n' + _err.stack);
                            }
                        }
                        else {
                            console.warn('"' + name + '" reporter blew up with error:\n' + err.stack);
                        }
                    }
                }
 
                Eif (Reporter !== null) {
                    return new Reporter(
                        runner, {
                            reporterOptions: reporterOptions
                        }
                    );
                }
                else {
                    console.error('Reporter does not found!', name);
                }
            }.bind(this)
        );
    }
}
 
util.inherits(MultiReporters, Base);
 
MultiReporters.CONFIG_FILE = '../config.json';
 
MultiReporters.prototype.done = function (failures, fn) {
    var numberOfReporters = _.size(this._reporters);
 
    if (numberOfReporters === 0) {
        console.error('Unable to invoke fn(failures) - no reporters were registered');
        return;
    }
 
    var reportersWithDoneHandler = this._reporters.filter(function (reporter) {
        return typeof reporter.done === 'function';
    });
 
    var numberOfReportersWithDoneHandler = _.size(reportersWithDoneHandler);
 
    if (numberOfReportersWithDoneHandler === 0) {
        return fn(failures);
    }
 
    var done = _.after(numberOfReportersWithDoneHandler, function() {
        fn(failures);
    });
 
    reportersWithDoneHandler.forEach(function(reporter) {
        reporter.done(failures, done);
    });
};
 
MultiReporters.prototype.getOptions = function (options) {
    debug('options', options);
    var resultantOptions = _.merge({}, this.getDefaultOptions(), this.getCustomOptions(options));
    debug('options file (resultant)', resultantOptions);
    return resultantOptions;
};
 
MultiReporters.prototype.getCustomOptions = function (options) {
    var customOptionsFile = _.get(options, 'reporterOptions.configFile');
    debug('options file (custom)', customOptionsFile);
 
    var customOptions;
    if (customOptionsFile) {
        customOptionsFile = path.resolve(customOptionsFile);
        debug('options file (custom)', customOptionsFile);
 
        try {
          if ('.js' === path.extname(customOptionsFile)) {
              customOptions = require(customOptionsFile);
          }
          else {
              customOptions = JSON.parse(fs.readFileSync(customOptionsFile).toString());
          }
 
          debug('options (custom)', customOptions);
        }
        catch (e) {
            console.error(e);
            throw e;
        }
    }
    else {
        customOptions = _.get(options, "reporterOptions");
    }
 
    return customOptions;
};
 
MultiReporters.prototype.getDefaultOptions = function () {
    var defaultOptionsFile = require.resolve(MultiReporters.CONFIG_FILE);
    debug('options file (default)', defaultOptionsFile);
 
    var defaultOptions = fs.readFileSync(defaultOptionsFile).toString();
    debug('options (default)', defaultOptions);
 
    try {
        defaultOptions = JSON.parse(defaultOptions);
    }
    catch (e) {
        console.error(e);
        throw e;
    }
 
    return defaultOptions;
};
 
MultiReporters.prototype.getReporterOptions = function (options, name) {
    var _name = name;
    var commonReporterOptions = _.get(options, 'reporterOptions', {});
    debug('reporter options (common)', _name, commonReporterOptions);
 
    name = [_.camelCase(name), 'ReporterOptions'].join('');
    var customReporterOptions = _.get(options, [name], {});
    debug('reporter options (custom)', _name, customReporterOptions);
 
    var resultantReporterOptions = _.merge({}, commonReporterOptions, customReporterOptions);
    debug('reporter options (resultant)', _name, resultantReporterOptions);
 
    return resultantReporterOptions;
};
 
module.exports = MultiReporters;