console_reporter.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. module.exports = exports = ConsoleReporter;
  2. var noopTimer = {
  3. start: function(){},
  4. elapsed: function(){ return 0; }
  5. };
  6. function ConsoleReporter() {
  7. var print = function() {},
  8. showColors = false,
  9. timer = noopTimer,
  10. jasmineCorePath = null,
  11. printDeprecation = function() {},
  12. specCount,
  13. executableSpecCount,
  14. failureCount,
  15. failedSpecs = [],
  16. pendingSpecs = [],
  17. ansi = {
  18. green: '\x1B[32m',
  19. red: '\x1B[31m',
  20. yellow: '\x1B[33m',
  21. none: '\x1B[0m'
  22. },
  23. failedSuites = [],
  24. stackFilter = defaultStackFilter,
  25. onComplete = function() {};
  26. this.setOptions = function(options) {
  27. if (options.print) {
  28. print = options.print;
  29. }
  30. showColors = options.showColors || false;
  31. if (options.timer) {
  32. timer = options.timer;
  33. }
  34. if (options.jasmineCorePath) {
  35. jasmineCorePath = options.jasmineCorePath;
  36. }
  37. if (options.printDeprecation) {
  38. printDeprecation = options.printDeprecation;
  39. }
  40. if (options.stackFilter) {
  41. stackFilter = options.stackFilter;
  42. }
  43. if(options.onComplete) {
  44. printDeprecation('Passing in an onComplete function to the ConsoleReporter is deprecated.');
  45. onComplete = options.onComplete;
  46. }
  47. };
  48. this.jasmineStarted = function(options) {
  49. specCount = 0;
  50. executableSpecCount = 0;
  51. failureCount = 0;
  52. if (options && options.order && options.order.random) {
  53. print('Randomized with seed ' + options.order.seed);
  54. printNewline();
  55. }
  56. print('Started');
  57. printNewline();
  58. timer.start();
  59. };
  60. this.jasmineDone = function(result) {
  61. printNewline();
  62. printNewline();
  63. if(failedSpecs.length > 0) {
  64. print('Failures:');
  65. }
  66. for (var i = 0; i < failedSpecs.length; i++) {
  67. specFailureDetails(failedSpecs[i], i + 1);
  68. }
  69. if (pendingSpecs.length > 0) {
  70. print("Pending:");
  71. }
  72. for(i = 0; i < pendingSpecs.length; i++) {
  73. pendingSpecDetails(pendingSpecs[i], i + 1);
  74. }
  75. if(specCount > 0) {
  76. printNewline();
  77. if(executableSpecCount !== specCount) {
  78. print('Ran ' + executableSpecCount + ' of ' + specCount + plural(' spec', specCount));
  79. printNewline();
  80. }
  81. var specCounts = executableSpecCount + ' ' + plural('spec', executableSpecCount) + ', ' +
  82. failureCount + ' ' + plural('failure', failureCount);
  83. if (pendingSpecs.length) {
  84. specCounts += ', ' + pendingSpecs.length + ' pending ' + plural('spec', pendingSpecs.length);
  85. }
  86. print(specCounts);
  87. } else {
  88. print('No specs found');
  89. }
  90. printNewline();
  91. var seconds = timer.elapsed() / 1000;
  92. print('Finished in ' + seconds + ' ' + plural('second', seconds));
  93. printNewline();
  94. for(i = 0; i < failedSuites.length; i++) {
  95. suiteFailureDetails(failedSuites[i]);
  96. }
  97. if (result && result.failedExpectations) {
  98. suiteFailureDetails(result);
  99. }
  100. if (result && result.order && result.order.random) {
  101. print('Randomized with seed ' + result.order.seed);
  102. printNewline();
  103. }
  104. onComplete(failureCount === 0);
  105. };
  106. this.specDone = function(result) {
  107. specCount++;
  108. if (result.status == 'pending') {
  109. pendingSpecs.push(result);
  110. executableSpecCount++;
  111. print(colored('yellow', '*'));
  112. return;
  113. }
  114. if (result.status == 'passed') {
  115. executableSpecCount++;
  116. print(colored('green', '.'));
  117. return;
  118. }
  119. if (result.status == 'failed') {
  120. failureCount++;
  121. failedSpecs.push(result);
  122. executableSpecCount++;
  123. print(colored('red', 'F'));
  124. }
  125. };
  126. this.suiteDone = function(result) {
  127. if (result.failedExpectations && result.failedExpectations.length > 0) {
  128. failureCount++;
  129. failedSuites.push(result);
  130. }
  131. };
  132. return this;
  133. function printNewline() {
  134. print('\n');
  135. }
  136. function colored(color, str) {
  137. return showColors ? (ansi[color] + str + ansi.none) : str;
  138. }
  139. function plural(str, count) {
  140. return count == 1 ? str : str + 's';
  141. }
  142. function repeat(thing, times) {
  143. var arr = [];
  144. for (var i = 0; i < times; i++) {
  145. arr.push(thing);
  146. }
  147. return arr;
  148. }
  149. function indent(str, spaces) {
  150. var lines = (str || '').split('\n');
  151. var newArr = [];
  152. for (var i = 0; i < lines.length; i++) {
  153. newArr.push(repeat(' ', spaces).join('') + lines[i]);
  154. }
  155. return newArr.join('\n');
  156. }
  157. function defaultStackFilter(stack) {
  158. if (!stack) {
  159. return '';
  160. }
  161. var filteredStack = stack.split('\n').filter(function(stackLine) {
  162. return stackLine.indexOf(jasmineCorePath) === -1;
  163. }).join('\n');
  164. return filteredStack;
  165. }
  166. function specFailureDetails(result, failedSpecNumber) {
  167. printNewline();
  168. print(failedSpecNumber + ') ');
  169. print(result.fullName);
  170. for (var i = 0; i < result.failedExpectations.length; i++) {
  171. var failedExpectation = result.failedExpectations[i];
  172. printNewline();
  173. print(indent('Message:', 2));
  174. printNewline();
  175. print(colored('red', indent(failedExpectation.message, 4)));
  176. printNewline();
  177. print(indent('Stack:', 2));
  178. printNewline();
  179. print(indent(stackFilter(failedExpectation.stack), 4));
  180. }
  181. printNewline();
  182. }
  183. function suiteFailureDetails(result) {
  184. for (var i = 0; i < result.failedExpectations.length; i++) {
  185. printNewline();
  186. print(colored('red', 'An error was thrown in an afterAll'));
  187. printNewline();
  188. print(colored('red', 'AfterAll ' + result.failedExpectations[i].message));
  189. }
  190. printNewline();
  191. }
  192. function pendingSpecDetails(result, pendingSpecNumber) {
  193. printNewline();
  194. printNewline();
  195. print(pendingSpecNumber + ') ');
  196. print(result.fullName);
  197. printNewline();
  198. var pendingReason = "No reason given";
  199. if (result.pendingReason && result.pendingReason !== '') {
  200. pendingReason = result.pendingReason;
  201. }
  202. print(indent(colored('yellow', pendingReason), 2));
  203. printNewline();
  204. }
  205. }