FailedTestsInteractiveMode.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. function _ansiEscapes() {
  7. const data = _interopRequireDefault(require('ansi-escapes'));
  8. _ansiEscapes = function () {
  9. return data;
  10. };
  11. return data;
  12. }
  13. function _chalk() {
  14. const data = _interopRequireDefault(require('chalk'));
  15. _chalk = 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. function _jestWatcher() {
  28. const data = require('jest-watcher');
  29. _jestWatcher = function () {
  30. return data;
  31. };
  32. return data;
  33. }
  34. function _interopRequireDefault(obj) {
  35. return obj && obj.__esModule ? obj : {default: obj};
  36. }
  37. function _defineProperty(obj, key, value) {
  38. if (key in obj) {
  39. Object.defineProperty(obj, key, {
  40. value: value,
  41. enumerable: true,
  42. configurable: true,
  43. writable: true
  44. });
  45. } else {
  46. obj[key] = value;
  47. }
  48. return obj;
  49. }
  50. const {ARROW, CLEAR} = _jestUtil().specialChars;
  51. function describeKey(key, description) {
  52. return `${_chalk().default.dim(
  53. ARROW + 'Press'
  54. )} ${key} ${_chalk().default.dim(description)}`;
  55. }
  56. const TestProgressLabel = _chalk().default.bold('Interactive Test Progress');
  57. class FailedTestsInteractiveMode {
  58. constructor(_pipe) {
  59. _defineProperty(this, '_isActive', false);
  60. _defineProperty(this, '_countPaths', 0);
  61. _defineProperty(this, '_skippedNum', 0);
  62. _defineProperty(this, '_testAssertions', []);
  63. _defineProperty(this, '_updateTestRunnerConfig', void 0);
  64. this._pipe = _pipe;
  65. }
  66. isActive() {
  67. return this._isActive;
  68. }
  69. put(key) {
  70. switch (key) {
  71. case 's':
  72. if (this._skippedNum === this._testAssertions.length) {
  73. break;
  74. }
  75. this._skippedNum += 1; // move skipped test to the end
  76. this._testAssertions.push(this._testAssertions.shift());
  77. if (this._testAssertions.length - this._skippedNum > 0) {
  78. this._run();
  79. } else {
  80. this._drawUIDoneWithSkipped();
  81. }
  82. break;
  83. case 'q':
  84. case _jestWatcher().KEYS.ESCAPE:
  85. this.abort();
  86. break;
  87. case 'r':
  88. this.restart();
  89. break;
  90. case _jestWatcher().KEYS.ENTER:
  91. if (this._testAssertions.length === 0) {
  92. this.abort();
  93. } else {
  94. this._run();
  95. }
  96. break;
  97. default:
  98. }
  99. }
  100. run(failedTestAssertions, updateConfig) {
  101. if (failedTestAssertions.length === 0) return;
  102. this._testAssertions = [...failedTestAssertions];
  103. this._countPaths = this._testAssertions.length;
  104. this._updateTestRunnerConfig = updateConfig;
  105. this._isActive = true;
  106. this._run();
  107. }
  108. updateWithResults(results) {
  109. if (!results.snapshot.failure && results.numFailedTests > 0) {
  110. return this._drawUIOverlay();
  111. }
  112. this._testAssertions.shift();
  113. if (this._testAssertions.length === 0) {
  114. return this._drawUIOverlay();
  115. } // Go to the next test
  116. return this._run();
  117. }
  118. _clearTestSummary() {
  119. this._pipe.write(_ansiEscapes().default.cursorUp(6));
  120. this._pipe.write(_ansiEscapes().default.eraseDown);
  121. }
  122. _drawUIDone() {
  123. this._pipe.write(CLEAR);
  124. const messages = [
  125. _chalk().default.bold('Watch Usage'),
  126. describeKey('Enter', 'to return to watch mode.')
  127. ];
  128. this._pipe.write(messages.join('\n') + '\n');
  129. }
  130. _drawUIDoneWithSkipped() {
  131. this._pipe.write(CLEAR);
  132. let stats = `${(0, _jestUtil().pluralize)(
  133. 'test',
  134. this._countPaths
  135. )} reviewed`;
  136. if (this._skippedNum > 0) {
  137. const skippedText = _chalk().default.bold.yellow(
  138. (0, _jestUtil().pluralize)('test', this._skippedNum) + ' skipped'
  139. );
  140. stats = `${stats}, ${skippedText}`;
  141. }
  142. const message = [
  143. TestProgressLabel,
  144. `${ARROW}${stats}`,
  145. '\n',
  146. _chalk().default.bold('Watch Usage'),
  147. describeKey('r', 'to restart Interactive Mode.'),
  148. describeKey('q', 'to quit Interactive Mode.'),
  149. describeKey('Enter', 'to return to watch mode.')
  150. ];
  151. this._pipe.write(`\n${message.join('\n')}`);
  152. }
  153. _drawUIProgress() {
  154. this._clearTestSummary();
  155. const numPass = this._countPaths - this._testAssertions.length;
  156. const numRemaining = this._countPaths - numPass - this._skippedNum;
  157. let stats = `${(0, _jestUtil().pluralize)('test', numRemaining)} remaining`;
  158. if (this._skippedNum > 0) {
  159. const skippedText = _chalk().default.bold.yellow(
  160. (0, _jestUtil().pluralize)('test', this._skippedNum) + ' skipped'
  161. );
  162. stats = `${stats}, ${skippedText}`;
  163. }
  164. const message = [
  165. TestProgressLabel,
  166. `${ARROW}${stats}`,
  167. '\n',
  168. _chalk().default.bold('Watch Usage'),
  169. describeKey('s', 'to skip the current test.'),
  170. describeKey('q', 'to quit Interactive Mode.'),
  171. describeKey('Enter', 'to return to watch mode.')
  172. ];
  173. this._pipe.write(`\n${message.join('\n')}`);
  174. }
  175. _drawUIOverlay() {
  176. if (this._testAssertions.length === 0) return this._drawUIDone();
  177. return this._drawUIProgress();
  178. }
  179. _run() {
  180. if (this._updateTestRunnerConfig) {
  181. this._updateTestRunnerConfig(this._testAssertions[0]);
  182. }
  183. }
  184. abort() {
  185. this._isActive = false;
  186. this._skippedNum = 0;
  187. if (this._updateTestRunnerConfig) {
  188. this._updateTestRunnerConfig();
  189. }
  190. }
  191. restart() {
  192. this._skippedNum = 0;
  193. this._countPaths = this._testAssertions.length;
  194. this._run();
  195. }
  196. }
  197. exports.default = FailedTestsInteractiveMode;