-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathindex.js
74 lines (62 loc) · 1.64 KB
/
index.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
const LOG_TAG = '[cypress-hmr-restarter]';
Cypress.on('window:load', (win) => {
if (!Cypress.config('isInteractive')) {
return;
}
const delay = Cypress.config('hmrRestartDelay') || 1500;
const url = getUrl();
const socket = new WebSocket(url);
let timeout;
socket.onopen = () => console.debug(LOG_TAG, 'Connected to HMR socket');
socket.onclose = () => console.debug(LOG_TAG, 'Disconnected from HMR socket');
socket.onmessage = (e) => {
let event;
try {
event = JSON.parse(e.data);
} catch (err) {
console.debug(
LOG_TAG,
`Failed to parse event data.`,
`\nError:`,
err.message,
`\nData:`,
e.data
);
return;
}
switch (event.type) {
case 'invalid':
console.debug(LOG_TAG, `Restarting due to HMR in ${delay}ms...`);
clickStop(win);
clearTimeout(timeout);
timeout = setTimeout(() => clickRestart(win), delay);
break;
}
};
});
function getUrl() {
const url = Cypress.config('hmrUrl');
if (url) {
return url;
}
const baseUrl = Cypress.config('baseUrl');
if (baseUrl) {
return baseUrl.replace(/^http(s?)/, 'ws$1') + '/sockjs-node';
}
throw new Error(
`${LOG_TAG} Need endpoint to connect to. Add either \`baseUrl\` or \`hmrUrl\` to \`cypress.json\`.`
);
}
function clickStop(win) {
click(win, 'stop', 'Stopped running tests.');
}
function clickRestart(win) {
click(win, 'restart', 'Restarted.');
}
function click(win, btnClass, log) {
const btn = win.top.document.querySelector(`.reporter .${btnClass}`);
if (btn) {
btn.click();
console.debug(LOG_TAG, log);
}
}