verbose_reporter.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. function _chalk() {
  7. const data = _interopRequireDefault(require('chalk'));
  8. _chalk = function _chalk() {
  9. return data;
  10. };
  11. return data;
  12. }
  13. function _jestUtil() {
  14. const data = require('jest-util');
  15. _jestUtil = function _jestUtil() {
  16. return data;
  17. };
  18. return data;
  19. }
  20. var _default_reporter = _interopRequireDefault(require('./default_reporter'));
  21. function _interopRequireDefault(obj) {
  22. return obj && obj.__esModule ? obj : {default: obj};
  23. }
  24. function _defineProperty(obj, key, value) {
  25. if (key in obj) {
  26. Object.defineProperty(obj, key, {
  27. value: value,
  28. enumerable: true,
  29. configurable: true,
  30. writable: true
  31. });
  32. } else {
  33. obj[key] = value;
  34. }
  35. return obj;
  36. }
  37. const ICONS = _jestUtil().specialChars.ICONS;
  38. class VerboseReporter extends _default_reporter.default {
  39. constructor(globalConfig) {
  40. super(globalConfig);
  41. _defineProperty(this, '_globalConfig', void 0);
  42. this._globalConfig = globalConfig;
  43. }
  44. static filterTestResults(testResults) {
  45. return testResults.filter(({status}) => status !== 'pending');
  46. }
  47. static groupTestsBySuites(testResults) {
  48. const root = {
  49. suites: [],
  50. tests: [],
  51. title: ''
  52. };
  53. testResults.forEach(testResult => {
  54. let targetSuite = root; // Find the target suite for this test,
  55. // creating nested suites as necessary.
  56. var _iteratorNormalCompletion = true;
  57. var _didIteratorError = false;
  58. var _iteratorError = undefined;
  59. try {
  60. for (
  61. var _iterator = testResult.ancestorTitles[Symbol.iterator](), _step;
  62. !(_iteratorNormalCompletion = (_step = _iterator.next()).done);
  63. _iteratorNormalCompletion = true
  64. ) {
  65. const title = _step.value;
  66. let matchingSuite = targetSuite.suites.find(s => s.title === title);
  67. if (!matchingSuite) {
  68. matchingSuite = {
  69. suites: [],
  70. tests: [],
  71. title
  72. };
  73. targetSuite.suites.push(matchingSuite);
  74. }
  75. targetSuite = matchingSuite;
  76. }
  77. } catch (err) {
  78. _didIteratorError = true;
  79. _iteratorError = err;
  80. } finally {
  81. try {
  82. if (!_iteratorNormalCompletion && _iterator.return != null) {
  83. _iterator.return();
  84. }
  85. } finally {
  86. if (_didIteratorError) {
  87. throw _iteratorError;
  88. }
  89. }
  90. }
  91. targetSuite.tests.push(testResult);
  92. });
  93. return root;
  94. }
  95. onTestResult(test, result, aggregatedResults) {
  96. super.testFinished(test.context.config, result, aggregatedResults);
  97. if (!result.skipped) {
  98. this.printTestFileHeader(
  99. result.testFilePath,
  100. test.context.config,
  101. result
  102. );
  103. if (!result.testExecError && !result.skipped) {
  104. this._logTestResults(result.testResults);
  105. }
  106. this.printTestFileFailureMessage(
  107. result.testFilePath,
  108. test.context.config,
  109. result
  110. );
  111. }
  112. super.forceFlushBufferedOutput();
  113. }
  114. _logTestResults(testResults) {
  115. this._logSuite(VerboseReporter.groupTestsBySuites(testResults), 0);
  116. this._logLine();
  117. }
  118. _logSuite(suite, indentLevel) {
  119. if (suite.title) {
  120. this._logLine(suite.title, indentLevel);
  121. }
  122. this._logTests(suite.tests, indentLevel + 1);
  123. suite.suites.forEach(suite => this._logSuite(suite, indentLevel + 1));
  124. }
  125. _getIcon(status) {
  126. if (status === 'failed') {
  127. return _chalk().default.red(ICONS.failed);
  128. } else if (status === 'pending') {
  129. return _chalk().default.yellow(ICONS.pending);
  130. } else if (status === 'todo') {
  131. return _chalk().default.magenta(ICONS.todo);
  132. } else {
  133. return _chalk().default.green(ICONS.success);
  134. }
  135. }
  136. _logTest(test, indentLevel) {
  137. const status = this._getIcon(test.status);
  138. const time = test.duration ? ` (${test.duration.toFixed(0)}ms)` : '';
  139. this._logLine(
  140. status + ' ' + _chalk().default.dim(test.title + time),
  141. indentLevel
  142. );
  143. }
  144. _logTests(tests, indentLevel) {
  145. if (this._globalConfig.expand) {
  146. tests.forEach(test => this._logTest(test, indentLevel));
  147. } else {
  148. const summedTests = tests.reduce(
  149. (result, test) => {
  150. if (test.status === 'pending') {
  151. result.pending.push(test);
  152. } else if (test.status === 'todo') {
  153. result.todo.push(test);
  154. } else {
  155. this._logTest(test, indentLevel);
  156. }
  157. return result;
  158. },
  159. {
  160. pending: [],
  161. todo: []
  162. }
  163. );
  164. if (summedTests.pending.length > 0) {
  165. summedTests.pending.forEach(this._logTodoOrPendingTest(indentLevel));
  166. }
  167. if (summedTests.todo.length > 0) {
  168. summedTests.todo.forEach(this._logTodoOrPendingTest(indentLevel));
  169. }
  170. }
  171. }
  172. _logTodoOrPendingTest(indentLevel) {
  173. return test => {
  174. const printedTestStatus =
  175. test.status === 'pending' ? 'skipped' : test.status;
  176. const icon = this._getIcon(test.status);
  177. const text = _chalk().default.dim(`${printedTestStatus} ${test.title}`);
  178. this._logLine(`${icon} ${text}`, indentLevel);
  179. };
  180. }
  181. _logLine(str, indentLevel) {
  182. const indentation = ' '.repeat(indentLevel || 0);
  183. this.log(indentation + (str || ''));
  184. }
  185. }
  186. exports.default = VerboseReporter;