Code coverage report for lib/reporters/console.js

Statements: 100% (44 / 44)      Branches: 100% (16 / 16)      Functions: 100% (9 / 9)      Lines: 100% (44 / 44)      Ignored: none     

All files » lib/reporters/ » console.js
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 1241 1 1 1 1             1 1 1                 1 2 2                   1 2 2               1   3   3                   3   3 1       2 3               3 3     3 1   1 4 1           2                     1 2 1               1   1   1 1 1 1 1 1 1 1   1
var colors    = require('colors');
var events    = require('events');
var util      = require('util');
var failures  = [];
var successes = [];
 
/**
 * Segment event handler, log a new line and test file name.
 *
 * @param {String} file: test file name
 */
function _segment(file) {
  console.log();
  process.stdout.write(util.format('%s ', file));
}
 
/**
 * Success event handler, displays a green dot and register success.
 *
 * @param {Object} test: test expectation (exitcode, output)
 * @param {Object} result: command execution result (exitcode, output)
 */
function _success(test, result) {
  process.stdout.write('.'.green);
  successes.push({ test: test, result: result });
}
 
/**
 * Failure event handler, displays a red dot and register failure.
 *
 * @param {Array} check errors
 * @param {Object} test: test expectation (exitcode, output)
 * @param {Object} result: command execution result (exitcode, output)
 */
function _failure(errors, test, result) {
  process.stdout.write('.'.red);
  failures.push({ errors: errors, test: test, result: result });
}
 
/**
 * End event handler, displays test summary and optional debug message.
 *
 * @param {Boolean} debug: if true, displays result output and exit code
 */
function _end(debug) {
 
  const DEBUG_FIELDS = ['exitcode', 'output', 'stdout', 'stderr'];
 
  var summary = util.format(
    '%d test%s, %d success%s, %d failure%s',
    successes.length + failures.length,
    (successes.length + failures.length > 1) ? 's' : '',
    successes.length,
    (successes.length > 1) ? 'es' : '',
    failures.length,
    (failures.length > 1) ? 's' : ''
  );
 
  console.log('');
 
  if (failures.length === 0) {
    console.log(summary.green);
 
  } else {
 
    failures.forEach(function (failure) {
      console.error(util.format('' +
          '\n----------------' +
          '\n%s%s' +
          '\n%s',
        failure.test.description ? failure.test.description + '\n' : '',
        failure.test.file.cyan,
        failure.test.command));
 
      failure.errors.forEach(function (error) {
        console.error(error.red);
      });
 
      if (debug) {
        console.error('exec dir: %s\n'.grey, failure.test.dir);
 
        DEBUG_FIELDS.forEach(function (field) {
          if (failure.test[field]) {
            console.error('%s: %s\n'.grey, field, failure.result[field]);
          }
        });
      }
    });
 
    console.error(summary.red);
  }
 
}
 
/**
 * Dir event handler, displays directory on debug
 *
 * @param {Boolean} debug: if true, displays result output and exit code
 * @param {String} dir: directory name to display
 */
function _dir(debug, dir) {
  if (debug) {
    console.log('Using directory: %s'.grey, dir);
  }
}
 
/**
 * class Reporter
 * An event emitter handling segment, success, failure, and end events.
 */
function Reporter() {
}
util.inherits(Reporter, events.EventEmitter);
 
var reporter = new Reporter();
reporter.on('segment', _segment);
reporter.on('success', _success);
reporter.on('failure', _failure);
reporter.on('end', _end);
reporter.on('dir', _dir);
reporter.successes = successes;
reporter.failures  = failures;
 
module.exports = reporter;