default_reporter.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. function _jestUtil() {
  7. const data = require('jest-util');
  8. _jestUtil = function _jestUtil() {
  9. return data;
  10. };
  11. return data;
  12. }
  13. function _chalk() {
  14. const data = _interopRequireDefault(require('chalk'));
  15. _chalk = function _chalk() {
  16. return data;
  17. };
  18. return data;
  19. }
  20. var _base_reporter = _interopRequireDefault(require('./base_reporter'));
  21. var _Status = _interopRequireDefault(require('./Status'));
  22. var _get_result_header = _interopRequireDefault(require('./get_result_header'));
  23. var _get_snapshot_status = _interopRequireDefault(
  24. require('./get_snapshot_status')
  25. );
  26. function _interopRequireDefault(obj) {
  27. return obj && obj.__esModule ? obj : {default: obj};
  28. }
  29. function _defineProperty(obj, key, value) {
  30. if (key in obj) {
  31. Object.defineProperty(obj, key, {
  32. value: value,
  33. enumerable: true,
  34. configurable: true,
  35. writable: true
  36. });
  37. } else {
  38. obj[key] = value;
  39. }
  40. return obj;
  41. }
  42. const TITLE_BULLET = _chalk().default.bold('\u25cf ');
  43. class DefaultReporter extends _base_reporter.default {
  44. // ANSI clear sequence for the last printed status
  45. constructor(globalConfig) {
  46. super();
  47. _defineProperty(this, '_clear', void 0);
  48. _defineProperty(this, '_err', void 0);
  49. _defineProperty(this, '_globalConfig', void 0);
  50. _defineProperty(this, '_out', void 0);
  51. _defineProperty(this, '_status', void 0);
  52. _defineProperty(this, '_bufferedOutput', void 0);
  53. this._globalConfig = globalConfig;
  54. this._clear = '';
  55. this._out = process.stdout.write.bind(process.stdout);
  56. this._err = process.stderr.write.bind(process.stderr);
  57. this._status = new _Status.default();
  58. this._bufferedOutput = new Set();
  59. this._wrapStdio(process.stdout);
  60. this._wrapStdio(process.stderr);
  61. this._status.onChange(() => {
  62. this._clearStatus();
  63. this._printStatus();
  64. });
  65. }
  66. _wrapStdio(stream) {
  67. const originalWrite = stream.write;
  68. let buffer = [];
  69. let timeout = null;
  70. const flushBufferedOutput = () => {
  71. const string = buffer.join('');
  72. buffer = []; // This is to avoid conflicts between random output and status text
  73. this._clearStatus();
  74. if (string) {
  75. originalWrite.call(stream, string);
  76. }
  77. this._printStatus();
  78. this._bufferedOutput.delete(flushBufferedOutput);
  79. };
  80. this._bufferedOutput.add(flushBufferedOutput);
  81. const debouncedFlush = () => {
  82. // If the process blows up no errors would be printed.
  83. // There should be a smart way to buffer stderr, but for now
  84. // we just won't buffer it.
  85. if (stream === process.stderr) {
  86. flushBufferedOutput();
  87. } else {
  88. if (!timeout) {
  89. timeout = setTimeout(() => {
  90. flushBufferedOutput();
  91. timeout = null;
  92. }, 100);
  93. }
  94. }
  95. };
  96. stream.write = chunk => {
  97. buffer.push(chunk);
  98. debouncedFlush();
  99. return true;
  100. };
  101. } // Don't wait for the debounced call and flush all output immediately.
  102. forceFlushBufferedOutput() {
  103. var _iteratorNormalCompletion = true;
  104. var _didIteratorError = false;
  105. var _iteratorError = undefined;
  106. try {
  107. for (
  108. var _iterator = this._bufferedOutput[Symbol.iterator](), _step;
  109. !(_iteratorNormalCompletion = (_step = _iterator.next()).done);
  110. _iteratorNormalCompletion = true
  111. ) {
  112. const flushBufferedOutput = _step.value;
  113. flushBufferedOutput();
  114. }
  115. } catch (err) {
  116. _didIteratorError = true;
  117. _iteratorError = err;
  118. } finally {
  119. try {
  120. if (!_iteratorNormalCompletion && _iterator.return != null) {
  121. _iterator.return();
  122. }
  123. } finally {
  124. if (_didIteratorError) {
  125. throw _iteratorError;
  126. }
  127. }
  128. }
  129. }
  130. _clearStatus() {
  131. if (_jestUtil().isInteractive) {
  132. if (this._globalConfig.useStderr) {
  133. this._err(this._clear);
  134. } else {
  135. this._out(this._clear);
  136. }
  137. }
  138. }
  139. _printStatus() {
  140. const _this$_status$get = this._status.get(),
  141. content = _this$_status$get.content,
  142. clear = _this$_status$get.clear;
  143. this._clear = clear;
  144. if (_jestUtil().isInteractive) {
  145. if (this._globalConfig.useStderr) {
  146. this._err(content);
  147. } else {
  148. this._out(content);
  149. }
  150. }
  151. }
  152. onRunStart(aggregatedResults, options) {
  153. this._status.runStarted(aggregatedResults, options);
  154. }
  155. onTestStart(test) {
  156. this._status.testStarted(test.path, test.context.config);
  157. }
  158. onRunComplete() {
  159. this.forceFlushBufferedOutput();
  160. this._status.runFinished();
  161. process.stdout.write = this._out;
  162. process.stderr.write = this._err;
  163. (0, _jestUtil().clearLine)(process.stderr);
  164. }
  165. onTestResult(test, testResult, aggregatedResults) {
  166. this.testFinished(test.context.config, testResult, aggregatedResults);
  167. if (!testResult.skipped) {
  168. this.printTestFileHeader(
  169. testResult.testFilePath,
  170. test.context.config,
  171. testResult
  172. );
  173. this.printTestFileFailureMessage(
  174. testResult.testFilePath,
  175. test.context.config,
  176. testResult
  177. );
  178. }
  179. this.forceFlushBufferedOutput();
  180. }
  181. testFinished(config, testResult, aggregatedResults) {
  182. this._status.testFinished(config, testResult, aggregatedResults);
  183. }
  184. printTestFileHeader(_testPath, config, result) {
  185. this.log(
  186. (0, _get_result_header.default)(result, this._globalConfig, config)
  187. );
  188. if (result.console) {
  189. this.log(
  190. ' ' +
  191. TITLE_BULLET +
  192. 'Console\n\n' +
  193. (0, _jestUtil().getConsoleOutput)(
  194. config.cwd,
  195. !!this._globalConfig.verbose,
  196. result.console
  197. )
  198. );
  199. }
  200. }
  201. printTestFileFailureMessage(_testPath, _config, result) {
  202. if (result.failureMessage) {
  203. this.log(result.failureMessage);
  204. }
  205. const didUpdate = this._globalConfig.updateSnapshot === 'all';
  206. const snapshotStatuses = (0, _get_snapshot_status.default)(
  207. result.snapshot,
  208. didUpdate
  209. );
  210. snapshotStatuses.forEach(this.log);
  211. }
  212. }
  213. exports.default = DefaultReporter;