Sha256: 34a06035b0f37865626f8bd084895d884c04be395e483af246ad3bf099191f78

Contents?: true

Size: 1.27 KB

Versions: 3

Compression:

Stored size: 1.27 KB

Contents

/**
 * Module dependencies.
 */

var Base = require('./base')
  , cursor = Base.cursor
  , color = Base.color;

/**
 * Expose `JSON`.
 */

exports = module.exports = JSONReporter;

/**
 * Initialize a new `JSON` reporter.
 *
 * @param {Runner} runner
 * @api public
 */

function JSONReporter(runner) {
  var self = this;
  Base.call(this, runner);

  var tests = []
    , failures = []
    , passes = [];

  runner.on('test end', function(test){
    tests.push(test);
  });

  runner.on('pass', function(test){
    passes.push(test);
  });

  runner.on('fail', function(test, err){
    test.error = err;
    failures.push(test);
  });

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

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

/**
 * Return a plain-object representation of `test`
 * free of cyclic properties etc.
 *
 * @param {Object} test
 * @return {Object}
 * @api private
 */

function clean(test) {
  var obj = {
    title: test.title,
    fullTitle: test.fullTitle(),
    duration: test.duration
  };

  if (test.error) {
    obj.error = {
      message: test.error.message,
      stack: test.error.stack
    };
  }

  return obj;
}

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
stylus-source-0.31.0 vendor/node_modules/mocha/lib/reporters/json.js
stylus-source-0.30.1 vendor/node_modules/mocha/lib/reporters/json.js
stylus-source-0.30.0 vendor/node_modules/mocha/lib/reporters/json.js