Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

reporter-specific options and support for the xunit reporter to output to a file (#2) #1218

Merged
merged 3 commits into from
Dec 15, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion bin/_mocha
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ program
.option('-A, --async-only', "force all tests to take a callback (async)")
.option('-C, --no-colors', 'force disabling of colors')
.option('-G, --growl', 'enable growl notification support')
.option('-O, --reporter-options <k=v,k2=v2,...>', 'reporter-specific options')
.option('-R, --reporter <name>', 'specify the reporter to use', 'spec')
.option('-S, --sort', "sort test files")
.option('-b, --bail', "bail after first test failure")
Expand Down Expand Up @@ -190,9 +191,22 @@ program.parse(process.argv);

Error.stackTraceLimit = Infinity; // TODO: config

// reporter options

var reporterOptions = {};
if (program.reporterOptions !== undefined) {
program.reporterOptions.split(",").forEach(function(opt) {
var L = opt.split("=");
if (L.length != 2) {
throw new Error("invalid reporter option '" + opt + "'");
}
reporterOptions[L[0]] = L[1];
});
}

// reporter

mocha.reporter(program.reporter);
mocha.reporter(program.reporter, reporterOptions);

// interface

Expand Down
18 changes: 14 additions & 4 deletions lib/mocha.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ function Mocha(options) {
this.suite = new exports.Suite('', new exports.Context);
this.ui(options.ui);
this.bail(options.bail);
this.reporter(options.reporter);
this.reporter(options.reporter, options.reporterOptions);
if (null != options.timeout) this.timeout(options.timeout);
this.useColors(options.useColors)
if (options.slow) this.slow(options.slow);
Expand Down Expand Up @@ -119,10 +119,10 @@ Mocha.prototype.addFile = function(file){
* Set reporter to `reporter`, defaults to "spec".
*
* @param {String|Function} reporter name or constructor
* @param {Object} reporterOptions optional options
* @api public
*/

Mocha.prototype.reporter = function(reporter){
Mocha.prototype.reporter = function(reporter, reporterOptions){
if ('function' == typeof reporter) {
this._reporter = reporter;
} else {
Expand All @@ -137,6 +137,7 @@ Mocha.prototype.reporter = function(reporter){
if (!_reporter) throw new Error('invalid reporter "' + reporter + '"');
this._reporter = _reporter;
}
this.options.reporterOptions = reporterOptions;
return this;
};

Expand Down Expand Up @@ -366,5 +367,14 @@ Mocha.prototype.run = function(fn){
if (options.growl) this._growl(runner, reporter);
exports.reporters.Base.useColors = options.useColors;
exports.reporters.Base.inlineDiffs = options.useInlineDiffs;
return runner.run(fn);

function done(failures) {
if (reporter.done) {
reporter.done(failures, fn);
} else {
fn(failures);
}
}

return runner.run(done);
};
50 changes: 41 additions & 9 deletions lib/reporters/xunit.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

var Base = require('./base')
, utils = require('../utils')
, fs = require('fs')
, escape = utils.escape;

/**
Expand All @@ -30,12 +31,19 @@ exports = module.exports = XUnit;
* @api public
*/

function XUnit(runner) {
function XUnit(runner, options) {
Base.call(this, runner);
var stats = this.stats
, tests = []
, self = this;

if (options.reporterOptions && options.reporterOptions.output) {
if (! fs.createWriteStream) {
throw new Error('file output not supported in browser');
}
self.fileStream = fs.createWriteStream(options.reporterOptions.output);
}

runner.on('pending', function(test){
tests.push(test);
});
Expand All @@ -49,7 +57,7 @@ function XUnit(runner) {
});

runner.on('end', function(){
console.log(tag('testsuite', {
self.write(tag('testsuite', {
name: 'Mocha Tests'
, tests: stats.tests
, failures: stats.failures
Expand All @@ -59,22 +67,46 @@ function XUnit(runner) {
, time: (stats.duration / 1000) || 0
}, false));

tests.forEach(test);
console.log('</testsuite>');
tests.forEach(function(t) { self.test(t); });
self.write('</testsuite>');
});
}

/**
* Override done to close the stream (if it's a file).
*/
XUnit.prototype.done = function(failures, fn) {
if (this.fileStream) {
this.fileStream.end(function() {
fn(failures);
});
} else {
fn(failures);
}
};

/**
* Inherit from `Base.prototype`.
*/

XUnit.prototype.__proto__ = Base.prototype;

/**
* Write out the given line
*/
XUnit.prototype.write = function(line) {
if (this.fileStream) {
this.fileStream.write(line + '\n');
} else {
console.log(line);
}
};

/**
* Output tag for the given `test.`
*/

function test(test) {
XUnit.prototype.test = function(test, ostream) {
var attrs = {
classname: test.parent.fullTitle()
, name: test.title
Expand All @@ -83,13 +115,13 @@ function test(test) {

if ('failed' == test.state) {
var err = test.err;
console.log(tag('testcase', attrs, false, tag('failure', {}, false, cdata(escape(err.message) + "\n" + err.stack))));
this.write(tag('testcase', attrs, false, tag('failure', {}, false, cdata(escape(err.message) + "\n" + err.stack))));
} else if (test.pending) {
console.log(tag('testcase', attrs, false, tag('skipped', {}, true)));
this.write(tag('testcase', attrs, false, tag('skipped', {}, true)));
} else {
console.log(tag('testcase', attrs, true) );
this.write(tag('testcase', attrs, true) );
}
}
};

/**
* HTML tag helper.
Expand Down