DefaultReporter.js 5.7 KB

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