VerboseReporter.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. } // Verbose mode is for debugging. Buffering of output is undesirable.
  44. // See https://github.com/facebook/jest/issues/8208
  45. __wrapStdio(stream) {
  46. const write = stream.write.bind(stream);
  47. stream.write = chunk => {
  48. this.__clearStatus();
  49. write(chunk);
  50. this.__printStatus();
  51. return true;
  52. };
  53. }
  54. static filterTestResults(testResults) {
  55. return testResults.filter(({status}) => status !== 'pending');
  56. }
  57. static groupTestsBySuites(testResults) {
  58. const root = {
  59. suites: [],
  60. tests: [],
  61. title: ''
  62. };
  63. testResults.forEach(testResult => {
  64. let targetSuite = root; // Find the target suite for this test,
  65. // creating nested suites as necessary.
  66. for (const title of testResult.ancestorTitles) {
  67. let matchingSuite = targetSuite.suites.find(s => s.title === title);
  68. if (!matchingSuite) {
  69. matchingSuite = {
  70. suites: [],
  71. tests: [],
  72. title
  73. };
  74. targetSuite.suites.push(matchingSuite);
  75. }
  76. targetSuite = matchingSuite;
  77. }
  78. targetSuite.tests.push(testResult);
  79. });
  80. return root;
  81. }
  82. onTestResult(test, result, aggregatedResults) {
  83. super.testFinished(test.context.config, result, aggregatedResults);
  84. if (!result.skipped) {
  85. this.printTestFileHeader(
  86. result.testFilePath,
  87. test.context.config,
  88. result
  89. );
  90. if (!result.testExecError && !result.skipped) {
  91. this._logTestResults(result.testResults);
  92. }
  93. this.printTestFileFailureMessage(
  94. result.testFilePath,
  95. test.context.config,
  96. result
  97. );
  98. }
  99. super.forceFlushBufferedOutput();
  100. }
  101. _logTestResults(testResults) {
  102. this._logSuite(VerboseReporter.groupTestsBySuites(testResults), 0);
  103. this._logLine();
  104. }
  105. _logSuite(suite, indentLevel) {
  106. if (suite.title) {
  107. this._logLine(suite.title, indentLevel);
  108. }
  109. this._logTests(suite.tests, indentLevel + 1);
  110. suite.suites.forEach(suite => this._logSuite(suite, indentLevel + 1));
  111. }
  112. _getIcon(status) {
  113. if (status === 'failed') {
  114. return _chalk().default.red(ICONS.failed);
  115. } else if (status === 'pending') {
  116. return _chalk().default.yellow(ICONS.pending);
  117. } else if (status === 'todo') {
  118. return _chalk().default.magenta(ICONS.todo);
  119. } else {
  120. return _chalk().default.green(ICONS.success);
  121. }
  122. }
  123. _logTest(test, indentLevel) {
  124. const status = this._getIcon(test.status);
  125. const time = test.duration
  126. ? ` (${(0, _jestUtil().formatTime)(Math.round(test.duration))})`
  127. : '';
  128. this._logLine(
  129. status + ' ' + _chalk().default.dim(test.title + time),
  130. indentLevel
  131. );
  132. }
  133. _logTests(tests, indentLevel) {
  134. if (this._globalConfig.expand) {
  135. tests.forEach(test => this._logTest(test, indentLevel));
  136. } else {
  137. const summedTests = tests.reduce(
  138. (result, test) => {
  139. if (test.status === 'pending') {
  140. result.pending.push(test);
  141. } else if (test.status === 'todo') {
  142. result.todo.push(test);
  143. } else {
  144. this._logTest(test, indentLevel);
  145. }
  146. return result;
  147. },
  148. {
  149. pending: [],
  150. todo: []
  151. }
  152. );
  153. if (summedTests.pending.length > 0) {
  154. summedTests.pending.forEach(this._logTodoOrPendingTest(indentLevel));
  155. }
  156. if (summedTests.todo.length > 0) {
  157. summedTests.todo.forEach(this._logTodoOrPendingTest(indentLevel));
  158. }
  159. }
  160. }
  161. _logTodoOrPendingTest(indentLevel) {
  162. return test => {
  163. const printedTestStatus =
  164. test.status === 'pending' ? 'skipped' : test.status;
  165. const icon = this._getIcon(test.status);
  166. const text = _chalk().default.dim(`${printedTestStatus} ${test.title}`);
  167. this._logLine(`${icon} ${text}`, indentLevel);
  168. };
  169. }
  170. _logLine(str, indentLevel) {
  171. const indentation = ' '.repeat(indentLevel || 0);
  172. this.log(indentation + (str || ''));
  173. }
  174. }
  175. exports.default = VerboseReporter;
  176. _defineProperty(VerboseReporter, 'filename', __filename);