UNPKG

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