-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathsocket-handler.js
92 lines (85 loc) · 3.08 KB
/
socket-handler.js
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
import chalk from 'chalk';
import { debug, info } from '../utils/debug.js';
import { sinceStart } from '../utils/time.js';
import { watchAssets } from './watch.js';
export function buildHandler(config, state) {
const Connections = new Set();
if (config.serve && !config.noWatch) {
watchAssets(config.assets, () => {
Connections.forEach((ws) => {
ws.send(JSON.stringify({ name: 'reload' }));
});
});
}
return {
perMessageDeflate: true,
async message(ws, message) {
const msg = JSON.parse(message);
msg.launcher = state.browsers.get(msg.browserId).launcher;
info(`${chalk.green('➡')} [${chalk.cyan(msg.browserId)}/${chalk.cyan(msg.windowId)}] ${chalk.green(msg.name)}`);
switch (msg.name) {
case 'suite-start':
if (!state.started) {
state.started = true;
config.reporter.onRunStart(msg);
}
config.reporter.onSuiteStart(msg);
break;
case 'test-start':
config.reporter.onTestStart(msg);
break;
case 'test-finish':
config.reporter.onTestFinish(msg);
break;
case 'suite-finish':
config.reporter.onSuiteFinish(msg);
if (!config.serve) {
ws.send(JSON.stringify({ name: 'close' }));
ws.close();
}
state.completed++;
debug(
`${chalk.green('✅ [Complete]')} ${chalk.cyan(msg.browserId)}/${chalk.cyan(msg.windowId)} ${chalk.yellow(
'@' + sinceStart()
)}`
);
if (state.completed === state.expected) {
const exitCode = config.reporter.onRunFinish(msg);
debug(`${chalk.green('✅ [All Complete]')} ${chalk.yellow('@' + sinceStart())}`);
if (!config.serve) {
state.browsers.forEach((browser) => {
browser.proc.kill();
browser.proc.unref();
});
state.server.stop();
if (config.cleanup) {
debug(`Running configured cleanup hook`);
await config.cleanup();
debug(`Configured cleanup hook completed`);
}
debug(`\n\nExiting with code ${exitCode}`);
// 1. We expect all cleanup to have happened after
// config.cleanup(), so exiting here should be safe.
// 2. We also want to forcibly exit with a success code in this
// case.
// eslint-disable-next-line n/no-process-exit
process.exit(exitCode);
}
} else {
console.log(`Waiting for ${state.expected - state.completed} more browsers to finish`);
}
break;
}
// console.log(JSON.parse(message));
}, // a message is received
open(ws) {
Connections.add(ws);
debug(`WebSocket opened`);
}, // a socket is opened
close(ws, code, message) {
Connections.delete(ws);
debug(`WebSocket closed`);
}, // a socket is closed
drain(ws) {}, // the socket is ready to receive more data
};
}