-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathgatsby.js
78 lines (66 loc) · 1.74 KB
/
gatsby.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
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 source = new EventSource(url);
let timeout;
source.onopen = () => console.debug(LOG_TAG, 'Connected to HMR event source');
source.addEventListener('message', function (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.action) {
case 'built':
console.debug(LOG_TAG, `Restarting due to HMR in ${delay}ms...`);
clickStop(win);
clearTimeout(timeout);
timeout = setTimeout(() => clickRestart(win), delay);
break;
}
});
win.onbeforeunload = () => {
// Ensure that the source is closed if the window is unloaded
console.debug(LOG_TAG, `Close the HMR Connection`);
source.close();
}
});
function getUrl() {
const url = Cypress.config('hmrUrl');
if (url) {
return url;
}
const baseUrl = Cypress.config('baseUrl');
if (baseUrl) {
return baseUrl + '/__webpack_hmr';
}
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);
}
}