BufferedConsole.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. function _assert() {
  7. const data = _interopRequireDefault(require('assert'));
  8. _assert = function () {
  9. return data;
  10. };
  11. return data;
  12. }
  13. function _console() {
  14. const data = require('console');
  15. _console = function () {
  16. return data;
  17. };
  18. return data;
  19. }
  20. function _util() {
  21. const data = require('util');
  22. _util = function () {
  23. return data;
  24. };
  25. return data;
  26. }
  27. function _chalk() {
  28. const data = _interopRequireDefault(require('chalk'));
  29. _chalk = function () {
  30. return data;
  31. };
  32. return data;
  33. }
  34. function _jestUtil() {
  35. const data = require('jest-util');
  36. _jestUtil = function () {
  37. return data;
  38. };
  39. return data;
  40. }
  41. function _interopRequireDefault(obj) {
  42. return obj && obj.__esModule ? obj : {default: obj};
  43. }
  44. function _defineProperty(obj, key, value) {
  45. if (key in obj) {
  46. Object.defineProperty(obj, key, {
  47. value: value,
  48. enumerable: true,
  49. configurable: true,
  50. writable: true
  51. });
  52. } else {
  53. obj[key] = value;
  54. }
  55. return obj;
  56. }
  57. class BufferedConsole extends _console().Console {
  58. constructor() {
  59. super({
  60. write: message => {
  61. BufferedConsole.write(this._buffer, 'log', message, null);
  62. return true;
  63. }
  64. });
  65. _defineProperty(this, '_buffer', []);
  66. _defineProperty(this, '_counters', {});
  67. _defineProperty(this, '_timers', {});
  68. _defineProperty(this, '_groupDepth', 0);
  69. _defineProperty(this, 'Console', _console().Console);
  70. }
  71. static write(buffer, type, message, level) {
  72. const stackLevel = level != null ? level : 2;
  73. const rawStack = new (_jestUtil().ErrorWithStack)(
  74. undefined,
  75. BufferedConsole.write
  76. ).stack;
  77. invariant(rawStack, 'always have a stack trace');
  78. const origin = rawStack
  79. .split('\n')
  80. .slice(stackLevel)
  81. .filter(Boolean)
  82. .join('\n');
  83. buffer.push({
  84. message,
  85. origin,
  86. type
  87. });
  88. return buffer;
  89. }
  90. _log(type, message) {
  91. BufferedConsole.write(
  92. this._buffer,
  93. type,
  94. ' '.repeat(this._groupDepth) + message,
  95. 3
  96. );
  97. }
  98. assert(value, message) {
  99. try {
  100. (0, _assert().default)(value, message);
  101. } catch (error) {
  102. this._log('assert', error.toString());
  103. }
  104. }
  105. count(label = 'default') {
  106. if (!this._counters[label]) {
  107. this._counters[label] = 0;
  108. }
  109. this._log(
  110. 'count',
  111. (0, _util().format)(`${label}: ${++this._counters[label]}`)
  112. );
  113. }
  114. countReset(label = 'default') {
  115. this._counters[label] = 0;
  116. }
  117. debug(firstArg, ...rest) {
  118. this._log('debug', (0, _util().format)(firstArg, ...rest));
  119. }
  120. dir(firstArg, options = {}) {
  121. const representation = (0, _util().inspect)(firstArg, options);
  122. this._log('dir', (0, _util().formatWithOptions)(options, representation));
  123. }
  124. dirxml(firstArg, ...rest) {
  125. this._log('dirxml', (0, _util().format)(firstArg, ...rest));
  126. }
  127. error(firstArg, ...rest) {
  128. this._log('error', (0, _util().format)(firstArg, ...rest));
  129. }
  130. group(title, ...rest) {
  131. this._groupDepth++;
  132. if (title || rest.length > 0) {
  133. this._log(
  134. 'group',
  135. _chalk().default.bold((0, _util().format)(title, ...rest))
  136. );
  137. }
  138. }
  139. groupCollapsed(title, ...rest) {
  140. this._groupDepth++;
  141. if (title || rest.length > 0) {
  142. this._log(
  143. 'groupCollapsed',
  144. _chalk().default.bold((0, _util().format)(title, ...rest))
  145. );
  146. }
  147. }
  148. groupEnd() {
  149. if (this._groupDepth > 0) {
  150. this._groupDepth--;
  151. }
  152. }
  153. info(firstArg, ...rest) {
  154. this._log('info', (0, _util().format)(firstArg, ...rest));
  155. }
  156. log(firstArg, ...rest) {
  157. this._log('log', (0, _util().format)(firstArg, ...rest));
  158. }
  159. time(label = 'default') {
  160. if (this._timers[label]) {
  161. return;
  162. }
  163. this._timers[label] = new Date();
  164. }
  165. timeEnd(label = 'default') {
  166. const startTime = this._timers[label];
  167. if (startTime) {
  168. const endTime = new Date();
  169. const time = endTime.getTime() - startTime.getTime();
  170. this._log(
  171. 'time',
  172. (0, _util().format)(`${label}: ${(0, _jestUtil().formatTime)(time)}`)
  173. );
  174. delete this._timers[label];
  175. }
  176. }
  177. timeLog(label = 'default', ...data) {
  178. const startTime = this._timers[label];
  179. if (startTime) {
  180. const endTime = new Date();
  181. const time = endTime.getTime() - startTime.getTime();
  182. this._log(
  183. 'time',
  184. (0, _util().format)(
  185. `${label}: ${(0, _jestUtil().formatTime)(time)}`,
  186. ...data
  187. )
  188. );
  189. }
  190. }
  191. warn(firstArg, ...rest) {
  192. this._log('warn', (0, _util().format)(firstArg, ...rest));
  193. }
  194. getBuffer() {
  195. return this._buffer.length ? this._buffer : undefined;
  196. }
  197. }
  198. exports.default = BufferedConsole;
  199. function invariant(condition, message) {
  200. if (!condition) {
  201. throw new Error(message);
  202. }
  203. }