UNPKG

5.65 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, '__esModule', {
4 value: true
5});
6exports.default = void 0;
7
8function _ansiEscapes() {
9 const data = _interopRequireDefault(require('ansi-escapes'));
10
11 _ansiEscapes = function () {
12 return data;
13 };
14
15 return data;
16}
17
18function _chalk() {
19 const data = _interopRequireDefault(require('chalk'));
20
21 _chalk = function () {
22 return data;
23 };
24
25 return data;
26}
27
28function _jestUtil() {
29 const data = require('jest-util');
30
31 _jestUtil = function () {
32 return data;
33 };
34
35 return data;
36}
37
38function _jestWatcher() {
39 const data = require('jest-watcher');
40
41 _jestWatcher = function () {
42 return data;
43 };
44
45 return data;
46}
47
48function _interopRequireDefault(obj) {
49 return obj && obj.__esModule ? obj : {default: obj};
50}
51
52function _defineProperty(obj, key, value) {
53 if (key in obj) {
54 Object.defineProperty(obj, key, {
55 value: value,
56 enumerable: true,
57 configurable: true,
58 writable: true
59 });
60 } else {
61 obj[key] = value;
62 }
63 return obj;
64}
65
66const {ARROW, CLEAR} = _jestUtil().specialChars;
67
68function describeKey(key, description) {
69 return `${_chalk().default.dim(
70 ARROW + 'Press'
71 )} ${key} ${_chalk().default.dim(description)}`;
72}
73
74const TestProgressLabel = _chalk().default.bold('Interactive Test Progress');
75
76class FailedTestsInteractiveMode {
77 constructor(_pipe) {
78 this._pipe = _pipe;
79
80 _defineProperty(this, '_isActive', false);
81
82 _defineProperty(this, '_countPaths', 0);
83
84 _defineProperty(this, '_skippedNum', 0);
85
86 _defineProperty(this, '_testAssertions', []);
87
88 _defineProperty(this, '_updateTestRunnerConfig', void 0);
89 }
90
91 isActive() {
92 return this._isActive;
93 }
94
95 put(key) {
96 switch (key) {
97 case 's':
98 if (this._skippedNum === this._testAssertions.length) {
99 break;
100 }
101
102 this._skippedNum += 1; // move skipped test to the end
103
104 this._testAssertions.push(this._testAssertions.shift());
105
106 if (this._testAssertions.length - this._skippedNum > 0) {
107 this._run();
108 } else {
109 this._drawUIDoneWithSkipped();
110 }
111
112 break;
113
114 case 'q':
115 case _jestWatcher().KEYS.ESCAPE:
116 this.abort();
117 break;
118
119 case 'r':
120 this.restart();
121 break;
122
123 case _jestWatcher().KEYS.ENTER:
124 if (this._testAssertions.length === 0) {
125 this.abort();
126 } else {
127 this._run();
128 }
129
130 break;
131
132 default:
133 }
134 }
135
136 run(failedTestAssertions, updateConfig) {
137 if (failedTestAssertions.length === 0) return;
138 this._testAssertions = [...failedTestAssertions];
139 this._countPaths = this._testAssertions.length;
140 this._updateTestRunnerConfig = updateConfig;
141 this._isActive = true;
142
143 this._run();
144 }
145
146 updateWithResults(results) {
147 if (!results.snapshot.failure && results.numFailedTests > 0) {
148 return this._drawUIOverlay();
149 }
150
151 this._testAssertions.shift();
152
153 if (this._testAssertions.length === 0) {
154 return this._drawUIOverlay();
155 } // Go to the next test
156
157 return this._run();
158 }
159
160 _clearTestSummary() {
161 this._pipe.write(_ansiEscapes().default.cursorUp(6));
162
163 this._pipe.write(_ansiEscapes().default.eraseDown);
164 }
165
166 _drawUIDone() {
167 this._pipe.write(CLEAR);
168
169 const messages = [
170 _chalk().default.bold('Watch Usage'),
171 describeKey('Enter', 'to return to watch mode.')
172 ];
173
174 this._pipe.write(messages.join('\n') + '\n');
175 }
176
177 _drawUIDoneWithSkipped() {
178 this._pipe.write(CLEAR);
179
180 let stats = `${(0, _jestUtil().pluralize)(
181 'test',
182 this._countPaths
183 )} reviewed`;
184
185 if (this._skippedNum > 0) {
186 const skippedText = _chalk().default.bold.yellow(
187 (0, _jestUtil().pluralize)('test', this._skippedNum) + ' skipped'
188 );
189
190 stats = `${stats}, ${skippedText}`;
191 }
192
193 const message = [
194 TestProgressLabel,
195 `${ARROW}${stats}`,
196 '\n',
197 _chalk().default.bold('Watch Usage'),
198 describeKey('r', 'to restart Interactive Mode.'),
199 describeKey('q', 'to quit Interactive Mode.'),
200 describeKey('Enter', 'to return to watch mode.')
201 ];
202
203 this._pipe.write(`\n${message.join('\n')}`);
204 }
205
206 _drawUIProgress() {
207 this._clearTestSummary();
208
209 const numPass = this._countPaths - this._testAssertions.length;
210 const numRemaining = this._countPaths - numPass - this._skippedNum;
211 let stats = `${(0, _jestUtil().pluralize)('test', numRemaining)} remaining`;
212
213 if (this._skippedNum > 0) {
214 const skippedText = _chalk().default.bold.yellow(
215 (0, _jestUtil().pluralize)('test', this._skippedNum) + ' skipped'
216 );
217
218 stats = `${stats}, ${skippedText}`;
219 }
220
221 const message = [
222 TestProgressLabel,
223 `${ARROW}${stats}`,
224 '\n',
225 _chalk().default.bold('Watch Usage'),
226 describeKey('s', 'to skip the current test.'),
227 describeKey('q', 'to quit Interactive Mode.'),
228 describeKey('Enter', 'to return to watch mode.')
229 ];
230
231 this._pipe.write(`\n${message.join('\n')}`);
232 }
233
234 _drawUIOverlay() {
235 if (this._testAssertions.length === 0) return this._drawUIDone();
236 return this._drawUIProgress();
237 }
238
239 _run() {
240 if (this._updateTestRunnerConfig) {
241 this._updateTestRunnerConfig(this._testAssertions[0]);
242 }
243 }
244
245 abort() {
246 this._isActive = false;
247 this._skippedNum = 0;
248
249 if (this._updateTestRunnerConfig) {
250 this._updateTestRunnerConfig();
251 }
252 }
253
254 restart() {
255 this._skippedNum = 0;
256 this._countPaths = this._testAssertions.length;
257
258 this._run();
259 }
260}
261
262exports.default = FailedTestsInteractiveMode;