-
-
Notifications
You must be signed in to change notification settings - Fork 6.6k
Expand file tree
/
Copy pathSnapshotInteractiveMode.js
More file actions
138 lines (116 loc) · 3.61 KB
/
SnapshotInteractiveMode.js
File metadata and controls
138 lines (116 loc) · 3.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/**
* @flow
*/
import type {AggregatedResult} from 'types/TestResult';
const chalk = require('chalk');
const ansiEscapes = require('ansi-escapes');
const {pluralize} = require('./reporters/utils');
const {rightPad} = require('./lib/terminalUtils');
const {KEYS} = require('./constants');
module.exports = class SnapshotInteractiveMode {
_pipe: stream$Writable | tty$WriteStream;
_isActive: boolean;
_updateTestRunnerConfig: (a: string, jestRunnerOptions: Object) => *;
_testFilePaths: Array<string>;
_countPaths: number;
constructor(pipe: stream$Writable | tty$WriteStream) {
this._pipe = pipe;
this._isActive = false;
}
isActive() {
return this._isActive;
}
_drawUIOverlay() {
this._pipe.write(ansiEscapes.scrollDown);
this._pipe.write(ansiEscapes.scrollDown);
this._pipe.write(ansiEscapes.cursorSavePosition);
this._pipe.write(ansiEscapes.cursorTo(0, 0));
const title = rightPad(' -> Interactive Snapshot Update Activated <-');
this._pipe.write(chalk.black.bold.bgYellow(title));
this._pipe.write(ansiEscapes.cursorRestorePosition);
this._pipe.write(ansiEscapes.cursorUp(6));
this._pipe.write(ansiEscapes.eraseDown);
const numFailed = this._testFilePaths.length;
const numPass = this._countPaths - this._testFilePaths.length;
let stats = chalk.bold.red(pluralize('suite', numFailed) + ' failed');
if (numPass) {
stats += ', ' + chalk.bold.green(pluralize('suite', numPass) + ' passed');
}
const messages = [
'\n' + chalk.bold('Interactive Snapshot Progress'),
' \u203A ' + stats,
'\n' + chalk.bold('Watch Usage'),
chalk.dim(' \u203A Press ') +
'u' +
chalk.dim(' to update failing snapshots.'),
this._testFilePaths.length > 1
? chalk.dim(' \u203A Press ') +
's' +
chalk.dim(' to skip the current snapshot..')
: '',
chalk.dim(' \u203A Press ') +
'q' +
chalk.dim(' to quit interactive snapshot mode.'),
chalk.dim(' \u203A Press ') +
'Enter' +
chalk.dim(' to trigger a test run.'),
];
this._pipe.write(messages.filter(message => !!message).join('\n') + '\n');
}
put(key: string) {
switch (key) {
case KEYS.S:
const testFilePath = this._testFilePaths.shift();
this._testFilePaths.push(testFilePath);
this._run({});
break;
case KEYS.U:
this._run({updateSnapshot: 'all'});
break;
case KEYS.Q:
case KEYS.ESCAPE:
this.abort();
break;
case KEYS.ENTER:
this._run({});
break;
default:
console.log('got key event', key);
break;
}
}
abort() {
this._isActive = false;
this._updateTestRunnerConfig('', {});
}
updateWithResults(results: AggregatedResult) {
const hasSnapshotFailure = !!results.snapshot.failure;
if (hasSnapshotFailure) {
this._drawUIOverlay();
return;
}
this._testFilePaths.shift();
if (this._testFilePaths.length === 0) {
this.abort();
return;
}
this._run({});
}
_run(jestRunnerOptions: Object) {
const testFilePath = this._testFilePaths[0];
this._updateTestRunnerConfig(testFilePath, jestRunnerOptions);
}
run(
failedSnapshotTestPaths: Array<string>,
onConfigChange: (path: string, jestRunnerOptions: Object) => *,
) {
if (!failedSnapshotTestPaths.length) {
return;
}
this._testFilePaths = [].concat(failedSnapshotTestPaths);
this._countPaths = this._testFilePaths.length;
this._updateTestRunnerConfig = onConfigChange;
this._isActive = true;
this._run({});
}
};