Skip to content

Commit a19587b

Browse files
committed
test: add e2e tests for reconnect
1 parent 3653449 commit a19587b

File tree

4 files changed

+294
-0
lines changed

4 files changed

+294
-0
lines changed

client-src/socket.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/* global __webpack_dev_server_client__ */
22

33
import WebSocketClient from "./clients/WebSocketClient.js";
4+
import { log } from "./utils/log.js";
45

56
// this WebsocketClient is here as a default fallback, in case the client is not injected
67
/* eslint-disable camelcase */
@@ -43,6 +44,8 @@ const socket = function initSocket(url, handlers, reconnect) {
4344

4445
retries += 1;
4546

47+
log.info("Trying to reconnect...");
48+
4649
setTimeout(() => {
4750
socket(url, handlers);
4851
}, retryInMs);
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`client.reconnect option specified as false should not try to reconnect: console messages 1`] = `
4+
Array [
5+
"[HMR] Waiting for update signal from WDS...",
6+
"[webpack-dev-server] Hot Module Replacement enabled.",
7+
"[webpack-dev-server] Live Reloading enabled.",
8+
"[webpack-dev-server] Disconnected!",
9+
]
10+
`;
11+
12+
exports[`client.reconnect option specified as false should not try to reconnect: page errors 1`] = `Array []`;
13+
14+
exports[`client.reconnect option specified as false should not try to reconnect: response status 1`] = `200`;
15+
16+
exports[`client.reconnect option specified as number should try to reconnect 2 times: console messages 1`] = `
17+
Array [
18+
"[HMR] Waiting for update signal from WDS...",
19+
"[webpack-dev-server] Hot Module Replacement enabled.",
20+
"[webpack-dev-server] Live Reloading enabled.",
21+
"[webpack-dev-server] Disconnected!",
22+
"[webpack-dev-server] Trying to reconnect...",
23+
"WebSocket connection to 'ws://127.0.0.1:8106/ws' failed: Error in connection establishment: net::ERR_CONNECTION_REFUSED",
24+
"[webpack-dev-server] JSHandle@object",
25+
"[webpack-dev-server] Trying to reconnect...",
26+
"WebSocket connection to 'ws://127.0.0.1:8106/ws' failed: Error in connection establishment: net::ERR_CONNECTION_REFUSED",
27+
"[webpack-dev-server] JSHandle@object",
28+
]
29+
`;
30+
31+
exports[`client.reconnect option specified as number should try to reconnect 2 times: page errors 1`] = `Array []`;
32+
33+
exports[`client.reconnect option specified as number should try to reconnect 2 times: response status 1`] = `200`;
34+
35+
exports[`client.reconnect option specified as true should try to reconnect unlimited times: console messages 1`] = `
36+
Array [
37+
"[HMR] Waiting for update signal from WDS...",
38+
"[webpack-dev-server] Hot Module Replacement enabled.",
39+
"[webpack-dev-server] Live Reloading enabled.",
40+
"[webpack-dev-server] Disconnected!",
41+
"[webpack-dev-server] Trying to reconnect...",
42+
"WebSocket connection to 'ws://127.0.0.1:8106/ws' failed: Error in connection establishment: net::ERR_CONNECTION_REFUSED",
43+
"[webpack-dev-server] JSHandle@object",
44+
"[webpack-dev-server] Trying to reconnect...",
45+
"WebSocket connection to 'ws://127.0.0.1:8106/ws' failed: Error in connection establishment: net::ERR_CONNECTION_REFUSED",
46+
"[webpack-dev-server] JSHandle@object",
47+
"[webpack-dev-server] Trying to reconnect...",
48+
"WebSocket connection to 'ws://127.0.0.1:8106/ws' failed: Error in connection establishment: net::ERR_CONNECTION_REFUSED",
49+
"[webpack-dev-server] JSHandle@object",
50+
"[webpack-dev-server] Trying to reconnect...",
51+
"WebSocket connection to 'ws://127.0.0.1:8106/ws' failed: Error in connection establishment: net::ERR_CONNECTION_REFUSED",
52+
"[webpack-dev-server] JSHandle@object",
53+
"[webpack-dev-server] Trying to reconnect...",
54+
"WebSocket connection to 'ws://127.0.0.1:8106/ws' failed: Error in connection establishment: net::ERR_CONNECTION_REFUSED",
55+
"[webpack-dev-server] JSHandle@object",
56+
"[webpack-dev-server] Trying to reconnect...",
57+
]
58+
`;
59+
60+
exports[`client.reconnect option specified as true should try to reconnect unlimited times: page errors 1`] = `Array []`;
61+
62+
exports[`client.reconnect option specified as true should try to reconnect unlimited times: response status 1`] = `200`;
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`client.reconnect option specified as false should not try to reconnect: console messages 1`] = `
4+
Array [
5+
"[HMR] Waiting for update signal from WDS...",
6+
"[webpack-dev-server] Hot Module Replacement enabled.",
7+
"[webpack-dev-server] Live Reloading enabled.",
8+
"[webpack-dev-server] Disconnected!",
9+
]
10+
`;
11+
12+
exports[`client.reconnect option specified as false should not try to reconnect: page errors 1`] = `Array []`;
13+
14+
exports[`client.reconnect option specified as false should not try to reconnect: response status 1`] = `200`;
15+
16+
exports[`client.reconnect option specified as number should try to reconnect 2 times: console messages 1`] = `
17+
Array [
18+
"[HMR] Waiting for update signal from WDS...",
19+
"[webpack-dev-server] Hot Module Replacement enabled.",
20+
"[webpack-dev-server] Live Reloading enabled.",
21+
"[webpack-dev-server] Disconnected!",
22+
"[webpack-dev-server] Trying to reconnect...",
23+
"WebSocket connection to 'ws://127.0.0.1:8106/ws' failed: Error in connection establishment: net::ERR_CONNECTION_REFUSED",
24+
"[webpack-dev-server] JSHandle@object",
25+
"[webpack-dev-server] Trying to reconnect...",
26+
"WebSocket connection to 'ws://127.0.0.1:8106/ws' failed: Error in connection establishment: net::ERR_CONNECTION_REFUSED",
27+
"[webpack-dev-server] JSHandle@object",
28+
]
29+
`;
30+
31+
exports[`client.reconnect option specified as number should try to reconnect 2 times: page errors 1`] = `Array []`;
32+
33+
exports[`client.reconnect option specified as number should try to reconnect 2 times: response status 1`] = `200`;
34+
35+
exports[`client.reconnect option specified as true should try to reconnect unlimited times: console messages 1`] = `
36+
Array [
37+
"[HMR] Waiting for update signal from WDS...",
38+
"[webpack-dev-server] Hot Module Replacement enabled.",
39+
"[webpack-dev-server] Live Reloading enabled.",
40+
"[webpack-dev-server] Disconnected!",
41+
"[webpack-dev-server] Trying to reconnect...",
42+
"WebSocket connection to 'ws://127.0.0.1:8106/ws' failed: Error in connection establishment: net::ERR_CONNECTION_REFUSED",
43+
"[webpack-dev-server] JSHandle@object",
44+
"[webpack-dev-server] Trying to reconnect...",
45+
"WebSocket connection to 'ws://127.0.0.1:8106/ws' failed: Error in connection establishment: net::ERR_CONNECTION_REFUSED",
46+
"[webpack-dev-server] JSHandle@object",
47+
"[webpack-dev-server] Trying to reconnect...",
48+
"WebSocket connection to 'ws://127.0.0.1:8106/ws' failed: Error in connection establishment: net::ERR_CONNECTION_REFUSED",
49+
"[webpack-dev-server] JSHandle@object",
50+
"[webpack-dev-server] Trying to reconnect...",
51+
"WebSocket connection to 'ws://127.0.0.1:8106/ws' failed: Error in connection establishment: net::ERR_CONNECTION_REFUSED",
52+
"[webpack-dev-server] JSHandle@object",
53+
"[webpack-dev-server] Trying to reconnect...",
54+
"WebSocket connection to 'ws://127.0.0.1:8106/ws' failed: Error in connection establishment: net::ERR_CONNECTION_REFUSED",
55+
"[webpack-dev-server] JSHandle@object",
56+
"[webpack-dev-server] Trying to reconnect...",
57+
]
58+
`;
59+
60+
exports[`client.reconnect option specified as true should try to reconnect unlimited times: page errors 1`] = `Array []`;
61+
62+
exports[`client.reconnect option specified as true should try to reconnect unlimited times: response status 1`] = `200`;

test/e2e/client-reconnect.test.js

Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
"use strict";
2+
3+
const webpack = require("webpack");
4+
const Server = require("../../lib/Server");
5+
const config = require("../fixtures/reload-config/webpack.config");
6+
const runBrowser = require("../helpers/run-browser");
7+
const port = require("../ports-map")["compress-option"];
8+
9+
describe("client.reconnect option", () => {
10+
describe("specified as true", () => {
11+
let compiler;
12+
let server;
13+
let page;
14+
let browser;
15+
let pageErrors;
16+
let consoleMessages;
17+
18+
beforeEach(async () => {
19+
compiler = webpack(config);
20+
21+
server = new Server({ port, client: { reconnect: true } }, compiler);
22+
23+
await server.start();
24+
25+
({ page, browser } = await runBrowser());
26+
27+
pageErrors = [];
28+
consoleMessages = [];
29+
});
30+
31+
afterEach(async () => {
32+
await browser.close();
33+
});
34+
35+
it("should try to reconnect unlimited times", async () => {
36+
page
37+
.on("console", (message) => {
38+
consoleMessages.push(message);
39+
})
40+
.on("pageerror", (error) => {
41+
pageErrors.push(error);
42+
});
43+
44+
const response = await page.goto(`http://127.0.0.1:${port}/main`, {
45+
waitUntil: "networkidle0",
46+
});
47+
48+
expect(response.status()).toMatchSnapshot("response status");
49+
50+
await server.stop();
51+
// Can't wait to check for unlimited times so wait only for 5-6 retries
52+
// eslint-disable-next-line no-restricted-properties
53+
await page.waitForTimeout(1000 * Math.pow(2, 5) + Math.random() * 100);
54+
55+
expect(consoleMessages.map((message) => message.text())).toMatchSnapshot(
56+
"console messages"
57+
);
58+
59+
expect(pageErrors).toMatchSnapshot("page errors");
60+
});
61+
});
62+
63+
describe("specified as false", () => {
64+
let compiler;
65+
let server;
66+
let page;
67+
let browser;
68+
let pageErrors;
69+
let consoleMessages;
70+
71+
beforeEach(async () => {
72+
compiler = webpack(config);
73+
74+
server = new Server({ port, client: { reconnect: false } }, compiler);
75+
76+
await server.start();
77+
78+
({ page, browser } = await runBrowser());
79+
80+
pageErrors = [];
81+
consoleMessages = [];
82+
});
83+
84+
afterEach(async () => {
85+
await browser.close();
86+
});
87+
88+
it("should not try to reconnect", async () => {
89+
page
90+
.on("console", (message) => {
91+
consoleMessages.push(message);
92+
})
93+
.on("pageerror", (error) => {
94+
pageErrors.push(error);
95+
});
96+
97+
const response = await page.goto(`http://127.0.0.1:${port}/main`, {
98+
waitUntil: "networkidle0",
99+
});
100+
101+
expect(response.status()).toMatchSnapshot("response status");
102+
103+
await server.stop();
104+
// Can't wait to check for unlimited times so wait only for 5-6 retries
105+
// eslint-disable-next-line no-restricted-properties
106+
await page.waitForTimeout(1000 * Math.pow(2, 2) + Math.random() * 100);
107+
108+
expect(consoleMessages.map((message) => message.text())).toMatchSnapshot(
109+
"console messages"
110+
);
111+
112+
expect(pageErrors).toMatchSnapshot("page errors");
113+
});
114+
});
115+
116+
describe("specified as number", () => {
117+
let compiler;
118+
let server;
119+
let page;
120+
let browser;
121+
let pageErrors;
122+
let consoleMessages;
123+
124+
beforeEach(async () => {
125+
compiler = webpack(config);
126+
127+
server = new Server({ port, client: { reconnect: 2 } }, compiler);
128+
129+
await server.start();
130+
131+
({ page, browser } = await runBrowser());
132+
133+
pageErrors = [];
134+
consoleMessages = [];
135+
});
136+
137+
afterEach(async () => {
138+
await browser.close();
139+
});
140+
141+
it("should try to reconnect 2 times", async () => {
142+
page
143+
.on("console", (message) => {
144+
consoleMessages.push(message);
145+
})
146+
.on("pageerror", (error) => {
147+
pageErrors.push(error);
148+
});
149+
150+
const response = await page.goto(`http://127.0.0.1:${port}/main`, {
151+
waitUntil: "networkidle0",
152+
});
153+
154+
expect(response.status()).toMatchSnapshot("response status");
155+
156+
await server.stop();
157+
// eslint-disable-next-line no-restricted-properties
158+
await page.waitForTimeout(1000 * Math.pow(2, 2) + Math.random() * 100);
159+
160+
expect(consoleMessages.map((message) => message.text())).toMatchSnapshot(
161+
"console messages"
162+
);
163+
164+
expect(pageErrors).toMatchSnapshot("page errors");
165+
});
166+
});
167+
});

0 commit comments

Comments
 (0)