-
-
Notifications
You must be signed in to change notification settings - Fork 6.6k
Expand file tree
/
Copy pathRunner.js
More file actions
178 lines (152 loc) · 5.06 KB
/
Runner.js
File metadata and controls
178 lines (152 loc) · 5.06 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
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
/**
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/
import type {Options, MessageType, SpawnOptions} from './types';
import {messageTypes} from './types';
import {ChildProcess, spawn} from 'child_process';
import {readFile} from 'fs';
import {tmpdir} from 'os';
import EventEmitter from 'events';
import ProjectWorkspace from './project_workspace';
import {createProcess} from './Process';
// This class represents the running process, and
// passes out events when it understands what data is being
// pass sent out of the process
export default class Runner extends EventEmitter {
debugprocess: ChildProcess;
outputPath: string;
workspace: ProjectWorkspace;
_createProcess: (
workspace: ProjectWorkspace,
args: Array<string>,
options?: SpawnOptions,
) => ChildProcess;
watchMode: boolean;
options: Options;
prevMessageTypes: MessageType[];
constructor(workspace: ProjectWorkspace, options?: Options) {
super();
this._createProcess = (options && options.createProcess) || createProcess;
this.options = options || {};
this.workspace = workspace;
this.outputPath = tmpdir() + '/jest_runner.json';
this.prevMessageTypes = [];
}
start(watchMode: boolean = true) {
if (this.debugprocess) {
return;
}
this.watchMode = watchMode;
// Handle the arg change on v18
const belowEighteen = this.workspace.localJestMajorVersion < 18;
const outputArg = belowEighteen ? '--jsonOutputFile' : '--outputFile';
const args = ['--json', '--useStderr', outputArg, this.outputPath];
if (this.watchMode) {
args.push('--watch');
}
if (this.options.testNamePattern) {
args.push('--testNamePattern', this.options.testNamePattern);
}
if (this.options.testFileNamePattern) {
args.push(this.options.testFileNamePattern);
}
const options = {
shell: this.options.shell,
};
this.debugprocess = this._createProcess(this.workspace, args, options);
this.debugprocess.stdout.on('data', (data: Buffer) => {
// Make jest save to a file, otherwise we get chunked data
// and it can be hard to put it back together.
const stringValue = data
.toString()
.replace(/\n$/, '')
.trim();
if (stringValue.startsWith('Test results written to')) {
readFile(this.outputPath, 'utf8', (err, data) => {
if (err) {
const message = `JSON report not found at ${this.outputPath}`;
this.emit('terminalError', message);
} else {
const noTestsFound = this.doResultsFollowNoTestsFoundMessage();
this.emit('executableJSON', JSON.parse(data), {noTestsFound});
}
});
} else {
this.emit('executableOutput', stringValue.replace('[2J[H', ''));
}
this.prevMessageTypes.length = 0;
});
this.debugprocess.stderr.on('data', (data: Buffer) => {
const type = this.findMessageType(data);
if (type === messageTypes.unknown) {
this.prevMessageTypes.length = 0;
} else {
this.prevMessageTypes.push(type);
}
this.emit('executableStdErr', data, {type});
});
this.debugprocess.on('exit', () => {
this.emit('debuggerProcessExit');
this.prevMessageTypes.length = 0;
});
this.debugprocess.on('error', (error: Error) => {
this.emit('terminalError', 'Process failed: ' + error.message);
this.prevMessageTypes.length = 0;
});
this.debugprocess.on('close', () => {
this.emit('debuggerProcessExit');
this.prevMessageTypes.length = 0;
});
}
runJestWithUpdateForSnapshots(completion: any, args: string[]) {
const defaultArgs = ['--updateSnapshot'];
const options = {shell: this.options.shell};
const updateProcess = this._createProcess(
this.workspace,
[...defaultArgs, ...(args ? args : [])],
options,
);
updateProcess.on('close', () => {
completion();
});
}
closeProcess() {
if (!this.debugprocess) {
return;
}
if (process.platform === 'win32') {
// Windows doesn't exit the process when it should.
spawn('taskkill', ['/pid', '' + this.debugprocess.pid, '/T', '/F']);
} else {
this.debugprocess.kill();
}
delete this.debugprocess;
}
findMessageType(buf: Buffer) {
const str = buf.toString('utf8', 0, 58);
if (str === 'No tests found related to files changed since last commit.') {
return messageTypes.noTests;
}
if (/^\s*Watch Usage\b/.test(str)) {
return messageTypes.watchUsage;
}
return messageTypes.unknown;
}
doResultsFollowNoTestsFoundMessage() {
if (this.prevMessageTypes.length === 1) {
return this.prevMessageTypes[0] === messageTypes.noTests;
}
if (this.prevMessageTypes.length === 2) {
return (
this.prevMessageTypes[0] === messageTypes.noTests &&
this.prevMessageTypes[1] === messageTypes.watchUsage
);
}
return false;
}
}