VerboseReporter.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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 () {
  9. return data;
  10. };
  11. return data;
  12. }
  13. function _jestUtil() {
  14. const data = require('jest-util');
  15. _jestUtil = function () {
  16. return data;
  17. };
  18. return data;
  19. }
  20. var _DefaultReporter = _interopRequireDefault(require('./DefaultReporter'));
  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;
  38. class VerboseReporter extends _DefaultReporter.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. for (const title of testResult.ancestorTitles) {
  57. let matchingSuite = targetSuite.suites.find(s => s.title === title);
  58. if (!matchingSuite) {
  59. matchingSuite = {
  60. suites: [],
  61. tests: [],
  62. title
  63. };
  64. targetSuite.suites.push(matchingSuite);
  65. }
  66. targetSuite = matchingSuite;
  67. }
  68. targetSuite.tests.push(testResult);
  69. });
  70. return root;
  71. }
  72. onTestResult(test, result, aggregatedResults) {
  73. super.testFinished(test.context.config, result, aggregatedResults);
  74. if (!result.skipped) {
  75. this.printTestFileHeader(
  76. result.testFilePath,
  77. test.context.config,
  78. result
  79. );
  80. if (!result.testExecError && !result.skipped) {
  81. this._logTestResults(result.testResults);
  82. }
  83. this.printTestFileFailureMessage(
  84. result.testFilePath,
  85. test.context.config,
  86. result
  87. );
  88. }
  89. super.forceFlushBufferedOutput();
  90. }
  91. _logTestResults(testResults) {
  92. this._logSuite(VerboseReporter.groupTestsBySuites(testResults), 0);
  93. this._logLine();
  94. }
  95. _logSuite(suite, indentLevel) {
  96. if (suite.title) {
  97. this._logLine(suite.title, indentLevel);
  98. }
  99. this._logTests(suite.tests, indentLevel + 1);
  100. suite.suites.forEach(suite => this._logSuite(suite, indentLevel + 1));
  101. }
  102. _getIcon(status) {
  103. if (status === 'failed') {
  104. return _chalk().default.red(ICONS.failed);
  105. } else if (status === 'pending') {
  106. return _chalk().default.yellow(ICONS.pending);
  107. } else if (status === 'todo') {
  108. return _chalk().default.magenta(ICONS.todo);
  109. } else {
  110. return _chalk().default.green(ICONS.success);
  111. }
  112. }
  113. _logTest(test, indentLevel) {
  114. const status = this._getIcon(test.status);
  115. const time = test.duration
  116. ? ` (${(0, _jestUtil().formatTime)(Math.round(test.duration))})`
  117. : '';
  118. this._logLine(
  119. status + ' ' + _chalk().default.dim(test.title + time),
  120. indentLevel
  121. );
  122. }
  123. _logTests(tests, indentLevel) {
  124. if (this._globalConfig.expand) {
  125. tests.forEach(test => this._logTest(test, indentLevel));
  126. } else {
  127. const summedTests = tests.reduce(
  128. (result, test) => {
  129. if (test.status === 'pending') {
  130. result.pending.push(test);
  131. } else if (test.status === 'todo') {
  132. result.todo.push(test);
  133. } else {
  134. this._logTest(test, indentLevel);
  135. }
  136. return result;
  137. },
  138. {
  139. pending: [],
  140. todo: []
  141. }
  142. );
  143. if (summedTests.pending.length > 0) {
  144. summedTests.pending.forEach(this._logTodoOrPendingTest(indentLevel));
  145. }
  146. if (summedTests.todo.length > 0) {
  147. summedTests.todo.forEach(this._logTodoOrPendingTest(indentLevel));
  148. }
  149. }
  150. }
  151. _logTodoOrPendingTest(indentLevel) {
  152. return test => {
  153. const printedTestStatus =
  154. test.status === 'pending' ? 'skipped' : test.status;
  155. const icon = this._getIcon(test.status);
  156. const text = _chalk().default.dim(`${printedTestStatus} ${test.title}`);
  157. this._logLine(`${icon} ${text}`, indentLevel);
  158. };
  159. }
  160. _logLine(str, indentLevel) {
  161. const indentation = ' '.repeat(indentLevel || 0);
  162. this.log(indentation + (str || ''));
  163. }
  164. }
  165. exports.default = VerboseReporter;