xunit.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. 'use strict';
  2. /**
  3. * @module XUnit
  4. */
  5. /**
  6. * Module dependencies.
  7. */
  8. var Base = require('./base');
  9. var utils = require('../utils');
  10. var inherits = utils.inherits;
  11. var fs = require('fs');
  12. var escape = utils.escape;
  13. var mkdirp = require('mkdirp');
  14. var path = require('path');
  15. /**
  16. * Save timer references to avoid Sinon interfering (see GH-237).
  17. */
  18. /* eslint-disable no-unused-vars, no-native-reassign */
  19. var Date = global.Date;
  20. var setTimeout = global.setTimeout;
  21. var setInterval = global.setInterval;
  22. var clearTimeout = global.clearTimeout;
  23. var clearInterval = global.clearInterval;
  24. /* eslint-enable no-unused-vars, no-native-reassign */
  25. /**
  26. * Expose `XUnit`.
  27. */
  28. exports = module.exports = XUnit;
  29. /**
  30. * Initialize a new `XUnit` reporter.
  31. *
  32. * @public
  33. * @class
  34. * @memberof Mocha.reporters
  35. * @extends Mocha.reporters.Base
  36. * @api public
  37. * @param {Runner} runner
  38. */
  39. function XUnit(runner, options) {
  40. Base.call(this, runner);
  41. var stats = this.stats;
  42. var tests = [];
  43. var self = this;
  44. // the name of the test suite, as it will appear in the resulting XML file
  45. var suiteName;
  46. // the default name of the test suite if none is provided
  47. var DEFAULT_SUITE_NAME = 'Mocha Tests';
  48. if (options && options.reporterOptions) {
  49. if (options.reporterOptions.output) {
  50. if (!fs.createWriteStream) {
  51. throw new Error('file output not supported in browser');
  52. }
  53. mkdirp.sync(path.dirname(options.reporterOptions.output));
  54. self.fileStream = fs.createWriteStream(options.reporterOptions.output);
  55. }
  56. // get the suite name from the reporter options (if provided)
  57. suiteName = options.reporterOptions.suiteName;
  58. }
  59. // fall back to the default suite name
  60. suiteName = suiteName || DEFAULT_SUITE_NAME;
  61. runner.on('pending', function(test) {
  62. tests.push(test);
  63. });
  64. runner.on('pass', function(test) {
  65. tests.push(test);
  66. });
  67. runner.on('fail', function(test) {
  68. tests.push(test);
  69. });
  70. runner.once('end', function() {
  71. self.write(
  72. tag(
  73. 'testsuite',
  74. {
  75. name: suiteName,
  76. tests: stats.tests,
  77. failures: stats.failures,
  78. errors: stats.failures,
  79. skipped: stats.tests - stats.failures - stats.passes,
  80. timestamp: new Date().toUTCString(),
  81. time: stats.duration / 1000 || 0
  82. },
  83. false
  84. )
  85. );
  86. tests.forEach(function(t) {
  87. self.test(t);
  88. });
  89. self.write('</testsuite>');
  90. });
  91. }
  92. /**
  93. * Inherit from `Base.prototype`.
  94. */
  95. inherits(XUnit, Base);
  96. /**
  97. * Override done to close the stream (if it's a file).
  98. *
  99. * @param failures
  100. * @param {Function} fn
  101. */
  102. XUnit.prototype.done = function(failures, fn) {
  103. if (this.fileStream) {
  104. this.fileStream.end(function() {
  105. fn(failures);
  106. });
  107. } else {
  108. fn(failures);
  109. }
  110. };
  111. /**
  112. * Write out the given line.
  113. *
  114. * @param {string} line
  115. */
  116. XUnit.prototype.write = function(line) {
  117. if (this.fileStream) {
  118. this.fileStream.write(line + '\n');
  119. } else if (typeof process === 'object' && process.stdout) {
  120. process.stdout.write(line + '\n');
  121. } else {
  122. console.log(line);
  123. }
  124. };
  125. /**
  126. * Output tag for the given `test.`
  127. *
  128. * @param {Test} test
  129. */
  130. XUnit.prototype.test = function(test) {
  131. var attrs = {
  132. classname: test.parent.fullTitle(),
  133. name: test.title,
  134. time: test.duration / 1000 || 0
  135. };
  136. if (test.state === 'failed') {
  137. var err = test.err;
  138. this.write(
  139. tag(
  140. 'testcase',
  141. attrs,
  142. false,
  143. tag(
  144. 'failure',
  145. {},
  146. false,
  147. escape(err.message) + '\n' + escape(err.stack)
  148. )
  149. )
  150. );
  151. } else if (test.isPending()) {
  152. this.write(tag('testcase', attrs, false, tag('skipped', {}, true)));
  153. } else {
  154. this.write(tag('testcase', attrs, true));
  155. }
  156. };
  157. /**
  158. * HTML tag helper.
  159. *
  160. * @param name
  161. * @param attrs
  162. * @param close
  163. * @param content
  164. * @return {string}
  165. */
  166. function tag(name, attrs, close, content) {
  167. var end = close ? '/>' : '>';
  168. var pairs = [];
  169. var tag;
  170. for (var key in attrs) {
  171. if (Object.prototype.hasOwnProperty.call(attrs, key)) {
  172. pairs.push(key + '="' + escape(attrs[key]) + '"');
  173. }
  174. }
  175. tag = '<' + name + (pairs.length ? ' ' + pairs.join(' ') : '') + end;
  176. if (content) {
  177. tag += content + '</' + name + end;
  178. }
  179. return tag;
  180. }