console-reporter.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. (function (root, factory) {
  2. define([], factory);
  3. } (this, function () {
  4. "use strict";
  5. var noopTimer = {
  6. start: function () {},
  7. elapsed: function () { return 0; }
  8. };
  9. function ConsoleReporter (options) {
  10. var timer = noopTimer,
  11. specCount,
  12. failureCount,
  13. failedSpecs = [],
  14. pendingCount,
  15. ansi = {
  16. green: '\x1B[32m',
  17. red: '\x1B[31m',
  18. yellow: '\x1B[33m',
  19. none: '\x1B[0m'
  20. },
  21. failedSuites = [];
  22. var print = function print (message) {
  23. console.log(message + '\x03\b');
  24. }
  25. this.jasmineStarted = function () {
  26. specCount = 0;
  27. failureCount = 0;
  28. pendingCount = 0;
  29. print('Started');
  30. printNewline();
  31. timer.start();
  32. };
  33. this.jasmineDone = function () {
  34. print("jasmineDone");
  35. printNewline();
  36. for (var i = 0; i < failedSpecs.length; i++) {
  37. specFailureDetails(failedSpecs[i]);
  38. }
  39. if(specCount > 0) {
  40. printNewline();
  41. var specCounts = specCount + ' ' + plural('spec', specCount) + ', ' +
  42. failureCount + ' ' + plural('failure', failureCount);
  43. if (pendingCount) {
  44. specCounts += ', ' + pendingCount + ' pending ' + plural('spec', pendingCount);
  45. }
  46. print(specCounts);
  47. } else {
  48. print('No specs found');
  49. }
  50. printNewline();
  51. var seconds = timer.elapsed() / 1000;
  52. print('Finished in ' + seconds + ' ' + plural('second', seconds));
  53. printNewline();
  54. for (i = 0; i < failedSuites.length; i++) {
  55. suiteFailureDetails(failedSuites[i]);
  56. }
  57. var exitCode = failureCount === 0 ? 0 : 1;
  58. console.info('All tests completed!' + exitCode);
  59. };
  60. this.specDone = function (result) {
  61. specCount++;
  62. if (result.status == 'pending') {
  63. pendingCount++;
  64. print(colored('yellow', '*'));
  65. return;
  66. }
  67. if (result.status == 'passed') {
  68. print(colored('green', '.'));
  69. return;
  70. }
  71. if (result.status == 'failed') {
  72. failureCount++;
  73. failedSpecs.push(result);
  74. print(colored('red', 'F'));
  75. }
  76. };
  77. this.suiteDone = function (result) {
  78. if (result.failedExpectations && result.failedExpectations.length > 0) {
  79. failureCount++;
  80. failedSuites.push(result);
  81. }
  82. };
  83. return this;
  84. function printNewline() {
  85. print('\n');
  86. }
  87. function colored (color, str) {
  88. return ansi[color] + str + ansi.none;
  89. }
  90. function plural (str, count) {
  91. return count == 1 ? str : str + 's';
  92. }
  93. function repeat (thing, times) {
  94. var arr = [];
  95. for (var i = 0; i < times; i++) {
  96. arr.push(thing);
  97. }
  98. return arr;
  99. }
  100. function indent (str, spaces) {
  101. var lines = (str || '').split('\n');
  102. var newArr = [];
  103. for (var i = 0; i < lines.length; i++) {
  104. newArr.push(repeat(' ', spaces).join('') + lines[i]);
  105. }
  106. return newArr.join('\n');
  107. }
  108. function specFailureDetails (result) {
  109. printNewline();
  110. print(result.fullName);
  111. for (var i = 0; i < result.failedExpectations.length; i++) {
  112. var failedExpectation = result.failedExpectations[i];
  113. printNewline();
  114. print(indent(failedExpectation.message, 2));
  115. print(indent(failedExpectation.stack, 2));
  116. }
  117. printNewline();
  118. }
  119. function suiteFailureDetails (result) {
  120. for (var i = 0; i < result.failedExpectations.length; i++) {
  121. printNewline();
  122. print(colored('red', 'An error was thrown in an afterAll'));
  123. printNewline();
  124. print(colored('red', 'AfterAll ' + result.failedExpectations[i].message));
  125. }
  126. printNewline();
  127. }
  128. }
  129. return ConsoleReporter;
  130. }));