Skip to content

armgit / mocha

Public Imported from GitHub
0

☕️ Classic, reliable, trusted test framework for Node.js and the browser

cleaning up xunit reporter

TJ Holowaychukb11cebd20724
diff --git a/lib/reporters/html.js b/lib/reporters/html.js
index 046b194c..1d937928 100644
--- a/lib/reporters/html.js
+++ b/lib/reporters/html.js
@@ -125,6 +125,10 @@ function HTML(runner) {
});
}
+/**
+ * Display error `msg`.
+ */
+
function error(msg) {
$('<div id="error">' + msg + '</div>').appendTo('body');
}
diff --git a/lib/reporters/xunit.js b/lib/reporters/xunit.js
index de850711..eb2abeb1 100644
--- a/lib/reporters/xunit.js
+++ b/lib/reporters/xunit.js
@@ -1,5 +1,11 @@
-var Base = require('./base');
-var utils = require('../utils');
+
+/**
+ * Module dependencies.
+ */
+
+var Base = require('./base')
+ , utils = require('../utils')
+ , escape = utils.escape;
/**
* Expose `XUnit`.
@@ -7,11 +13,18 @@ var utils = require('../utils');
exports = module.exports = XUnit;
+/**
+ * Initialize a new `XUnit` reporter.
+ *
+ * @param {Runner} runner
+ * @api public
+ */
+
function XUnit(runner) {
Base.call(this, runner);
- var stats = this.stats;
- var tests = [];
- var self = this;
+ var stats = this.stats
+ , tests = []
+ , self = this;
runner.on('test end', function(test) {
tests.push(test);
@@ -27,52 +40,46 @@ function XUnit(runner) {
time: stats.duration / 1000.0
}, false));
- var _i, _len;
- for (_i = 0; _len = tests.length, _i < _len; _i++) {
- processTest(tests[_i]);
- }
-
+
+ tests.forEach(test);
console.error(end('testsuite'));
});
}
-function processTest(test) {
- var attributes = {
- classname: test.fullTitle(),
- name: test.title,
- time: test.duration / 1000.0
+/**
+ * Inherit from `Base.prototype`.
+ */
+
+XUnit.prototype.__proto__ = Base.prototype;
+
+/**
+ * Output tag for the given `test.`
+ */
+
+function test(test) {
+ var attrs = {
+ classname: test.fullTitle()
+ , name: test.title
+ , time: test.duration / 1000.0
};
if (test.failed) {
- console.error( tag('testcase', attributes, false, failedTestTag(test)) );
+ var err = test.err;
+ attrs.message = escape(err.message);
+ console.error(tag('testcase', attrs, false, tag('failure', attrs, false, cdata(err.stack))));
} else if (test.pending) {
- console.error( tag('testcase', attributes, false, pendingTestTag(test)) );
+ console.error(tag('testcase', attrs, false, tag('skipped', {}, true)));
} else {
- console.error( tag('testcase', attributes, true) );
+ console.error(tag('testcase', attrs, true) );
}
}
-function pendingTestTag(test) {
- return tag('skipped', { message: utils.escape(test.err.stack || test.err) }, true);
-}
-
-function failedTestTag(test) {
- var str = test.err;
-
- // <=IE7 stringifies to [Object Error]. Since it can be overloaded, we
- // check for the result of the stringifying.
- if ('[object Error]' == str) str = test.err.message;
-
- var attributes = { message: utils.escape(str) };
- return tag('failure', attributes, false, cdata(test.err.stack));
-}
-
-function tag(name, attribs, single, content) {
+function tag(name, attrs, single, content) {
var tag;
var end = single ? ' />' : '>'
var strAttr = [];
- for (var attr in attribs) {
- attribs.hasOwnProperty(attr) && strAttr.push(attr + '="' + utils.escape(attribs[attr]) + '"');
+ for (var attr in attrs) {
+ attrs.hasOwnProperty(attr) && strAttr.push(attr + '="' + escape(attrs[attr]) + '"');
}
tag = '<' + name + (strAttr.length? ' ' + strAttr.join(' ') : '' ) + end;
@@ -84,8 +91,10 @@ function end(name) {
return '</' + name + '>';
}
-function cdata(data) {
- return '<![CDATA[' + utils.escape(data) + ']]>';
-}
+/**
+ * Return cdata escaped CDATA `str`.
+ */
-XUnit.prototype.__proto__ = Base.prototype;
+function cdata(str) {
+ return '<![CDATA[' + escape(str) + ']]>';
+}