diff --git a/.eslintignore b/.eslintignore index c9ce987fed..861faf369b 100644 --- a/.eslintignore +++ b/.eslintignore @@ -1,4 +1,5 @@ /client +/dist !/test/client coverage node_modules diff --git a/.eslintrc.js b/.eslintrc.js index 153fda6f97..871471d890 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -2,6 +2,7 @@ module.exports = { extends: ['webpack', 'prettier'], + parser: '@babel/eslint-parser', parserOptions: { sourceType: 'script', ecmaVersion: 2018, diff --git a/lib/Server.js b/lib/Server.js index 4a8910bf9c..bfb141f68d 100644 --- a/lib/Server.js +++ b/lib/Server.js @@ -36,8 +36,6 @@ class Server { this.staticWatchers = []; // Keep track of websocket proxies for external websocket upgrade. this.webSocketProxies = []; - // this value of web socket can be overwritten for tests - this.webSocketHeartbeatInterval = 30000; normalizeOptions( this.compiler, diff --git a/lib/servers/SockJSServer.js b/lib/servers/SockJSServer.js index ccc81ec700..5d0e859288 100644 --- a/lib/servers/SockJSServer.js +++ b/lib/servers/SockJSServer.js @@ -65,11 +65,9 @@ module.exports = class SockJSServer extends BaseServer { send(connection, message) { // prevent cases where the server is trying to send data while connection is closing - if (connection.readyState !== 1) { - return; + if (connection.readyState === 1) { + connection.write(message); } - - connection.write(message); } close(callback) { diff --git a/lib/servers/WebsocketServer.js b/lib/servers/WebsocketServer.js index ddeba5e543..e2c703ba94 100644 --- a/lib/servers/WebsocketServer.js +++ b/lib/servers/WebsocketServer.js @@ -3,14 +3,16 @@ /* eslint-disable class-methods-use-this */ -const ws = require('ws'); +const WebSocket = require('ws'); const BaseServer = require('./BaseServer'); module.exports = class WebsocketServer extends BaseServer { + static heartbeatInterval = 30000; + constructor(server) { super(server); - this.implementation = new ws.Server({ + this.implementation = new WebSocket.Server({ ...this.server.options.webSocketServer.options, noServer: true, }); @@ -40,7 +42,7 @@ module.exports = class WebsocketServer extends BaseServer { socket.isAlive = false; socket.ping(noop); }); - }, this.server.webSocketHeartbeatInterval); + }, WebsocketServer.heartbeatInterval); this.implementation.on('close', () => { clearInterval(interval); @@ -48,12 +50,10 @@ module.exports = class WebsocketServer extends BaseServer { } send(connection, message) { - // prevent cases where the server is trying to send data while connection is closing - if (connection.readyState !== 1) { - return; + // Prevent cases where the server is trying to send data while connection is closing + if (connection.readyState === WebSocket.OPEN) { + connection.send(message); } - - connection.send(message); } close(callback) { diff --git a/package-lock.json b/package-lock.json index c7272faee3..de87e58ff6 100644 --- a/package-lock.json +++ b/package-lock.json @@ -40,7 +40,8 @@ }, "devDependencies": { "@babel/cli": "^7.14.5", - "@babel/core": "^7.14.5", + "@babel/core": "^7.14.6", + "@babel/eslint-parser": "^7.14.7", "@babel/plugin-transform-object-assign": "^7.14.5", "@babel/plugin-transform-runtime": "^7.14.5", "@babel/preset-env": "^7.14.5", @@ -53,7 +54,7 @@ "body-parser": "^1.19.0", "core-js": "^3.12.1", "css-loader": "^5.2.4", - "eslint": "^7.28.0", + "eslint": "^7.29.0", "eslint-config-prettier": "^8.3.0", "eslint-config-webpack": "^1.2.5", "eslint-plugin-import": "^2.23.2", @@ -175,6 +176,24 @@ "url": "https://opencollective.com/babel" } }, + "node_modules/@babel/eslint-parser": { + "version": "7.14.7", + "resolved": "https://registry.npmjs.org/@babel/eslint-parser/-/eslint-parser-7.14.7.tgz", + "integrity": "sha512-6WPwZqO5priAGIwV6msJcdc9TsEPzYeYdS/Xuoap+/ihkgN6dzHp2bcAAwyWZ5bLzk0vvjDmKvRwkqNaiJ8BiQ==", + "dev": true, + "dependencies": { + "eslint-scope": "^5.1.1", + "eslint-visitor-keys": "^2.1.0", + "semver": "^6.3.0" + }, + "engines": { + "node": "^10.13.0 || ^12.13.0 || >=14.0.0" + }, + "peerDependencies": { + "@babel/core": ">=7.11.0", + "eslint": ">=7.5.0" + } + }, "node_modules/@babel/generator": { "version": "7.14.5", "resolved": "https://registry.npmjs.org/@babel/generator/-/generator-7.14.5.tgz", @@ -17903,6 +17922,17 @@ "source-map": "^0.5.0" } }, + "@babel/eslint-parser": { + "version": "7.14.7", + "resolved": "https://registry.npmjs.org/@babel/eslint-parser/-/eslint-parser-7.14.7.tgz", + "integrity": "sha512-6WPwZqO5priAGIwV6msJcdc9TsEPzYeYdS/Xuoap+/ihkgN6dzHp2bcAAwyWZ5bLzk0vvjDmKvRwkqNaiJ8BiQ==", + "dev": true, + "requires": { + "eslint-scope": "^5.1.1", + "eslint-visitor-keys": "^2.1.0", + "semver": "^6.3.0" + } + }, "@babel/generator": { "version": "7.14.5", "resolved": "https://registry.npmjs.org/@babel/generator/-/generator-7.14.5.tgz", diff --git a/package.json b/package.json index 5f57933e33..a2e88e69c1 100644 --- a/package.json +++ b/package.json @@ -60,7 +60,8 @@ }, "devDependencies": { "@babel/cli": "^7.14.5", - "@babel/core": "^7.14.5", + "@babel/core": "^7.14.6", + "@babel/eslint-parser": "^7.14.7", "@babel/plugin-transform-object-assign": "^7.14.5", "@babel/plugin-transform-runtime": "^7.14.5", "@babel/preset-env": "^7.14.5", @@ -73,7 +74,7 @@ "body-parser": "^1.19.0", "core-js": "^3.12.1", "css-loader": "^5.2.4", - "eslint": "^7.28.0", + "eslint": "^7.29.0", "eslint-config-prettier": "^8.3.0", "eslint-config-webpack": "^1.2.5", "eslint-plugin-import": "^2.23.2", diff --git a/test/e2e/__snapshots__/logging.test.js.snap.webpack4 b/test/e2e/__snapshots__/logging.test.js.snap.webpack4 index 46799fde52..592e6a4b43 100644 --- a/test/e2e/__snapshots__/logging.test.js.snap.webpack4 +++ b/test/e2e/__snapshots__/logging.test.js.snap.webpack4 @@ -1,11 +1,5 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`logging should work and do not log messages about hot and live reloading is enabled (default) 1`] = ` -Array [ - "Hey.", -] -`; - exports[`logging should work and do not log messages about hot and live reloading is enabled (sockjs) 1`] = ` Array [ "Hey.", @@ -18,17 +12,6 @@ Array [ ] `; -exports[`logging should work and log errors by default (default) 1`] = ` -Array [ - "[HMR] Waiting for update signal from WDS...", - "Hey.", - "[webpack-dev-server] Hot Module Replacement enabled.", - "[webpack-dev-server] Live Reloading enabled.", - "[webpack-dev-server] Errors while compiling. Reload prevented.", - "[webpack-dev-server] Error from compilation", -] -`; - exports[`logging should work and log errors by default (sockjs) 1`] = ` Array [ "[HMR] Waiting for update signal from WDS...", @@ -51,13 +34,6 @@ Array [ ] `; -exports[`logging should work and log message about live reloading is enabled (default) 1`] = ` -Array [ - "Hey.", - "[webpack-dev-server] Live Reloading enabled.", -] -`; - exports[`logging should work and log message about live reloading is enabled (sockjs) 1`] = ` Array [ "Hey.", @@ -72,33 +48,6 @@ Array [ ] `; -exports[`logging should work and log messages about hot and live reloading is enabled (default) 1`] = ` -Array [ - "[HMR] Waiting for update signal from WDS...", - "Hey.", - "[webpack-dev-server] Hot Module Replacement enabled.", - "[webpack-dev-server] Live Reloading enabled.", -] -`; - -exports[`logging should work and log messages about hot and live reloading is enabled (default) 2`] = ` -Array [ - "[HMR] Waiting for update signal from WDS...", - "Hey.", - "[webpack-dev-server] Hot Module Replacement enabled.", - "[webpack-dev-server] Live Reloading enabled.", -] -`; - -exports[`logging should work and log messages about hot and live reloading is enabled (default) 3`] = ` -Array [ - "[HMR] Waiting for update signal from WDS...", - "Hey.", - "[webpack-dev-server] Hot Module Replacement enabled.", - "[webpack-dev-server] Live Reloading enabled.", -] -`; - exports[`logging should work and log messages about hot and live reloading is enabled (sockjs) 1`] = ` Array [ "[HMR] Waiting for update signal from WDS...", @@ -153,14 +102,6 @@ Array [ ] `; -exports[`logging should work and log messages about hot is enabled (default) 1`] = ` -Array [ - "[HMR] Waiting for update signal from WDS...", - "Hey.", - "[webpack-dev-server] Hot Module Replacement enabled.", -] -`; - exports[`logging should work and log messages about hot is enabled (sockjs) 1`] = ` Array [ "[HMR] Waiting for update signal from WDS...", @@ -177,14 +118,6 @@ Array [ ] `; -exports[`logging should work and log only error (default) 1`] = ` -Array [ - "Hey.", - "[webpack-dev-server] Errors while compiling. Reload prevented.", - "[webpack-dev-server] Error from compilation", -] -`; - exports[`logging should work and log only error (sockjs) 1`] = ` Array [ "Hey.", @@ -201,20 +134,6 @@ Array [ ] `; -exports[`logging should work and log static changes (default) 1`] = ` -Array [ - "[HMR] Waiting for update signal from WDS...", - "Hey.", - "[webpack-dev-server] Hot Module Replacement enabled.", - "[webpack-dev-server] Live Reloading enabled.", - "[webpack-dev-server] \\"/test/fixtures/client-config/static/foo.txt\\" from static directory was changed. Reloading...", - "[HMR] Waiting for update signal from WDS...", - "Hey.", - "[webpack-dev-server] Hot Module Replacement enabled.", - "[webpack-dev-server] Live Reloading enabled.", -] -`; - exports[`logging should work and log static changes (sockjs) 1`] = ` Array [ "[HMR] Waiting for update signal from WDS...", @@ -243,16 +162,6 @@ Array [ ] `; -exports[`logging should work and log warning and errors (default) 1`] = ` -Array [ - "Hey.", - "[webpack-dev-server] Warnings while compiling.", - "[webpack-dev-server] Warning from compilation", - "[webpack-dev-server] Errors while compiling. Reload prevented.", - "[webpack-dev-server] Error from compilation", -] -`; - exports[`logging should work and log warning and errors (sockjs) 1`] = ` Array [ "Hey.", @@ -273,17 +182,6 @@ Array [ ] `; -exports[`logging should work and log warnings by default (default) 1`] = ` -Array [ - "[HMR] Waiting for update signal from WDS...", - "Hey.", - "[webpack-dev-server] Hot Module Replacement enabled.", - "[webpack-dev-server] Live Reloading enabled.", - "[webpack-dev-server] Warnings while compiling.", - "[webpack-dev-server] Warning from compilation", -] -`; - exports[`logging should work and log warnings by default (sockjs) 1`] = ` Array [ "[HMR] Waiting for update signal from WDS...", @@ -306,15 +204,6 @@ Array [ ] `; -exports[`logging should work when the "client.logging" is "info" (default) 1`] = ` -Array [ - "[HMR] Waiting for update signal from WDS...", - "Hey.", - "[webpack-dev-server] Hot Module Replacement enabled.", - "[webpack-dev-server] Live Reloading enabled.", -] -`; - exports[`logging should work when the "client.logging" is "info" (sockjs) 1`] = ` Array [ "[HMR] Waiting for update signal from WDS...", @@ -333,15 +222,6 @@ Array [ ] `; -exports[`logging should work when the "client.logging" is "log" (default) 1`] = ` -Array [ - "[HMR] Waiting for update signal from WDS...", - "Hey.", - "[webpack-dev-server] Hot Module Replacement enabled.", - "[webpack-dev-server] Live Reloading enabled.", -] -`; - exports[`logging should work when the "client.logging" is "log" (sockjs) 1`] = ` Array [ "[HMR] Waiting for update signal from WDS...", @@ -360,12 +240,6 @@ Array [ ] `; -exports[`logging should work when the "client.logging" is "none" (default) 1`] = ` -Array [ - "Hey.", -] -`; - exports[`logging should work when the "client.logging" is "none" (sockjs) 1`] = ` Array [ "Hey.", @@ -378,15 +252,6 @@ Array [ ] `; -exports[`logging should work when the "client.logging" is "verbose" (default) 1`] = ` -Array [ - "[HMR] Waiting for update signal from WDS...", - "Hey.", - "[webpack-dev-server] Hot Module Replacement enabled.", - "[webpack-dev-server] Live Reloading enabled.", -] -`; - exports[`logging should work when the "client.logging" is "verbose" (sockjs) 1`] = ` Array [ "[HMR] Waiting for update signal from WDS...", diff --git a/test/e2e/__snapshots__/logging.test.js.snap.webpack5 b/test/e2e/__snapshots__/logging.test.js.snap.webpack5 index 46799fde52..592e6a4b43 100644 --- a/test/e2e/__snapshots__/logging.test.js.snap.webpack5 +++ b/test/e2e/__snapshots__/logging.test.js.snap.webpack5 @@ -1,11 +1,5 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`logging should work and do not log messages about hot and live reloading is enabled (default) 1`] = ` -Array [ - "Hey.", -] -`; - exports[`logging should work and do not log messages about hot and live reloading is enabled (sockjs) 1`] = ` Array [ "Hey.", @@ -18,17 +12,6 @@ Array [ ] `; -exports[`logging should work and log errors by default (default) 1`] = ` -Array [ - "[HMR] Waiting for update signal from WDS...", - "Hey.", - "[webpack-dev-server] Hot Module Replacement enabled.", - "[webpack-dev-server] Live Reloading enabled.", - "[webpack-dev-server] Errors while compiling. Reload prevented.", - "[webpack-dev-server] Error from compilation", -] -`; - exports[`logging should work and log errors by default (sockjs) 1`] = ` Array [ "[HMR] Waiting for update signal from WDS...", @@ -51,13 +34,6 @@ Array [ ] `; -exports[`logging should work and log message about live reloading is enabled (default) 1`] = ` -Array [ - "Hey.", - "[webpack-dev-server] Live Reloading enabled.", -] -`; - exports[`logging should work and log message about live reloading is enabled (sockjs) 1`] = ` Array [ "Hey.", @@ -72,33 +48,6 @@ Array [ ] `; -exports[`logging should work and log messages about hot and live reloading is enabled (default) 1`] = ` -Array [ - "[HMR] Waiting for update signal from WDS...", - "Hey.", - "[webpack-dev-server] Hot Module Replacement enabled.", - "[webpack-dev-server] Live Reloading enabled.", -] -`; - -exports[`logging should work and log messages about hot and live reloading is enabled (default) 2`] = ` -Array [ - "[HMR] Waiting for update signal from WDS...", - "Hey.", - "[webpack-dev-server] Hot Module Replacement enabled.", - "[webpack-dev-server] Live Reloading enabled.", -] -`; - -exports[`logging should work and log messages about hot and live reloading is enabled (default) 3`] = ` -Array [ - "[HMR] Waiting for update signal from WDS...", - "Hey.", - "[webpack-dev-server] Hot Module Replacement enabled.", - "[webpack-dev-server] Live Reloading enabled.", -] -`; - exports[`logging should work and log messages about hot and live reloading is enabled (sockjs) 1`] = ` Array [ "[HMR] Waiting for update signal from WDS...", @@ -153,14 +102,6 @@ Array [ ] `; -exports[`logging should work and log messages about hot is enabled (default) 1`] = ` -Array [ - "[HMR] Waiting for update signal from WDS...", - "Hey.", - "[webpack-dev-server] Hot Module Replacement enabled.", -] -`; - exports[`logging should work and log messages about hot is enabled (sockjs) 1`] = ` Array [ "[HMR] Waiting for update signal from WDS...", @@ -177,14 +118,6 @@ Array [ ] `; -exports[`logging should work and log only error (default) 1`] = ` -Array [ - "Hey.", - "[webpack-dev-server] Errors while compiling. Reload prevented.", - "[webpack-dev-server] Error from compilation", -] -`; - exports[`logging should work and log only error (sockjs) 1`] = ` Array [ "Hey.", @@ -201,20 +134,6 @@ Array [ ] `; -exports[`logging should work and log static changes (default) 1`] = ` -Array [ - "[HMR] Waiting for update signal from WDS...", - "Hey.", - "[webpack-dev-server] Hot Module Replacement enabled.", - "[webpack-dev-server] Live Reloading enabled.", - "[webpack-dev-server] \\"/test/fixtures/client-config/static/foo.txt\\" from static directory was changed. Reloading...", - "[HMR] Waiting for update signal from WDS...", - "Hey.", - "[webpack-dev-server] Hot Module Replacement enabled.", - "[webpack-dev-server] Live Reloading enabled.", -] -`; - exports[`logging should work and log static changes (sockjs) 1`] = ` Array [ "[HMR] Waiting for update signal from WDS...", @@ -243,16 +162,6 @@ Array [ ] `; -exports[`logging should work and log warning and errors (default) 1`] = ` -Array [ - "Hey.", - "[webpack-dev-server] Warnings while compiling.", - "[webpack-dev-server] Warning from compilation", - "[webpack-dev-server] Errors while compiling. Reload prevented.", - "[webpack-dev-server] Error from compilation", -] -`; - exports[`logging should work and log warning and errors (sockjs) 1`] = ` Array [ "Hey.", @@ -273,17 +182,6 @@ Array [ ] `; -exports[`logging should work and log warnings by default (default) 1`] = ` -Array [ - "[HMR] Waiting for update signal from WDS...", - "Hey.", - "[webpack-dev-server] Hot Module Replacement enabled.", - "[webpack-dev-server] Live Reloading enabled.", - "[webpack-dev-server] Warnings while compiling.", - "[webpack-dev-server] Warning from compilation", -] -`; - exports[`logging should work and log warnings by default (sockjs) 1`] = ` Array [ "[HMR] Waiting for update signal from WDS...", @@ -306,15 +204,6 @@ Array [ ] `; -exports[`logging should work when the "client.logging" is "info" (default) 1`] = ` -Array [ - "[HMR] Waiting for update signal from WDS...", - "Hey.", - "[webpack-dev-server] Hot Module Replacement enabled.", - "[webpack-dev-server] Live Reloading enabled.", -] -`; - exports[`logging should work when the "client.logging" is "info" (sockjs) 1`] = ` Array [ "[HMR] Waiting for update signal from WDS...", @@ -333,15 +222,6 @@ Array [ ] `; -exports[`logging should work when the "client.logging" is "log" (default) 1`] = ` -Array [ - "[HMR] Waiting for update signal from WDS...", - "Hey.", - "[webpack-dev-server] Hot Module Replacement enabled.", - "[webpack-dev-server] Live Reloading enabled.", -] -`; - exports[`logging should work when the "client.logging" is "log" (sockjs) 1`] = ` Array [ "[HMR] Waiting for update signal from WDS...", @@ -360,12 +240,6 @@ Array [ ] `; -exports[`logging should work when the "client.logging" is "none" (default) 1`] = ` -Array [ - "Hey.", -] -`; - exports[`logging should work when the "client.logging" is "none" (sockjs) 1`] = ` Array [ "Hey.", @@ -378,15 +252,6 @@ Array [ ] `; -exports[`logging should work when the "client.logging" is "verbose" (default) 1`] = ` -Array [ - "[HMR] Waiting for update signal from WDS...", - "Hey.", - "[webpack-dev-server] Hot Module Replacement enabled.", - "[webpack-dev-server] Live Reloading enabled.", -] -`; - exports[`logging should work when the "client.logging" is "verbose" (sockjs) 1`] = ` Array [ "[HMR] Waiting for update signal from WDS...", diff --git a/test/e2e/__snapshots__/web-socket-communication.test.js.snap.webpack4 b/test/e2e/__snapshots__/web-socket-communication.test.js.snap.webpack4 new file mode 100644 index 0000000000..d1cd9ca1fb --- /dev/null +++ b/test/e2e/__snapshots__/web-socket-communication.test.js.snap.webpack4 @@ -0,0 +1,93 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`web socket communication should work and close web socket client connection when web socket server closed ("sockjs"): console messages 1`] = ` +Array [ + "[HMR] Waiting for update signal from WDS...", + "Hey.", + "[webpack-dev-server] Hot Module Replacement enabled.", + "[webpack-dev-server] Live Reloading enabled.", + "[webpack-dev-server] Disconnected!", +] +`; + +exports[`web socket communication should work and close web socket client connection when web socket server closed ("sockjs"): page errors 1`] = `Array []`; + +exports[`web socket communication should work and close web socket client connection when web socket server closed ("ws"): console messages 1`] = ` +Array [ + "[HMR] Waiting for update signal from WDS...", + "Hey.", + "[webpack-dev-server] Hot Module Replacement enabled.", + "[webpack-dev-server] Live Reloading enabled.", + "[webpack-dev-server] Disconnected!", +] +`; + +exports[`web socket communication should work and close web socket client connection when web socket server closed ("ws"): page errors 1`] = `Array []`; + +exports[`web socket communication should work and reconnect when the connection is lost ("sockjs"): console messages 1`] = ` +Array [ + "[HMR] Waiting for update signal from WDS...", + "Hey.", + "[webpack-dev-server] Hot Module Replacement enabled.", + "[webpack-dev-server] Live Reloading enabled.", + "[webpack-dev-server] Disconnected!", + "[webpack-dev-server] Hot Module Replacement enabled.", + "[webpack-dev-server] Live Reloading enabled.", + "[webpack-dev-server] App hot update...", + "[HMR] Checking for updates on the server...", + "Failed to load resource: the server responded with a status of 404 (Not Found)", + "[HMR] Cannot find update. Need to do a full reload!", + "[HMR] (Probably because of restarting the webpack-dev-server)", + "[HMR] Waiting for update signal from WDS...", + "Hey.", + "[webpack-dev-server] Hot Module Replacement enabled.", + "[webpack-dev-server] Live Reloading enabled.", +] +`; + +exports[`web socket communication should work and reconnect when the connection is lost ("sockjs"): page errors 1`] = `Array []`; + +exports[`web socket communication should work and reconnect when the connection is lost ("ws"): console messages 1`] = ` +Array [ + "[HMR] Waiting for update signal from WDS...", + "Hey.", + "[webpack-dev-server] Hot Module Replacement enabled.", + "[webpack-dev-server] Live Reloading enabled.", + "[webpack-dev-server] Disconnected!", + "[webpack-dev-server] Hot Module Replacement enabled.", + "[webpack-dev-server] Live Reloading enabled.", + "[webpack-dev-server] App hot update...", + "[HMR] Checking for updates on the server...", + "Failed to load resource: the server responded with a status of 404 (Not Found)", + "[HMR] Cannot find update. Need to do a full reload!", + "[HMR] (Probably because of restarting the webpack-dev-server)", + "[HMR] Waiting for update signal from WDS...", + "Hey.", + "[webpack-dev-server] Hot Module Replacement enabled.", + "[webpack-dev-server] Live Reloading enabled.", +] +`; + +exports[`web socket communication should work and reconnect when the connection is lost ("ws"): page errors 1`] = `Array []`; + +exports[`web socket communication should work and terminate client that is not alive ("sockjs"): console messages 1`] = ` +Array [ + "[HMR] Waiting for update signal from WDS...", + "Hey.", + "[webpack-dev-server] Hot Module Replacement enabled.", + "[webpack-dev-server] Live Reloading enabled.", +] +`; + +exports[`web socket communication should work and terminate client that is not alive ("sockjs"): page errors 1`] = `Array []`; + +exports[`web socket communication should work and terminate client that is not alive ("ws"): console messages 1`] = ` +Array [ + "[HMR] Waiting for update signal from WDS...", + "Hey.", + "[webpack-dev-server] Hot Module Replacement enabled.", + "[webpack-dev-server] Live Reloading enabled.", +] +`; + +exports[`web socket communication should work and terminate client that is not alive ("ws"): page errors 1`] = `Array []`; diff --git a/test/e2e/__snapshots__/web-socket-communication.test.js.snap.webpack5 b/test/e2e/__snapshots__/web-socket-communication.test.js.snap.webpack5 new file mode 100644 index 0000000000..d1cd9ca1fb --- /dev/null +++ b/test/e2e/__snapshots__/web-socket-communication.test.js.snap.webpack5 @@ -0,0 +1,93 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`web socket communication should work and close web socket client connection when web socket server closed ("sockjs"): console messages 1`] = ` +Array [ + "[HMR] Waiting for update signal from WDS...", + "Hey.", + "[webpack-dev-server] Hot Module Replacement enabled.", + "[webpack-dev-server] Live Reloading enabled.", + "[webpack-dev-server] Disconnected!", +] +`; + +exports[`web socket communication should work and close web socket client connection when web socket server closed ("sockjs"): page errors 1`] = `Array []`; + +exports[`web socket communication should work and close web socket client connection when web socket server closed ("ws"): console messages 1`] = ` +Array [ + "[HMR] Waiting for update signal from WDS...", + "Hey.", + "[webpack-dev-server] Hot Module Replacement enabled.", + "[webpack-dev-server] Live Reloading enabled.", + "[webpack-dev-server] Disconnected!", +] +`; + +exports[`web socket communication should work and close web socket client connection when web socket server closed ("ws"): page errors 1`] = `Array []`; + +exports[`web socket communication should work and reconnect when the connection is lost ("sockjs"): console messages 1`] = ` +Array [ + "[HMR] Waiting for update signal from WDS...", + "Hey.", + "[webpack-dev-server] Hot Module Replacement enabled.", + "[webpack-dev-server] Live Reloading enabled.", + "[webpack-dev-server] Disconnected!", + "[webpack-dev-server] Hot Module Replacement enabled.", + "[webpack-dev-server] Live Reloading enabled.", + "[webpack-dev-server] App hot update...", + "[HMR] Checking for updates on the server...", + "Failed to load resource: the server responded with a status of 404 (Not Found)", + "[HMR] Cannot find update. Need to do a full reload!", + "[HMR] (Probably because of restarting the webpack-dev-server)", + "[HMR] Waiting for update signal from WDS...", + "Hey.", + "[webpack-dev-server] Hot Module Replacement enabled.", + "[webpack-dev-server] Live Reloading enabled.", +] +`; + +exports[`web socket communication should work and reconnect when the connection is lost ("sockjs"): page errors 1`] = `Array []`; + +exports[`web socket communication should work and reconnect when the connection is lost ("ws"): console messages 1`] = ` +Array [ + "[HMR] Waiting for update signal from WDS...", + "Hey.", + "[webpack-dev-server] Hot Module Replacement enabled.", + "[webpack-dev-server] Live Reloading enabled.", + "[webpack-dev-server] Disconnected!", + "[webpack-dev-server] Hot Module Replacement enabled.", + "[webpack-dev-server] Live Reloading enabled.", + "[webpack-dev-server] App hot update...", + "[HMR] Checking for updates on the server...", + "Failed to load resource: the server responded with a status of 404 (Not Found)", + "[HMR] Cannot find update. Need to do a full reload!", + "[HMR] (Probably because of restarting the webpack-dev-server)", + "[HMR] Waiting for update signal from WDS...", + "Hey.", + "[webpack-dev-server] Hot Module Replacement enabled.", + "[webpack-dev-server] Live Reloading enabled.", +] +`; + +exports[`web socket communication should work and reconnect when the connection is lost ("ws"): page errors 1`] = `Array []`; + +exports[`web socket communication should work and terminate client that is not alive ("sockjs"): console messages 1`] = ` +Array [ + "[HMR] Waiting for update signal from WDS...", + "Hey.", + "[webpack-dev-server] Hot Module Replacement enabled.", + "[webpack-dev-server] Live Reloading enabled.", +] +`; + +exports[`web socket communication should work and terminate client that is not alive ("sockjs"): page errors 1`] = `Array []`; + +exports[`web socket communication should work and terminate client that is not alive ("ws"): console messages 1`] = ` +Array [ + "[HMR] Waiting for update signal from WDS...", + "Hey.", + "[webpack-dev-server] Hot Module Replacement enabled.", + "[webpack-dev-server] Live Reloading enabled.", +] +`; + +exports[`web socket communication should work and terminate client that is not alive ("ws"): page errors 1`] = `Array []`; diff --git a/test/e2e/__snapshots__/web-socket-server-url.test.js.snap.webpack4 b/test/e2e/__snapshots__/web-socket-server-url.test.js.snap.webpack4 index 4a006deca8..60deae68bb 100644 --- a/test/e2e/__snapshots__/web-socket-server-url.test.js.snap.webpack4 +++ b/test/e2e/__snapshots__/web-socket-server-url.test.js.snap.webpack4 @@ -371,7 +371,7 @@ Array [ exports[`web socket server URL should work with the "client.webSocketURL.pathname" option ("ws"): page errors 1`] = `Array []`; -exports[`web socket server URL should work with the "client.webSocketURL.pathname" option and custom web socket server "path" ("sockjs"): console messages 1`] = ` +exports[`web socket server URL should work with the "client.webSocketURL.pathname" option and the custom web socket server "path" ("sockjs"): console messages 1`] = ` Array [ "[HMR] Waiting for update signal from WDS...", "Hey.", @@ -380,9 +380,9 @@ Array [ ] `; -exports[`web socket server URL should work with the "client.webSocketURL.pathname" option and custom web socket server "path" ("sockjs"): page errors 1`] = `Array []`; +exports[`web socket server URL should work with the "client.webSocketURL.pathname" option and the custom web socket server "path" ("sockjs"): page errors 1`] = `Array []`; -exports[`web socket server URL should work with the "client.webSocketURL.pathname" option and custom web socket server "path" ("ws"): console messages 1`] = ` +exports[`web socket server URL should work with the "client.webSocketURL.pathname" option and the custom web socket server "path" ("ws"): console messages 1`] = ` Array [ "[HMR] Waiting for update signal from WDS...", "Hey.", @@ -391,7 +391,95 @@ Array [ ] `; -exports[`web socket server URL should work with the "client.webSocketURL.pathname" option and custom web socket server "path" ("ws"): page errors 1`] = `Array []`; +exports[`web socket server URL should work with the "client.webSocketURL.pathname" option and the custom web socket server "path" ("ws"): page errors 1`] = `Array []`; + +exports[`web socket server URL should work with the "client.webSocketURL.pathname" option and the custom web socket server "path" ending with slash ("sockjs"): console messages 1`] = ` +Array [ + "[HMR] Waiting for update signal from WDS...", + "Hey.", + "[webpack-dev-server] Hot Module Replacement enabled.", + "[webpack-dev-server] Live Reloading enabled.", +] +`; + +exports[`web socket server URL should work with the "client.webSocketURL.pathname" option and the custom web socket server "path" ending with slash ("sockjs"): page errors 1`] = `Array []`; + +exports[`web socket server URL should work with the "client.webSocketURL.pathname" option and the custom web socket server "path" ending with slash ("ws"): console messages 1`] = ` +Array [ + "[HMR] Waiting for update signal from WDS...", + "Hey.", + "[webpack-dev-server] Hot Module Replacement enabled.", + "[webpack-dev-server] Live Reloading enabled.", +] +`; + +exports[`web socket server URL should work with the "client.webSocketURL.pathname" option and the custom web socket server "path" ending with slash ("ws"): page errors 1`] = `Array []`; + +exports[`web socket server URL should work with the "client.webSocketURL.pathname" option and the custom web socket server "path" ending without slash ("sockjs"): console messages 1`] = ` +Array [ + "[HMR] Waiting for update signal from WDS...", + "Hey.", + "[webpack-dev-server] Hot Module Replacement enabled.", + "[webpack-dev-server] Live Reloading enabled.", +] +`; + +exports[`web socket server URL should work with the "client.webSocketURL.pathname" option and the custom web socket server "path" ending without slash ("sockjs"): page errors 1`] = `Array []`; + +exports[`web socket server URL should work with the "client.webSocketURL.pathname" option and the custom web socket server "path" ending without slash ("ws"): console messages 1`] = ` +Array [ + "[HMR] Waiting for update signal from WDS...", + "Hey.", + "[webpack-dev-server] Hot Module Replacement enabled.", + "[webpack-dev-server] Live Reloading enabled.", +] +`; + +exports[`web socket server URL should work with the "client.webSocketURL.pathname" option and the custom web socket server "path" ending without slash ("ws"): page errors 1`] = `Array []`; + +exports[`web socket server URL should work with the "client.webSocketURL.pathname" option and the custom web socket server "path" using empty value ("sockjs"): console messages 1`] = ` +Array [ + "[HMR] Waiting for update signal from WDS...", + "Hey.", + "[webpack-dev-server] Hot Module Replacement enabled.", + "[webpack-dev-server] Live Reloading enabled.", +] +`; + +exports[`web socket server URL should work with the "client.webSocketURL.pathname" option and the custom web socket server "path" using empty value ("sockjs"): page errors 1`] = `Array []`; + +exports[`web socket server URL should work with the "client.webSocketURL.pathname" option and the custom web socket server "path" using empty value ("ws"): console messages 1`] = ` +Array [ + "[HMR] Waiting for update signal from WDS...", + "Hey.", + "[webpack-dev-server] Hot Module Replacement enabled.", + "[webpack-dev-server] Live Reloading enabled.", +] +`; + +exports[`web socket server URL should work with the "client.webSocketURL.pathname" option and the custom web socket server "path" using empty value ("ws"): page errors 1`] = `Array []`; + +exports[`web socket server URL should work with the "client.webSocketURL.pathname" option and the custom web socket server "prefix" for compatibility with "sockjs" ("sockjs"): console messages 1`] = ` +Array [ + "[HMR] Waiting for update signal from WDS...", + "Hey.", + "[webpack-dev-server] Hot Module Replacement enabled.", + "[webpack-dev-server] Live Reloading enabled.", +] +`; + +exports[`web socket server URL should work with the "client.webSocketURL.pathname" option and the custom web socket server "prefix" for compatibility with "sockjs" ("sockjs"): page errors 1`] = `Array []`; + +exports[`web socket server URL should work with the "client.webSocketURL.pathname" option and the custom web socket server "prefix" for compatibility with "sockjs" ("ws"): console messages 1`] = ` +Array [ + "[HMR] Waiting for update signal from WDS...", + "Hey.", + "[webpack-dev-server] Hot Module Replacement enabled.", + "[webpack-dev-server] Live Reloading enabled.", +] +`; + +exports[`web socket server URL should work with the "client.webSocketURL.pathname" option and the custom web socket server "prefix" for compatibility with "sockjs" ("ws"): page errors 1`] = `Array []`; exports[`web socket server URL should work with the "client.webSocketURL.port" option ("sockjs"): console messages 1`] = ` Array [ @@ -568,3 +656,47 @@ Array [ `; exports[`web socket server URL should work with the "client.webSocketURL.username" option ("ws"): page errors 1`] = `Array []`; + +exports[`web socket server URL should work with the custom web socket server "path" ("sockjs"): console messages 1`] = ` +Array [ + "[HMR] Waiting for update signal from WDS...", + "Hey.", + "[webpack-dev-server] Hot Module Replacement enabled.", + "[webpack-dev-server] Live Reloading enabled.", +] +`; + +exports[`web socket server URL should work with the custom web socket server "path" ("sockjs"): page errors 1`] = `Array []`; + +exports[`web socket server URL should work with the custom web socket server "path" ("ws"): console messages 1`] = ` +Array [ + "[HMR] Waiting for update signal from WDS...", + "Hey.", + "[webpack-dev-server] Hot Module Replacement enabled.", + "[webpack-dev-server] Live Reloading enabled.", +] +`; + +exports[`web socket server URL should work with the custom web socket server "path" ("ws"): page errors 1`] = `Array []`; + +exports[`web socket server URL should work with the custom web socket server "path" using empty value ("sockjs"): console messages 1`] = ` +Array [ + "[HMR] Waiting for update signal from WDS...", + "Hey.", + "[webpack-dev-server] Hot Module Replacement enabled.", + "[webpack-dev-server] Live Reloading enabled.", +] +`; + +exports[`web socket server URL should work with the custom web socket server "path" using empty value ("sockjs"): page errors 1`] = `Array []`; + +exports[`web socket server URL should work with the custom web socket server "path" using empty value ("ws"): console messages 1`] = ` +Array [ + "[HMR] Waiting for update signal from WDS...", + "Hey.", + "[webpack-dev-server] Hot Module Replacement enabled.", + "[webpack-dev-server] Live Reloading enabled.", +] +`; + +exports[`web socket server URL should work with the custom web socket server "path" using empty value ("ws"): page errors 1`] = `Array []`; diff --git a/test/e2e/__snapshots__/web-socket-server-url.test.js.snap.webpack5 b/test/e2e/__snapshots__/web-socket-server-url.test.js.snap.webpack5 index 4a006deca8..60deae68bb 100644 --- a/test/e2e/__snapshots__/web-socket-server-url.test.js.snap.webpack5 +++ b/test/e2e/__snapshots__/web-socket-server-url.test.js.snap.webpack5 @@ -371,7 +371,7 @@ Array [ exports[`web socket server URL should work with the "client.webSocketURL.pathname" option ("ws"): page errors 1`] = `Array []`; -exports[`web socket server URL should work with the "client.webSocketURL.pathname" option and custom web socket server "path" ("sockjs"): console messages 1`] = ` +exports[`web socket server URL should work with the "client.webSocketURL.pathname" option and the custom web socket server "path" ("sockjs"): console messages 1`] = ` Array [ "[HMR] Waiting for update signal from WDS...", "Hey.", @@ -380,9 +380,9 @@ Array [ ] `; -exports[`web socket server URL should work with the "client.webSocketURL.pathname" option and custom web socket server "path" ("sockjs"): page errors 1`] = `Array []`; +exports[`web socket server URL should work with the "client.webSocketURL.pathname" option and the custom web socket server "path" ("sockjs"): page errors 1`] = `Array []`; -exports[`web socket server URL should work with the "client.webSocketURL.pathname" option and custom web socket server "path" ("ws"): console messages 1`] = ` +exports[`web socket server URL should work with the "client.webSocketURL.pathname" option and the custom web socket server "path" ("ws"): console messages 1`] = ` Array [ "[HMR] Waiting for update signal from WDS...", "Hey.", @@ -391,7 +391,95 @@ Array [ ] `; -exports[`web socket server URL should work with the "client.webSocketURL.pathname" option and custom web socket server "path" ("ws"): page errors 1`] = `Array []`; +exports[`web socket server URL should work with the "client.webSocketURL.pathname" option and the custom web socket server "path" ("ws"): page errors 1`] = `Array []`; + +exports[`web socket server URL should work with the "client.webSocketURL.pathname" option and the custom web socket server "path" ending with slash ("sockjs"): console messages 1`] = ` +Array [ + "[HMR] Waiting for update signal from WDS...", + "Hey.", + "[webpack-dev-server] Hot Module Replacement enabled.", + "[webpack-dev-server] Live Reloading enabled.", +] +`; + +exports[`web socket server URL should work with the "client.webSocketURL.pathname" option and the custom web socket server "path" ending with slash ("sockjs"): page errors 1`] = `Array []`; + +exports[`web socket server URL should work with the "client.webSocketURL.pathname" option and the custom web socket server "path" ending with slash ("ws"): console messages 1`] = ` +Array [ + "[HMR] Waiting for update signal from WDS...", + "Hey.", + "[webpack-dev-server] Hot Module Replacement enabled.", + "[webpack-dev-server] Live Reloading enabled.", +] +`; + +exports[`web socket server URL should work with the "client.webSocketURL.pathname" option and the custom web socket server "path" ending with slash ("ws"): page errors 1`] = `Array []`; + +exports[`web socket server URL should work with the "client.webSocketURL.pathname" option and the custom web socket server "path" ending without slash ("sockjs"): console messages 1`] = ` +Array [ + "[HMR] Waiting for update signal from WDS...", + "Hey.", + "[webpack-dev-server] Hot Module Replacement enabled.", + "[webpack-dev-server] Live Reloading enabled.", +] +`; + +exports[`web socket server URL should work with the "client.webSocketURL.pathname" option and the custom web socket server "path" ending without slash ("sockjs"): page errors 1`] = `Array []`; + +exports[`web socket server URL should work with the "client.webSocketURL.pathname" option and the custom web socket server "path" ending without slash ("ws"): console messages 1`] = ` +Array [ + "[HMR] Waiting for update signal from WDS...", + "Hey.", + "[webpack-dev-server] Hot Module Replacement enabled.", + "[webpack-dev-server] Live Reloading enabled.", +] +`; + +exports[`web socket server URL should work with the "client.webSocketURL.pathname" option and the custom web socket server "path" ending without slash ("ws"): page errors 1`] = `Array []`; + +exports[`web socket server URL should work with the "client.webSocketURL.pathname" option and the custom web socket server "path" using empty value ("sockjs"): console messages 1`] = ` +Array [ + "[HMR] Waiting for update signal from WDS...", + "Hey.", + "[webpack-dev-server] Hot Module Replacement enabled.", + "[webpack-dev-server] Live Reloading enabled.", +] +`; + +exports[`web socket server URL should work with the "client.webSocketURL.pathname" option and the custom web socket server "path" using empty value ("sockjs"): page errors 1`] = `Array []`; + +exports[`web socket server URL should work with the "client.webSocketURL.pathname" option and the custom web socket server "path" using empty value ("ws"): console messages 1`] = ` +Array [ + "[HMR] Waiting for update signal from WDS...", + "Hey.", + "[webpack-dev-server] Hot Module Replacement enabled.", + "[webpack-dev-server] Live Reloading enabled.", +] +`; + +exports[`web socket server URL should work with the "client.webSocketURL.pathname" option and the custom web socket server "path" using empty value ("ws"): page errors 1`] = `Array []`; + +exports[`web socket server URL should work with the "client.webSocketURL.pathname" option and the custom web socket server "prefix" for compatibility with "sockjs" ("sockjs"): console messages 1`] = ` +Array [ + "[HMR] Waiting for update signal from WDS...", + "Hey.", + "[webpack-dev-server] Hot Module Replacement enabled.", + "[webpack-dev-server] Live Reloading enabled.", +] +`; + +exports[`web socket server URL should work with the "client.webSocketURL.pathname" option and the custom web socket server "prefix" for compatibility with "sockjs" ("sockjs"): page errors 1`] = `Array []`; + +exports[`web socket server URL should work with the "client.webSocketURL.pathname" option and the custom web socket server "prefix" for compatibility with "sockjs" ("ws"): console messages 1`] = ` +Array [ + "[HMR] Waiting for update signal from WDS...", + "Hey.", + "[webpack-dev-server] Hot Module Replacement enabled.", + "[webpack-dev-server] Live Reloading enabled.", +] +`; + +exports[`web socket server URL should work with the "client.webSocketURL.pathname" option and the custom web socket server "prefix" for compatibility with "sockjs" ("ws"): page errors 1`] = `Array []`; exports[`web socket server URL should work with the "client.webSocketURL.port" option ("sockjs"): console messages 1`] = ` Array [ @@ -568,3 +656,47 @@ Array [ `; exports[`web socket server URL should work with the "client.webSocketURL.username" option ("ws"): page errors 1`] = `Array []`; + +exports[`web socket server URL should work with the custom web socket server "path" ("sockjs"): console messages 1`] = ` +Array [ + "[HMR] Waiting for update signal from WDS...", + "Hey.", + "[webpack-dev-server] Hot Module Replacement enabled.", + "[webpack-dev-server] Live Reloading enabled.", +] +`; + +exports[`web socket server URL should work with the custom web socket server "path" ("sockjs"): page errors 1`] = `Array []`; + +exports[`web socket server URL should work with the custom web socket server "path" ("ws"): console messages 1`] = ` +Array [ + "[HMR] Waiting for update signal from WDS...", + "Hey.", + "[webpack-dev-server] Hot Module Replacement enabled.", + "[webpack-dev-server] Live Reloading enabled.", +] +`; + +exports[`web socket server URL should work with the custom web socket server "path" ("ws"): page errors 1`] = `Array []`; + +exports[`web socket server URL should work with the custom web socket server "path" using empty value ("sockjs"): console messages 1`] = ` +Array [ + "[HMR] Waiting for update signal from WDS...", + "Hey.", + "[webpack-dev-server] Hot Module Replacement enabled.", + "[webpack-dev-server] Live Reloading enabled.", +] +`; + +exports[`web socket server URL should work with the custom web socket server "path" using empty value ("sockjs"): page errors 1`] = `Array []`; + +exports[`web socket server URL should work with the custom web socket server "path" using empty value ("ws"): console messages 1`] = ` +Array [ + "[HMR] Waiting for update signal from WDS...", + "Hey.", + "[webpack-dev-server] Hot Module Replacement enabled.", + "[webpack-dev-server] Live Reloading enabled.", +] +`; + +exports[`web socket server URL should work with the custom web socket server "path" using empty value ("ws"): page errors 1`] = `Array []`; diff --git a/test/e2e/logging.test.js b/test/e2e/logging.test.js index fb852e4460..411f423ada 100644 --- a/test/e2e/logging.test.js +++ b/test/e2e/logging.test.js @@ -10,9 +10,8 @@ const port = require('../ports-map').logging; describe('logging', () => { const webSocketServers = [ - {}, - { webSocketServer: 'sockjs' }, { webSocketServer: 'ws' }, + { webSocketServer: 'sockjs' }, ]; const cases = [ diff --git a/test/e2e/web-socket-communication.test.js b/test/e2e/web-socket-communication.test.js new file mode 100644 index 0000000000..b5de07748d --- /dev/null +++ b/test/e2e/web-socket-communication.test.js @@ -0,0 +1,331 @@ +'use strict'; + +const webpack = require('webpack'); +const WebSocket = require('ws'); +const Server = require('../../lib/Server'); +const WebsocketServer = require('../../lib/servers/WebsocketServer'); +const config = require('../fixtures/client-config/webpack.config'); +const runBrowser = require('../helpers/run-browser'); +const port = require('../ports-map')['web-socket-heartbeat']; + +describe('web socket communication', () => { + const webSocketServers = ['ws', 'sockjs']; + + webSocketServers.forEach((websocketServer) => { + it(`should work and close web socket client connection when web socket server closed ("${websocketServer}")`, async () => { + WebsocketServer.heartbeatInterval = 100; + + const compiler = webpack(config); + const devServerOptions = Object.assign( + {}, + { + host: '127.0.0.1', + port, + webSocketServer: websocketServer, + } + ); + const server = new Server(devServerOptions, compiler); + + await new Promise((resolve, reject) => { + server.listen(devServerOptions.port, devServerOptions.host, (error) => { + if (error) { + reject(error); + + return; + } + + resolve(); + }); + }); + + const { page, browser } = await runBrowser(); + + const pageErrors = []; + const consoleMessages = []; + + page + .on('console', (message) => { + consoleMessages.push(message); + }) + .on('pageerror', (error) => { + pageErrors.push(error); + }); + + await page.goto(`http://127.0.0.1:${port}/main`, { + waitUntil: 'networkidle0', + }); + + await new Promise((resolve, reject) => { + server.close((error) => { + if (error) { + reject(error); + + return; + } + + resolve(); + }); + }); + + await new Promise((resolve) => { + const interval = setInterval(() => { + if (server.webSocketConnections.length === 0) { + clearInterval(interval); + resolve(); + } + }, 100); + }); + + expect(consoleMessages.map((message) => message.text())).toMatchSnapshot( + 'console messages' + ); + expect(pageErrors).toMatchSnapshot('page errors'); + + await browser.close(); + await new Promise((resolve, reject) => { + server.close((error) => { + if (error) { + reject(error); + + return; + } + + resolve(); + }); + }); + }); + + it(`should work and terminate client that is not alive ("${websocketServer}")`, async () => { + WebsocketServer.heartbeatInterval = 100; + + const compiler = webpack(config); + const devServerOptions = Object.assign( + {}, + { + host: '127.0.0.1', + port, + webSocketServer: websocketServer, + } + ); + const server = new Server(devServerOptions, compiler); + + await new Promise((resolve, reject) => { + server.listen(devServerOptions.port, devServerOptions.host, (error) => { + if (error) { + reject(error); + + return; + } + + resolve(); + }); + }); + + const { page, browser } = await runBrowser(); + + const pageErrors = []; + const consoleMessages = []; + + page + .on('console', (message) => { + consoleMessages.push(message); + }) + .on('pageerror', (error) => { + pageErrors.push(error); + }); + + await page.goto(`http://127.0.0.1:${port}/main`, { + waitUntil: 'networkidle0', + }); + await browser.close(); + + // Wait heartbeat + await new Promise((resolve) => { + setTimeout(() => { + resolve(); + }, 200); + }); + + expect(server.webSocketConnections).toHaveLength(0); + expect(consoleMessages.map((message) => message.text())).toMatchSnapshot( + 'console messages' + ); + expect(pageErrors).toMatchSnapshot('page errors'); + + await new Promise((resolve, reject) => { + server.close((error) => { + if (error) { + reject(error); + + return; + } + + resolve(); + }); + }); + }); + + it(`should work and reconnect when the connection is lost ("${websocketServer}")`, async () => { + WebsocketServer.heartbeatInterval = 100; + + const compiler = webpack(config); + const devServerOptions = Object.assign( + {}, + { + host: '127.0.0.1', + port, + webSocketServer: websocketServer, + } + ); + const server = new Server(devServerOptions, compiler); + + await new Promise((resolve, reject) => { + server.listen(devServerOptions.port, devServerOptions.host, (error) => { + if (error) { + reject(error); + + return; + } + + resolve(); + }); + }); + + const { page, browser } = await runBrowser(); + + const pageErrors = []; + const consoleMessages = []; + + page + .on('console', (message) => { + consoleMessages.push(message); + }) + .on('pageerror', (error) => { + pageErrors.push(error); + }); + + await page.goto(`http://127.0.0.1:${port}/main`, { + waitUntil: 'networkidle0', + }); + + await new Promise((resolve, reject) => { + server.close((error) => { + if (error) { + reject(error); + + return; + } + + resolve(); + }); + }); + + await new Promise((resolve, reject) => { + server.listen(devServerOptions.port, devServerOptions.host, (error) => { + if (error) { + reject(error); + + return; + } + + resolve(); + }); + }); + + await page.waitForNavigation({ + waitUntil: 'networkidle0', + }); + + expect(consoleMessages.map((message) => message.text())).toMatchSnapshot( + 'console messages' + ); + expect(pageErrors).toMatchSnapshot('page errors'); + + await browser.close(); + await new Promise((resolve, reject) => { + server.close((error) => { + if (error) { + reject(error); + + return; + } + + resolve(); + }); + }); + }); + }); + + it(`should work and do heartbeat using ("ws" web socket server)`, async () => { + WebsocketServer.heartbeatInterval = 100; + + const compiler = webpack(config); + const devServerOptions = Object.assign( + {}, + { + host: '127.0.0.1', + port, + webSocketServer: 'ws', + } + ); + const server = new Server(devServerOptions, compiler); + + await new Promise((resolve, reject) => { + server.listen(devServerOptions.port, devServerOptions.host, (error) => { + if (error) { + reject(error); + + return; + } + + resolve(); + }); + }); + + server.webSocketServer.heartbeatInterval = 100; + + await new Promise((resolve, reject) => { + const ws = new WebSocket(`ws://127.0.0.1:${devServerOptions.port}/ws`, { + headers: { + host: `127.0.0.1:${devServerOptions.port}`, + origin: `http://127.0.0.1:${devServerOptions.port}`, + }, + }); + + let opened = false; + let received = false; + + ws.on('open', () => { + opened = true; + }); + + ws.on('error', (error) => { + reject(error); + }); + + ws.on('ping', () => { + if (opened && received) { + ws.close(); + } + }); + + ws.on('message', (data) => { + const message = JSON.parse(data); + + if (message.type === 'ok') { + received = true; + } + }); + + ws.on('close', () => { + resolve(); + }); + }); + + await new Promise((resolve) => { + server.close(() => { + resolve(); + }); + }); + }); +}); diff --git a/test/e2e/web-socket-server-url.test.js b/test/e2e/web-socket-server-url.test.js index d65edd53d6..1cc9f4b927 100644 --- a/test/e2e/web-socket-server-url.test.js +++ b/test/e2e/web-socket-server-url.test.js @@ -1557,7 +1557,267 @@ describe('web socket server URL', () => { }); }); - it(`should work with the "client.webSocketURL.pathname" option and custom web socket server "path" ("${webSocketServer}")`, async () => { + it(`should work with the custom web socket server "path" ("${webSocketServer}")`, async () => { + const compiler = webpack(config); + const devServerOptions = { + webSocketServer: { + type: webSocketServer, + options: { + path: '/custom-ws/foo/bar', + }, + }, + port: port1, + host: '0.0.0.0', + allowedHosts: 'all', + }; + const server = new Server(devServerOptions, compiler); + + await new Promise((resolve, reject) => { + server.listen(devServerOptions.port, devServerOptions.host, (error) => { + if (error) { + reject(error); + + return; + } + + resolve(); + }); + }); + + const { page, browser } = await runBrowser(); + + const pageErrors = []; + const consoleMessages = []; + + page + .on('console', (message) => { + consoleMessages.push(message); + }) + .on('pageerror', (error) => { + pageErrors.push(error); + }); + + const webSocketRequests = []; + + if (webSocketServer === 'ws') { + const client = page._client; + + client.on('Network.webSocketCreated', (test) => { + webSocketRequests.push(test); + }); + } else { + page.on('request', (request) => { + if (/\/custom-ws\/foo\/bar/.test(request.url())) { + webSocketRequests.push({ url: request.url() }); + } + }); + } + + await page.goto(`http://127.0.0.1:${port1}/main`, { + waitUntil: 'networkidle0', + }); + + const webSocketRequest = webSocketRequests[0]; + + expect(webSocketRequest.url).toContain( + `${websocketURLProtocol}://127.0.0.1:${port1}/custom-ws/foo/bar` + ); + expect(consoleMessages.map((message) => message.text())).toMatchSnapshot( + 'console messages' + ); + expect(pageErrors).toMatchSnapshot('page errors'); + + await browser.close(); + await new Promise((resolve, reject) => { + server.close((error) => { + if (error) { + reject(error); + + return; + } + + resolve(); + }); + }); + }); + + // Only works for "ws" server + it(`should work with the custom web socket server "path" using empty value ("${webSocketServer}")`, async () => { + const compiler = webpack(config); + const devServerOptions = { + webSocketServer: { + type: webSocketServer, + options: { + path: webSocketServer === 'ws' ? '' : '/custom-ws', + }, + }, + port: port1, + host: '0.0.0.0', + allowedHosts: 'all', + }; + const server = new Server(devServerOptions, compiler); + + await new Promise((resolve, reject) => { + server.listen(devServerOptions.port, devServerOptions.host, (error) => { + if (error) { + reject(error); + + return; + } + + resolve(); + }); + }); + + const { page, browser } = await runBrowser(); + + const pageErrors = []; + const consoleMessages = []; + + page + .on('console', (message) => { + consoleMessages.push(message); + }) + .on('pageerror', (error) => { + pageErrors.push(error); + }); + + const webSocketRequests = []; + + if (webSocketServer === 'ws') { + const client = page._client; + + client.on('Network.webSocketCreated', (test) => { + webSocketRequests.push(test); + }); + } else { + page.on('request', (request) => { + if (/\/custom-ws\//.test(request.url())) { + webSocketRequests.push({ url: request.url() }); + } + }); + } + + await page.goto(`http://127.0.0.1:${port1}/main`, { + waitUntil: 'networkidle0', + }); + + const webSocketRequest = webSocketRequests[0]; + + expect(webSocketRequest.url).toContain( + webSocketServer === 'ws' + ? `${websocketURLProtocol}://127.0.0.1:${port1}` + : `${websocketURLProtocol}://127.0.0.1:${port1}/custom-ws` + ); + expect(consoleMessages.map((message) => message.text())).toMatchSnapshot( + 'console messages' + ); + expect(pageErrors).toMatchSnapshot('page errors'); + + await browser.close(); + await new Promise((resolve, reject) => { + server.close((error) => { + if (error) { + reject(error); + + return; + } + + resolve(); + }); + }); + }); + + it(`should work with the "client.webSocketURL.pathname" option and the custom web socket server "path" ("${webSocketServer}")`, async () => { + const compiler = webpack(config); + const devServerOptions = { + client: { + webSocketURL: { + pathname: '/custom-ws/foo/bar', + }, + }, + webSocketServer: { + type: webSocketServer, + options: { + path: '/custom-ws/foo/bar', + }, + }, + port: port1, + host: '0.0.0.0', + allowedHosts: 'all', + }; + const server = new Server(devServerOptions, compiler); + + await new Promise((resolve, reject) => { + server.listen(devServerOptions.port, devServerOptions.host, (error) => { + if (error) { + reject(error); + + return; + } + + resolve(); + }); + }); + + const { page, browser } = await runBrowser(); + + const pageErrors = []; + const consoleMessages = []; + + page + .on('console', (message) => { + consoleMessages.push(message); + }) + .on('pageerror', (error) => { + pageErrors.push(error); + }); + + const webSocketRequests = []; + + if (webSocketServer === 'ws') { + const client = page._client; + + client.on('Network.webSocketCreated', (test) => { + webSocketRequests.push(test); + }); + } else { + page.on('request', (request) => { + if (/\/custom-ws\/foo\/bar/.test(request.url())) { + webSocketRequests.push({ url: request.url() }); + } + }); + } + + await page.goto(`http://127.0.0.1:${port1}/main`, { + waitUntil: 'networkidle0', + }); + + const webSocketRequest = webSocketRequests[0]; + + expect(webSocketRequest.url).toContain( + `${websocketURLProtocol}://127.0.0.1:${port1}/custom-ws/foo/bar` + ); + expect(consoleMessages.map((message) => message.text())).toMatchSnapshot( + 'console messages' + ); + expect(pageErrors).toMatchSnapshot('page errors'); + + await browser.close(); + await new Promise((resolve, reject) => { + server.close((error) => { + if (error) { + reject(error); + + return; + } + + resolve(); + }); + }); + }); + + it(`should work with the "client.webSocketURL.pathname" option and the custom web socket server "path" ending without slash ("${webSocketServer}")`, async () => { const compiler = webpack(config); const devServerOptions = { client: { @@ -1646,6 +1906,279 @@ describe('web socket server URL', () => { }); }); + // Only works for "ws" server, "sockjs" adds "/" be default, because need do requests like "/custom-ws/info?t=1624462615772" + it(`should work with the "client.webSocketURL.pathname" option and the custom web socket server "path" ending with slash ("${webSocketServer}")`, async () => { + const compiler = webpack(config); + const devServerOptions = { + client: { + webSocketURL: { + pathname: webSocketServer === 'ws' ? '/custom-ws/' : '/custom-ws', + }, + }, + webSocketServer: { + type: webSocketServer, + options: { + path: webSocketServer === 'ws' ? '/custom-ws/' : '/custom-ws', + }, + }, + port: port1, + host: '0.0.0.0', + allowedHosts: 'all', + }; + const server = new Server(devServerOptions, compiler); + + await new Promise((resolve, reject) => { + server.listen(devServerOptions.port, devServerOptions.host, (error) => { + if (error) { + reject(error); + + return; + } + + resolve(); + }); + }); + + const { page, browser } = await runBrowser(); + + const pageErrors = []; + const consoleMessages = []; + + page + .on('console', (message) => { + consoleMessages.push(message); + }) + .on('pageerror', (error) => { + pageErrors.push(error); + }); + + const webSocketRequests = []; + + if (webSocketServer === 'ws') { + const client = page._client; + + client.on('Network.webSocketCreated', (test) => { + webSocketRequests.push(test); + }); + } else { + page.on('request', (request) => { + if (/\/custom-ws\//.test(request.url())) { + webSocketRequests.push({ url: request.url() }); + } + }); + } + + await page.goto(`http://127.0.0.1:${port1}/main`, { + waitUntil: 'networkidle0', + }); + + const webSocketRequest = webSocketRequests[0]; + + expect(webSocketRequest.url).toContain( + `${websocketURLProtocol}://127.0.0.1:${port1}/custom-ws/` + ); + expect(consoleMessages.map((message) => message.text())).toMatchSnapshot( + 'console messages' + ); + expect(pageErrors).toMatchSnapshot('page errors'); + + await browser.close(); + await new Promise((resolve, reject) => { + server.close((error) => { + if (error) { + reject(error); + + return; + } + + resolve(); + }); + }); + }); + + // Only works for "ws" server + it(`should work with the "client.webSocketURL.pathname" option and the custom web socket server "path" using empty value ("${webSocketServer}")`, async () => { + const compiler = webpack(config); + const devServerOptions = { + client: { + webSocketURL: { + pathname: webSocketServer === 'ws' ? '' : '/custom-ws', + }, + }, + webSocketServer: { + type: webSocketServer, + options: { + path: webSocketServer === 'ws' ? '' : '/custom-ws', + }, + }, + port: port1, + host: '0.0.0.0', + allowedHosts: 'all', + }; + const server = new Server(devServerOptions, compiler); + + await new Promise((resolve, reject) => { + server.listen(devServerOptions.port, devServerOptions.host, (error) => { + if (error) { + reject(error); + + return; + } + + resolve(); + }); + }); + + const { page, browser } = await runBrowser(); + + const pageErrors = []; + const consoleMessages = []; + + page + .on('console', (message) => { + consoleMessages.push(message); + }) + .on('pageerror', (error) => { + pageErrors.push(error); + }); + + const webSocketRequests = []; + + if (webSocketServer === 'ws') { + const client = page._client; + + client.on('Network.webSocketCreated', (test) => { + webSocketRequests.push(test); + }); + } else { + page.on('request', (request) => { + if (/\/custom-ws\//.test(request.url())) { + webSocketRequests.push({ url: request.url() }); + } + }); + } + + await page.goto(`http://127.0.0.1:${port1}/main`, { + waitUntil: 'networkidle0', + }); + + const webSocketRequest = webSocketRequests[0]; + + expect(webSocketRequest.url).toContain( + webSocketServer === 'ws' + ? `${websocketURLProtocol}://127.0.0.1:${port1}` + : `${websocketURLProtocol}://127.0.0.1:${port1}/custom-ws` + ); + expect(consoleMessages.map((message) => message.text())).toMatchSnapshot( + 'console messages' + ); + expect(pageErrors).toMatchSnapshot('page errors'); + + await browser.close(); + await new Promise((resolve, reject) => { + server.close((error) => { + if (error) { + reject(error); + + return; + } + + resolve(); + }); + }); + }); + + // Only works for "sockjs" server + it(`should work with the "client.webSocketURL.pathname" option and the custom web socket server "prefix" for compatibility with "sockjs" ("${webSocketServer}")`, async () => { + const compiler = webpack(config); + const devServerOptions = { + client: { + webSocketURL: { + pathname: '/custom-ws', + }, + }, + webSocketServer: { + type: webSocketServer, + options: + webSocketServer === 'ws' + ? { path: '/custom-ws' } + : { prefix: '/custom-ws' }, + }, + port: port1, + host: '0.0.0.0', + allowedHosts: 'all', + }; + const server = new Server(devServerOptions, compiler); + + await new Promise((resolve, reject) => { + server.listen(devServerOptions.port, devServerOptions.host, (error) => { + if (error) { + reject(error); + + return; + } + + resolve(); + }); + }); + + const { page, browser } = await runBrowser(); + + const pageErrors = []; + const consoleMessages = []; + + page + .on('console', (message) => { + consoleMessages.push(message); + }) + .on('pageerror', (error) => { + pageErrors.push(error); + }); + + const webSocketRequests = []; + + if (webSocketServer === 'ws') { + const client = page._client; + + client.on('Network.webSocketCreated', (test) => { + webSocketRequests.push(test); + }); + } else { + page.on('request', (request) => { + if (/\/custom-ws/.test(request.url())) { + webSocketRequests.push({ url: request.url() }); + } + }); + } + + await page.goto(`http://127.0.0.1:${port1}/main`, { + waitUntil: 'networkidle0', + }); + + const webSocketRequest = webSocketRequests[0]; + + expect(webSocketRequest.url).toContain( + `${websocketURLProtocol}://127.0.0.1:${port1}/custom-ws` + ); + expect(consoleMessages.map((message) => message.text())).toMatchSnapshot( + 'console messages' + ); + expect(pageErrors).toMatchSnapshot('page errors'); + + await browser.close(); + await new Promise((resolve, reject) => { + server.close((error) => { + if (error) { + reject(error); + + return; + } + + resolve(); + }); + }); + }); + it(`should work when "host" option is IPv4 ("${webSocketServer}")`, async () => { const hostname = internalIp.v4.sync(); const compiler = webpack(config); diff --git a/test/ports-map.js b/test/ports-map.js index 4eb929a2d9..b2946f5711 100644 --- a/test/ports-map.js +++ b/test/ports-map.js @@ -46,6 +46,7 @@ const listOfTests = { 'sockjs-server': 1, 'web-socket-server': 1, routes: 1, + 'web-socket-heartbeat': 1, }; let startPort = 8089; diff --git a/test/server/Server.test.js b/test/server/Server.test.js index 50cf56339d..fc64d1ff2e 100644 --- a/test/server/Server.test.js +++ b/test/server/Server.test.js @@ -526,7 +526,9 @@ describe('Server', () => { } it('should returns the port when the port is specified', async () => { - process.env.WEBPACK_DEV_SERVER_PORT_RETRY = 1; + const retryCount = 1; + + process.env.WEBPACK_DEV_SERVER_PORT_RETRY = retryCount; const freePort = await Server.getFreePort(8082); expect(freePort).toEqual(8082); @@ -535,7 +537,7 @@ describe('Server', () => { it('should returns the port when the port is null', async () => { const retryCount = 2; - process.env.WEBPACK_DEV_SERVER_PORT_RETRY = 2; + process.env.WEBPACK_DEV_SERVER_PORT_RETRY = retryCount; await createDummyServers(retryCount); const freePort = await Server.getFreePort(null); @@ -545,7 +547,7 @@ describe('Server', () => { it('should returns the port when the port is undefined', async () => { const retryCount = 3; - process.env.WEBPACK_DEV_SERVER_PORT_RETRY = 3; + process.env.WEBPACK_DEV_SERVER_PORT_RETRY = retryCount; await createDummyServers(retryCount); // eslint-disable-next-line no-undefined @@ -554,7 +556,7 @@ describe('Server', () => { }); it('should retry finding the port for up to defaultPortRetry times (number)', async () => { - const retryCount = 3; + const retryCount = 4; process.env.WEBPACK_DEV_SERVER_PORT_RETRY = retryCount; @@ -564,9 +566,9 @@ describe('Server', () => { }); it('should retry finding the port for up to defaultPortRetry times (string)', async () => { - const retryCount = 3; + const retryCount = 5; - process.env.WEBPACK_DEV_SERVER_PORT_RETRY = `${retryCount}`; + process.env.WEBPACK_DEV_SERVER_PORT_RETRY = retryCount; await createDummyServers(retryCount); const freePort = await Server.getFreePort(); @@ -575,7 +577,7 @@ describe('Server', () => { // TODO: fix me, Flaky on CI it('should retry finding the port when serial ports are busy', async () => { - const busyPorts = [60000, 60001, 60002, 60003, 60004, 60005, 60006]; + const busyPorts = [60000, 60001, 60002, 60003, 60004, 60005]; process.env.WEBPACK_DEV_SERVER_PORT_RETRY = 6; diff --git a/test/server/servers/SockJSServer.test.js b/test/server/servers/SockJSServer.test.js deleted file mode 100644 index 5a32aa94fc..0000000000 --- a/test/server/servers/SockJSServer.test.js +++ /dev/null @@ -1,215 +0,0 @@ -/** - * @jest-environment jsdom - */ - -'use strict'; - -const http = require('http'); -const express = require('express'); -const SockJS = require('sockjs-client/dist/sockjs'); -const SockJSServer = require('../../../lib/servers/SockJSServer'); -const port = require('../../ports-map')['sockjs-server']; - -describe('SockJSServer', () => { - describe("should work with the 'path' option", () => { - let socketServer; - let server; - - beforeEach((done) => { - // eslint-disable-next-line new-cap - const app = new express(); - - server = http.createServer(app); - server.listen(port, 'localhost', () => { - socketServer = new SockJSServer({ - logger: { - error: () => {}, - log: () => {}, - debug: () => {}, - }, - options: { - webSocketServer: { - options: { - path: '/ws', - }, - }, - }, - server, - }); - - done(); - }); - }); - - describe('server', () => { - it('should receive connection, send message, and close client', (done) => { - const data = []; - - let headers; - socketServer.onConnection((connection, h) => { - headers = h; - data.push('open'); - socketServer.send(connection, 'hello world'); - setTimeout(() => { - // the server closes the connection with the client - socketServer.closeConnection(connection); - }, 1000); - }); - - const client = new SockJS(`http://localhost:${port}/ws`); - - client.onmessage = (e) => { - data.push(e.data); - }; - - client.onclose = () => { - data.push('close'); - }; - - setTimeout(() => { - expect(headers.host).toMatchSnapshot(); - expect(data).toMatchSnapshot(); - done(); - }, 3000); - }); - - it('should receive client close event', (done) => { - let receivedClientClose = false; - socketServer.onConnection((connection) => { - socketServer.onConnectionClose(connection, () => { - receivedClientClose = true; - }); - }); - - // eslint-disable-next-line new-cap - const client = new SockJS(`http://localhost:${port}/ws`); - - setTimeout(() => { - // the client closes itself, the server does not close it - client.close(); - }, 1000); - - setTimeout(() => { - expect(receivedClientClose).toBeTruthy(); - done(); - }, 3000); - }); - - it('should not throw an exception when connection is null', () => { - const cb = jest.fn(); - - socketServer.onConnection(cb); - - expect(() => { - socketServer.implementation.emit('connection', null); - }).not.toThrow(); - expect(cb.mock.calls[0]).toEqual([null, null]); - }); - }); - - afterEach((done) => { - server.close(done); - }); - }); - - describe("should work with the 'prefix' option", () => { - let socketServer; - let server; - - beforeEach((done) => { - // eslint-disable-next-line new-cap - const app = new express(); - - server = http.createServer(app); - server.listen(port, 'localhost', () => { - socketServer = new SockJSServer({ - logger: { - error: () => {}, - log: () => {}, - debug: () => {}, - }, - options: { - webSocketServer: { - options: { - prefix: '/ws', - }, - }, - }, - server, - }); - - done(); - }); - }); - - describe('server', () => { - it('should receive connection, send message, and close client', (done) => { - const data = []; - - let headers; - socketServer.onConnection((connection, h) => { - headers = h; - data.push('open'); - socketServer.send(connection, 'hello world'); - setTimeout(() => { - // the server closes the connection with the client - socketServer.closeConnection(connection); - }, 1000); - }); - - const client = new SockJS(`http://localhost:${port}/ws`); - - client.onmessage = (e) => { - data.push(e.data); - }; - - client.onclose = () => { - data.push('close'); - }; - - setTimeout(() => { - expect(headers.host).toMatchSnapshot(); - expect(data).toMatchSnapshot(); - done(); - }, 3000); - }); - - it('should receive client close event', (done) => { - let receivedClientClose = false; - socketServer.onConnection((connection) => { - socketServer.onConnectionClose(connection, () => { - receivedClientClose = true; - }); - }); - - // eslint-disable-next-line new-cap - const client = new SockJS(`http://localhost:${port}/ws`); - - setTimeout(() => { - // the client closes itself, the server does not close it - client.close(); - }, 1000); - - setTimeout(() => { - expect(receivedClientClose).toBeTruthy(); - done(); - }, 3000); - }); - - it('should not throw an exception when connection is null', () => { - const cb = jest.fn(); - - socketServer.onConnection(cb); - - expect(() => { - socketServer.implementation.emit('connection', null); - }).not.toThrow(); - expect(cb.mock.calls[0]).toEqual([null, null]); - }); - }); - - afterEach((done) => { - server.close(done); - }); - }); -}); diff --git a/test/server/servers/WebsocketServer.test.js b/test/server/servers/WebsocketServer.test.js deleted file mode 100644 index 17d2c01d91..0000000000 --- a/test/server/servers/WebsocketServer.test.js +++ /dev/null @@ -1,121 +0,0 @@ -/** - * @jest-environment jsdom - */ - -'use strict'; - -const http = require('http'); -const express = require('express'); -const ws = require('ws'); -const WebsocketServer = require('../../../lib/servers/WebsocketServer'); -const port = require('../../ports-map')['web-socket-server']; - -describe('WebsocketServer', () => { - let socketServer; - let server; - - beforeEach((done) => { - // eslint-disable-next-line new-cap - const app = new express(); - - server = http.createServer(app); - server.listen(port, 'localhost', () => { - socketServer = new WebsocketServer({ - options: { - webSocketServer: { - options: { - path: '/ws-server', - }, - }, - }, - server, - webSocketHeartbeatInterval: 800, - }); - done(); - }); - }); - - it('should receive connection, send message, and close client', (done) => { - const data = []; - - let headers; - socketServer.onConnection((connection, h) => { - headers = h; - data.push('open'); - socketServer.send(connection, 'hello world'); - setTimeout(() => { - // the server closes the connection with the client - socketServer.closeConnection(connection); - }, 1000); - }); - - // eslint-disable-next-line new-cap - const client = new ws(`http://localhost:${port}/ws-server`); - - client.onmessage = (e) => { - data.push(e.data); - }; - - client.onclose = () => { - data.push('close'); - }; - - // the heartbeat interval was shortened greatly above - // so that the client is quickly pinged - client.on('ping', () => { - data.push('ping'); - }); - - setTimeout(() => { - expect(headers.host).toMatchSnapshot(); - expect(data).toMatchSnapshot(); - done(); - }, 3000); - }); - - it('should receive client close event', (done) => { - let receivedClientClose = false; - socketServer.onConnection((connection) => { - socketServer.onConnectionClose(connection, () => { - receivedClientClose = true; - }); - }); - - // eslint-disable-next-line new-cap - const client = new ws(`http://localhost:${port}/ws-server`); - - setTimeout(() => { - // the client closes itself, the server does not close it - client.close(); - }, 1000); - - setTimeout(() => { - expect(receivedClientClose).toBeTruthy(); - done(); - }, 3000); - }); - - it('should terminate a client that is not alive', (done) => { - let receivedClientClose = false; - socketServer.onConnection((connection) => { - // this makes the server think the client did not respond - // to a ping in time, so the server will terminate it - connection.isAlive = false; - socketServer.onConnectionClose(connection, () => { - receivedClientClose = true; - }); - }); - - // eslint-disable-next-line new-cap, no-unused-vars - const client = new ws(`http://localhost:${port}/ws-server`); - - setTimeout(() => { - expect(receivedClientClose).toBeTruthy(); - done(); - }, 3000); - }); - - afterEach((done) => { - server.close(done); - }); -}); diff --git a/test/server/servers/__snapshots__/SockJSServer.test.js.snap.webpack4 b/test/server/servers/__snapshots__/SockJSServer.test.js.snap.webpack4 deleted file mode 100644 index 077534b65b..0000000000 --- a/test/server/servers/__snapshots__/SockJSServer.test.js.snap.webpack4 +++ /dev/null @@ -1,21 +0,0 @@ -// Jest Snapshot v1, https://goo.gl/fbAQLP - -exports[`SockJSServer should work with the 'path' option server should receive connection, send message, and close client 1`] = `"localhost:8131"`; - -exports[`SockJSServer should work with the 'path' option server should receive connection, send message, and close client 2`] = ` -Array [ - "open", - "hello world", - "close", -] -`; - -exports[`SockJSServer should work with the 'prefix' option server should receive connection, send message, and close client 1`] = `"localhost:8131"`; - -exports[`SockJSServer should work with the 'prefix' option server should receive connection, send message, and close client 2`] = ` -Array [ - "open", - "hello world", - "close", -] -`; diff --git a/test/server/servers/__snapshots__/SockJSServer.test.js.snap.webpack5 b/test/server/servers/__snapshots__/SockJSServer.test.js.snap.webpack5 deleted file mode 100644 index 077534b65b..0000000000 --- a/test/server/servers/__snapshots__/SockJSServer.test.js.snap.webpack5 +++ /dev/null @@ -1,21 +0,0 @@ -// Jest Snapshot v1, https://goo.gl/fbAQLP - -exports[`SockJSServer should work with the 'path' option server should receive connection, send message, and close client 1`] = `"localhost:8131"`; - -exports[`SockJSServer should work with the 'path' option server should receive connection, send message, and close client 2`] = ` -Array [ - "open", - "hello world", - "close", -] -`; - -exports[`SockJSServer should work with the 'prefix' option server should receive connection, send message, and close client 1`] = `"localhost:8131"`; - -exports[`SockJSServer should work with the 'prefix' option server should receive connection, send message, and close client 2`] = ` -Array [ - "open", - "hello world", - "close", -] -`; diff --git a/test/server/servers/__snapshots__/WebsocketServer.test.js.snap.webpack4 b/test/server/servers/__snapshots__/WebsocketServer.test.js.snap.webpack4 deleted file mode 100644 index c3d5fdbcd5..0000000000 --- a/test/server/servers/__snapshots__/WebsocketServer.test.js.snap.webpack4 +++ /dev/null @@ -1,12 +0,0 @@ -// Jest Snapshot v1, https://goo.gl/fbAQLP - -exports[`WebsocketServer should receive connection, send message, and close client 1`] = `"localhost:8132"`; - -exports[`WebsocketServer should receive connection, send message, and close client 2`] = ` -Array [ - "open", - "hello world", - "ping", - "close", -] -`; diff --git a/test/server/servers/__snapshots__/WebsocketServer.test.js.snap.webpack5 b/test/server/servers/__snapshots__/WebsocketServer.test.js.snap.webpack5 deleted file mode 100644 index c3d5fdbcd5..0000000000 --- a/test/server/servers/__snapshots__/WebsocketServer.test.js.snap.webpack5 +++ /dev/null @@ -1,12 +0,0 @@ -// Jest Snapshot v1, https://goo.gl/fbAQLP - -exports[`WebsocketServer should receive connection, send message, and close client 1`] = `"localhost:8132"`; - -exports[`WebsocketServer should receive connection, send message, and close client 2`] = ` -Array [ - "open", - "hello world", - "ping", - "close", -] -`;