BufferedConsole.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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, ...rest) {
  121. this._log('dir', (0, _util().format)(firstArg, ...rest));
  122. }
  123. dirxml(firstArg, ...rest) {
  124. this._log('dirxml', (0, _util().format)(firstArg, ...rest));
  125. }
  126. error(firstArg, ...rest) {
  127. this._log('error', (0, _util().format)(firstArg, ...rest));
  128. }
  129. group(title, ...rest) {
  130. this._groupDepth++;
  131. if (title || rest.length > 0) {
  132. this._log(
  133. 'group',
  134. _chalk().default.bold((0, _util().format)(title, ...rest))
  135. );
  136. }
  137. }
  138. groupCollapsed(title, ...rest) {
  139. this._groupDepth++;
  140. if (title || rest.length > 0) {
  141. this._log(
  142. 'groupCollapsed',
  143. _chalk().default.bold((0, _util().format)(title, ...rest))
  144. );
  145. }
  146. }
  147. groupEnd() {
  148. if (this._groupDepth > 0) {
  149. this._groupDepth--;
  150. }
  151. }
  152. info(firstArg, ...rest) {
  153. this._log('info', (0, _util().format)(firstArg, ...rest));
  154. }
  155. log(firstArg, ...rest) {
  156. this._log('log', (0, _util().format)(firstArg, ...rest));
  157. }
  158. time(label = 'default') {
  159. if (this._timers[label]) {
  160. return;
  161. }
  162. this._timers[label] = new Date();
  163. }
  164. timeEnd(label = 'default') {
  165. const startTime = this._timers[label];
  166. if (startTime) {
  167. const endTime = new Date();
  168. const time = endTime.getTime() - startTime.getTime();
  169. this._log(
  170. 'time',
  171. (0, _util().format)(`${label}: ${(0, _jestUtil().formatTime)(time)}`)
  172. );
  173. delete this._timers[label];
  174. }
  175. }
  176. timeLog(label = 'default', ...data) {
  177. const startTime = this._timers[label];
  178. if (startTime) {
  179. const endTime = new Date();
  180. const time = endTime.getTime() - startTime.getTime();
  181. this._log(
  182. 'time',
  183. (0, _util().format)(
  184. `${label}: ${(0, _jestUtil().formatTime)(time)}`,
  185. ...data
  186. )
  187. );
  188. }
  189. }
  190. warn(firstArg, ...rest) {
  191. this._log('warn', (0, _util().format)(firstArg, ...rest));
  192. }
  193. getBuffer() {
  194. return this._buffer.length ? this._buffer : undefined;
  195. }
  196. }
  197. exports.default = BufferedConsole;
  198. function invariant(condition, message) {
  199. if (!condition) {
  200. throw new Error(message);
  201. }
  202. }