☕️ Classic, reliable, trusted test framework for Node.js and the browser
xunit reporter support for optionally writing to a file
Michael Demmer82bba0cd9c38
diff --git a/lib/reporters/xunit.js b/lib/reporters/xunit.jsindex 3506a07b..c18afddc 100644--- a/lib/reporters/xunit.js+++ b/lib/reporters/xunit.js@@ -5,6 +5,7 @@var Base = require('./base'), utils = require('../utils')+ , fs = require('fs'), escape = utils.escape;/**@@ -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);});@@ -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@@ -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@@ -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.