UNPKG

7.41 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
68class SnapshotInteractiveMode {
69 constructor(pipe) {
70 _defineProperty(this, '_pipe', void 0);
71
72 _defineProperty(this, '_isActive', void 0);
73
74 _defineProperty(this, '_updateTestRunnerConfig', void 0);
75
76 _defineProperty(this, '_testAssertions', void 0);
77
78 _defineProperty(this, '_countPaths', void 0);
79
80 _defineProperty(this, '_skippedNum', void 0);
81
82 this._pipe = pipe;
83 this._isActive = false;
84 this._skippedNum = 0;
85 }
86
87 isActive() {
88 return this._isActive;
89 }
90
91 getSkippedNum() {
92 return this._skippedNum;
93 }
94
95 _clearTestSummary() {
96 this._pipe.write(_ansiEscapes().default.cursorUp(6));
97
98 this._pipe.write(_ansiEscapes().default.eraseDown);
99 }
100
101 _drawUIProgress() {
102 this._clearTestSummary();
103
104 const numPass = this._countPaths - this._testAssertions.length;
105 const numRemaining = this._countPaths - numPass - this._skippedNum;
106
107 let stats = _chalk().default.bold.dim(
108 (0, _jestUtil().pluralize)('snapshot', numRemaining) + ' remaining'
109 );
110
111 if (numPass) {
112 stats +=
113 ', ' +
114 _chalk().default.bold.green(
115 (0, _jestUtil().pluralize)('snapshot', numPass) + ' updated'
116 );
117 }
118
119 if (this._skippedNum) {
120 stats +=
121 ', ' +
122 _chalk().default.bold.yellow(
123 (0, _jestUtil().pluralize)('snapshot', this._skippedNum) + ' skipped'
124 );
125 }
126
127 const messages = [
128 '\n' + _chalk().default.bold('Interactive Snapshot Progress'),
129 ARROW + stats,
130 '\n' + _chalk().default.bold('Watch Usage'),
131 _chalk().default.dim(ARROW + 'Press ') +
132 'u' +
133 _chalk().default.dim(' to update failing snapshots for this test.'),
134 _chalk().default.dim(ARROW + 'Press ') +
135 's' +
136 _chalk().default.dim(' to skip the current test.'),
137 _chalk().default.dim(ARROW + 'Press ') +
138 'q' +
139 _chalk().default.dim(' to quit Interactive Snapshot Mode.'),
140 _chalk().default.dim(ARROW + 'Press ') +
141 'Enter' +
142 _chalk().default.dim(' to trigger a test run.')
143 ];
144
145 this._pipe.write(messages.filter(Boolean).join('\n') + '\n');
146 }
147
148 _drawUIDoneWithSkipped() {
149 this._pipe.write(CLEAR);
150
151 const numPass = this._countPaths - this._testAssertions.length;
152
153 let stats = _chalk().default.bold.dim(
154 (0, _jestUtil().pluralize)('snapshot', this._countPaths) + ' reviewed'
155 );
156
157 if (numPass) {
158 stats +=
159 ', ' +
160 _chalk().default.bold.green(
161 (0, _jestUtil().pluralize)('snapshot', numPass) + ' updated'
162 );
163 }
164
165 if (this._skippedNum) {
166 stats +=
167 ', ' +
168 _chalk().default.bold.yellow(
169 (0, _jestUtil().pluralize)('snapshot', this._skippedNum) + ' skipped'
170 );
171 }
172
173 const messages = [
174 '\n' + _chalk().default.bold('Interactive Snapshot Result'),
175 ARROW + stats,
176 '\n' + _chalk().default.bold('Watch Usage'),
177 _chalk().default.dim(ARROW + 'Press ') +
178 'r' +
179 _chalk().default.dim(' to restart Interactive Snapshot Mode.'),
180 _chalk().default.dim(ARROW + 'Press ') +
181 'q' +
182 _chalk().default.dim(' to quit Interactive Snapshot Mode.')
183 ];
184
185 this._pipe.write(messages.filter(Boolean).join('\n') + '\n');
186 }
187
188 _drawUIDone() {
189 this._pipe.write(CLEAR);
190
191 const numPass = this._countPaths - this._testAssertions.length;
192
193 let stats = _chalk().default.bold.dim(
194 (0, _jestUtil().pluralize)('snapshot', this._countPaths) + ' reviewed'
195 );
196
197 if (numPass) {
198 stats +=
199 ', ' +
200 _chalk().default.bold.green(
201 (0, _jestUtil().pluralize)('snapshot', numPass) + ' updated'
202 );
203 }
204
205 const messages = [
206 '\n' + _chalk().default.bold('Interactive Snapshot Result'),
207 ARROW + stats,
208 '\n' + _chalk().default.bold('Watch Usage'),
209 _chalk().default.dim(ARROW + 'Press ') +
210 'Enter' +
211 _chalk().default.dim(' to return to watch mode.')
212 ];
213
214 this._pipe.write(messages.filter(Boolean).join('\n') + '\n');
215 }
216
217 _drawUIOverlay() {
218 if (this._testAssertions.length === 0) {
219 return this._drawUIDone();
220 }
221
222 if (this._testAssertions.length - this._skippedNum === 0) {
223 return this._drawUIDoneWithSkipped();
224 }
225
226 return this._drawUIProgress();
227 }
228
229 put(key) {
230 switch (key) {
231 case 's':
232 if (this._skippedNum === this._testAssertions.length) break;
233 this._skippedNum += 1; // move skipped test to the end
234
235 this._testAssertions.push(this._testAssertions.shift());
236
237 if (this._testAssertions.length - this._skippedNum > 0) {
238 this._run(false);
239 } else {
240 this._drawUIDoneWithSkipped();
241 }
242
243 break;
244
245 case 'u':
246 this._run(true);
247
248 break;
249
250 case 'q':
251 case _jestWatcher().KEYS.ESCAPE:
252 this.abort();
253 break;
254
255 case 'r':
256 this.restart();
257 break;
258
259 case _jestWatcher().KEYS.ENTER:
260 if (this._testAssertions.length === 0) {
261 this.abort();
262 } else {
263 this._run(false);
264 }
265
266 break;
267
268 default:
269 break;
270 }
271 }
272
273 abort() {
274 this._isActive = false;
275 this._skippedNum = 0;
276
277 this._updateTestRunnerConfig(null, false);
278 }
279
280 restart() {
281 this._skippedNum = 0;
282 this._countPaths = this._testAssertions.length;
283
284 this._run(false);
285 }
286
287 updateWithResults(results) {
288 const hasSnapshotFailure = !!results.snapshot.failure;
289
290 if (hasSnapshotFailure) {
291 this._drawUIOverlay();
292
293 return;
294 }
295
296 this._testAssertions.shift();
297
298 if (this._testAssertions.length - this._skippedNum === 0) {
299 this._drawUIOverlay();
300
301 return;
302 } // Go to the next test
303
304 this._run(false);
305 }
306
307 _run(shouldUpdateSnapshot) {
308 const testAssertion = this._testAssertions[0];
309
310 this._updateTestRunnerConfig(testAssertion, shouldUpdateSnapshot);
311 }
312
313 run(failedSnapshotTestAssertions, onConfigChange) {
314 if (!failedSnapshotTestAssertions.length) {
315 return;
316 }
317
318 this._testAssertions = [...failedSnapshotTestAssertions];
319 this._countPaths = this._testAssertions.length;
320 this._updateTestRunnerConfig = onConfigChange;
321 this._isActive = true;
322
323 this._run(false);
324 }
325}
326
327exports.default = SnapshotInteractiveMode;