xunit.js 3.6 KB

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