/
watchPlugin.ts
66 lines (52 loc) 路 1.56 KB
/
watchPlugin.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
import { AddressInfo, createServer, Server, Socket } from 'net';
// import split from 'split';
class WatchPlugin {
private _ready: Promise<void>;
private _server: Server;
private _socket: Socket;
constructor() {
this._server = createServer((socket) => {
this._setSocket(socket);
});
this._ready = new Promise((resolve) => {
this._server.listen(0, () => resolve());
});
}
_setSocket(socket: Socket) {
this._socket = socket;
// this._socket.pipe(split()).on('data', (data: string) => {
// debug('received: %s', data);
// try {
// const message = JSON.parse(data);
// // if (message.name === 'codeupdate') {
// // this.emit('codeupdate', message.code);
// // } else if (message.name === 'stopped') {
// // this.emit('stopped');
// // } else if (message.name === 'stoprunner') {
// // this.emit('stoprunner');
// // }
// } catch (e) {
// // ignore non JSON messages (last empty message)
// }
// });
}
apply(jestHooks) {
jestHooks.shouldRunTestSuite(async () => {
await this._ready;
const port = (this._server.address() as AddressInfo).port;
process.env.QAW_RUN_SERVER_PORT = `${port}`;
if (this._socket) {
try {
this._socket.write(JSON.stringify({ name: 'stop' }) + '\n');
} catch (e) {}
}
return true;
});
jestHooks.onTestRunComplete(() => {
// console.log('results', results);
});
}
getUsageInfo() {}
run() {}
}
module.exports = WatchPlugin;