-
Notifications
You must be signed in to change notification settings - Fork 172
/
Copy pathRunner.ts
238 lines (205 loc) · 6.23 KB
/
Runner.ts
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
import {
Runner,
TestReconciler,
JestTotalResults,
TestFileAssertionStatus
} from "jest-editor-support";
import * as Rx from "rxjs";
import { observable, autorun } from "mobx";
import { createProcess } from "../util/Process";
import { executeInSequence } from "../util/jest";
export interface TestExecutionResults {
totalResult: JestTotalResults;
testFileAssertions: TestFileAssertionStatus[];
}
export default class TestRunner {
@observable isRunning: boolean = false;
@observable isWatchMode: boolean = true;
@observable isWatching: boolean = false;
@observable testFileNamePattern: string = "";
@observable testNamePattern: string = "";
@observable displayText: string = "";
runner: Runner;
reconciler: TestReconciler;
executableJSONEmitter: Rx.Subject<TestExecutionResults>;
rootPath: string;
pathToJest: string;
localJestMajorVersion: number;
pathToConfig: string;
constructor({ rootPath, pathToJest }) {
this.executableJSONEmitter = new Rx.Subject<TestExecutionResults>();
this.rootPath = rootPath;
this.pathToJest = pathToJest;
this.reconciler = new TestReconciler();
}
initializeRunner({ testFileNamePattern, testNamePattern }) {
this.runner = new Runner(
{
rootPath: this.rootPath,
pathToJest: this.pathToJest,
localJestMajorVersion: this.localJestMajorVersion,
pathToConfig: ""
},
{
testFileNamePattern,
testNamePattern,
createProcess
}
);
this.runner
.on("executableJSON", (data: JestTotalResults) => {
const results = this.reconciler.updateFileWithJestStatus(data);
this.executableJSONEmitter.next({
totalResult: data,
testFileAssertions: results
});
this.isRunning = false;
this.setDisplayText();
console.log("executableJSON", data);
})
.on("debuggerProcessExit", () => {
this.isRunning = false;
this.isWatching = false;
this.setDisplayText();
})
.on("executableOutput", output => {
console.log("executableOutput", output);
})
.on("executableStdErr", error => {
console.log("executableStdErr", error.toString());
})
.on("nonTerminalError", error => {
console.log("nonTerminalError", error);
})
.on("exception", result => {
console.log("exception", result);
})
.on("terminalError", (error: Buffer) => {
const message = error.toString();
const noANSI = message.replace(
/[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g,
""
);
console.log("terminalError", message);
if (noANSI.includes("snapshot test failed")) {
console.log("terminalError", message);
}
});
}
getExecutableJSONEmitter() {
return this.executableJSONEmitter;
}
toggleWatchModel(toggle = null) {
this.isWatchMode = toggle || !this.isWatchMode;
}
start(testFileNamePattern: string = "", testNamePattern: string = "") {
if (this.isRunning) {
return this.rerunAllTests();
}
if (this.isWatchMode) {
this.isWatching = true;
}
this.initializeRunner({
testFileNamePattern,
testNamePattern
});
this.isRunning = true;
this.runner.start(this.isWatchMode);
this.setDisplayText();
}
updateSnapshot(testName: string) {
return new Promise((resolve, reject) => {
this.runner.runJestWithUpdateForSnapshots(
() => {
resolve();
},
["--testNamePattern", testName]
);
});
}
filterByTestFileName(testFileNamePattern: string) {
this.setTestFilterPatterns(testFileNamePattern, "");
if (this.isWatching) {
this.interactiveFilterByFileName(this.testFileNamePattern);
} else {
this.start(this.testFileNamePattern, this.testNamePattern);
}
this.setDisplayText();
}
filterByTestName(testFileNamePattern: string, testNamePattern: string) {
this.setTestFilterPatterns(testFileNamePattern, testNamePattern);
if (this.isWatching) {
this.interactiveFilterByTestName(this.testNamePattern);
} else {
// if process is not running... start a new one
this.start(this.testFileNamePattern, this.testNamePattern);
}
this.setDisplayText();
}
terminate() {
(this.runner as any).debugprocess.kill();
this.isRunning = false;
if (this.isWatchMode) {
this.isWatching = false;
}
this.setDisplayText();
}
private setTestFilterPatterns(fileName, testName) {
this.testFileNamePattern = fileName !== "" ? `^${fileName}$` : "";
this.testNamePattern = testName !== "" ? `^${testName}$` : "";
}
private setDisplayText() {
this.displayText = "";
if (this.isRunning) {
this.displayText = "Booting jest";
} else if (this.isWatching) {
if (this.testFileNamePattern && this.testNamePattern) {
this.displayText = `Watching ${this.testNamePattern} in ${
this.testFileNamePattern
}`;
} else if (this.testFileNamePattern && !this.testNamePattern) {
this.displayText = `Watching tests in file ${this.testFileNamePattern}`;
} else {
this.displayText = "Watching for changes";
}
}
}
private rerunAllTests() {
const debugProcess = (this.runner as any).debugprocess;
debugProcess.stdin.write("a");
}
private interactiveFilterByTestName(testNameRegex: string) {
const debugProcess = (this.runner as any).debugprocess;
executeInSequence([
{
fn: () => debugProcess.stdin.write("t"),
delay: 0
},
{
fn: () => debugProcess.stdin.write(testNameRegex),
delay: 100
},
{
fn: () => debugProcess.stdin.write(new Buffer("0d", "hex").toString()),
delay: 200
}
]);
}
private interactiveFilterByFileName(testFileNameRegex: string) {
const debugProcess = (this.runner as any).debugprocess;
executeInSequence([
{
fn: () => debugProcess.stdin.write("p"),
delay: 0
},
{
fn: () => debugProcess.stdin.write(testFileNameRegex),
delay: 100
},
{
fn: () => debugProcess.stdin.write(new Buffer("0d", "hex").toString()),
delay: 200
}
]);
}
}