From 714a227e6f0868b79209a53fa806b88990b31cea Mon Sep 17 00:00:00 2001 From: evilebottnawi Date: Mon, 4 Oct 2021 21:05:18 +0300 Subject: [PATCH 1/3] fix: schema for web socket server type --- lib/options.json | 5 ++++- .../validate-options.test.js.snap.webpack4 | 20 +++++++++++++++++-- .../validate-options.test.js.snap.webpack5 | 20 +++++++++++++++++-- test/validate-options.test.js | 3 +++ 4 files changed, 43 insertions(+), 5 deletions(-) diff --git a/lib/options.json b/lib/options.json index f1c01e5d91..c7b43e0fe4 100644 --- a/lib/options.json +++ b/lib/options.json @@ -793,6 +793,9 @@ "WebSocketServerEnum": { "enum": [false, "sockjs", "ws"] }, + "WebSocketServerType": { + "enum": ["sockjs", "ws"] + }, "WebSocketServerFunction": { "instanceof": "Function" }, @@ -805,7 +808,7 @@ "type": { "anyOf": [ { - "$ref": "#/definitions/WebSocketServerEnum" + "$ref": "#/definitions/WebSocketServerType" }, { "$ref": "#/definitions/WebSocketServerString" diff --git a/test/__snapshots__/validate-options.test.js.snap.webpack4 b/test/__snapshots__/validate-options.test.js.snap.webpack4 index 79c47d8241..aa759f096a 100644 --- a/test/__snapshots__/validate-options.test.js.snap.webpack4 +++ b/test/__snapshots__/validate-options.test.js.snap.webpack4 @@ -721,6 +721,22 @@ exports[`options validate should throw an error on the "webSocketServer" option object { type?, options? }" `; +exports[`options validate should throw an error on the "webSocketServer" option with '{"type":false}' value 1`] = ` +"ValidationError: Invalid options object. Dev Server has been initialized using an options object that does not match the API schema. + - options.webSocketServer should be one of these: + false | \\"sockjs\\" | \\"ws\\" | non-empty string | function | object { type?, options? } + -> Allows to set web socket server and options (by default 'ws'). + -> Read more at https://webpack.js.org/configuration/dev-server/#devserverwebsocketserver + Details: + * options.webSocketServer.type should be one of these: + \\"sockjs\\" | \\"ws\\" | non-empty string | function + Details: + * options.webSocketServer.type should be one of these: + \\"sockjs\\" | \\"ws\\" + * options.webSocketServer.type should be a non-empty string. + * options.webSocketServer.type should be an instance of function." +`; + exports[`options validate should throw an error on the "webSocketServer" option with '{"type":true}' value 1`] = ` "ValidationError: Invalid options object. Dev Server has been initialized using an options object that does not match the API schema. - options.webSocketServer should be one of these: @@ -729,10 +745,10 @@ exports[`options validate should throw an error on the "webSocketServer" option -> Read more at https://webpack.js.org/configuration/dev-server/#devserverwebsocketserver Details: * options.webSocketServer.type should be one of these: - false | \\"sockjs\\" | \\"ws\\" | non-empty string | function + \\"sockjs\\" | \\"ws\\" | non-empty string | function Details: * options.webSocketServer.type should be one of these: - false | \\"sockjs\\" | \\"ws\\" + \\"sockjs\\" | \\"ws\\" * options.webSocketServer.type should be a non-empty string. * options.webSocketServer.type should be an instance of function." `; diff --git a/test/__snapshots__/validate-options.test.js.snap.webpack5 b/test/__snapshots__/validate-options.test.js.snap.webpack5 index 79c47d8241..aa759f096a 100644 --- a/test/__snapshots__/validate-options.test.js.snap.webpack5 +++ b/test/__snapshots__/validate-options.test.js.snap.webpack5 @@ -721,6 +721,22 @@ exports[`options validate should throw an error on the "webSocketServer" option object { type?, options? }" `; +exports[`options validate should throw an error on the "webSocketServer" option with '{"type":false}' value 1`] = ` +"ValidationError: Invalid options object. Dev Server has been initialized using an options object that does not match the API schema. + - options.webSocketServer should be one of these: + false | \\"sockjs\\" | \\"ws\\" | non-empty string | function | object { type?, options? } + -> Allows to set web socket server and options (by default 'ws'). + -> Read more at https://webpack.js.org/configuration/dev-server/#devserverwebsocketserver + Details: + * options.webSocketServer.type should be one of these: + \\"sockjs\\" | \\"ws\\" | non-empty string | function + Details: + * options.webSocketServer.type should be one of these: + \\"sockjs\\" | \\"ws\\" + * options.webSocketServer.type should be a non-empty string. + * options.webSocketServer.type should be an instance of function." +`; + exports[`options validate should throw an error on the "webSocketServer" option with '{"type":true}' value 1`] = ` "ValidationError: Invalid options object. Dev Server has been initialized using an options object that does not match the API schema. - options.webSocketServer should be one of these: @@ -729,10 +745,10 @@ exports[`options validate should throw an error on the "webSocketServer" option -> Read more at https://webpack.js.org/configuration/dev-server/#devserverwebsocketserver Details: * options.webSocketServer.type should be one of these: - false | \\"sockjs\\" | \\"ws\\" | non-empty string | function + \\"sockjs\\" | \\"ws\\" | non-empty string | function Details: * options.webSocketServer.type should be one of these: - false | \\"sockjs\\" | \\"ws\\" + \\"sockjs\\" | \\"ws\\" * options.webSocketServer.type should be a non-empty string. * options.webSocketServer.type should be an instance of function." `; diff --git a/test/validate-options.test.js b/test/validate-options.test.js index 7bc528b944..f79893f24b 100644 --- a/test/validate-options.test.js +++ b/test/validate-options.test.js @@ -448,6 +448,9 @@ const tests = { failure: [ null, true, + { + type: false, + }, { notAnOption: true, }, From f1244ca654cc39f4575edd3db568913f08f93bb7 Mon Sep 17 00:00:00 2001 From: evilebottnawi Date: Tue, 5 Oct 2021 12:24:05 +0300 Subject: [PATCH 2/3] refactor: code --- lib/options.json | 13 ++++++++++--- .../validate-options.test.js.snap.webpack4 | 8 ++++++++ .../validate-options.test.js.snap.webpack5 | 8 ++++++++ 3 files changed, 26 insertions(+), 3 deletions(-) diff --git a/lib/options.json b/lib/options.json index c7b43e0fe4..8b1834294a 100644 --- a/lib/options.json +++ b/lib/options.json @@ -790,12 +790,19 @@ "description": "Allows to set web socket server and options (by default 'ws').", "link": "https://webpack.js.org/configuration/dev-server/#devserverwebsocketserver" }, - "WebSocketServerEnum": { - "enum": [false, "sockjs", "ws"] - }, "WebSocketServerType": { "enum": ["sockjs", "ws"] }, + "WebSocketServerEnum": { + "anyOf": [ + { + "enum": [false] + }, + { + "$ref": "#/definitions/WebSocketServerType" + } + ] + }, "WebSocketServerFunction": { "instanceof": "Function" }, diff --git a/test/__snapshots__/validate-options.test.js.snap.webpack4 b/test/__snapshots__/validate-options.test.js.snap.webpack4 index aa759f096a..93171a058e 100644 --- a/test/__snapshots__/validate-options.test.js.snap.webpack4 +++ b/test/__snapshots__/validate-options.test.js.snap.webpack4 @@ -762,6 +762,10 @@ exports[`options validate should throw an error on the "webSocketServer" option Details: * options.webSocketServer should be one of these: false | \\"sockjs\\" | \\"ws\\" + Details: + * options.webSocketServer should be false. + * options.webSocketServer should be one of these: + \\"sockjs\\" | \\"ws\\" * options.webSocketServer should be a non-empty string. * options.webSocketServer should be an instance of function. * options.webSocketServer should be an object: @@ -777,6 +781,10 @@ exports[`options validate should throw an error on the "webSocketServer" option Details: * options.webSocketServer should be one of these: false | \\"sockjs\\" | \\"ws\\" + Details: + * options.webSocketServer should be false. + * options.webSocketServer should be one of these: + \\"sockjs\\" | \\"ws\\" * options.webSocketServer should be a non-empty string. * options.webSocketServer should be an instance of function. * options.webSocketServer should be an object: diff --git a/test/__snapshots__/validate-options.test.js.snap.webpack5 b/test/__snapshots__/validate-options.test.js.snap.webpack5 index aa759f096a..93171a058e 100644 --- a/test/__snapshots__/validate-options.test.js.snap.webpack5 +++ b/test/__snapshots__/validate-options.test.js.snap.webpack5 @@ -762,6 +762,10 @@ exports[`options validate should throw an error on the "webSocketServer" option Details: * options.webSocketServer should be one of these: false | \\"sockjs\\" | \\"ws\\" + Details: + * options.webSocketServer should be false. + * options.webSocketServer should be one of these: + \\"sockjs\\" | \\"ws\\" * options.webSocketServer should be a non-empty string. * options.webSocketServer should be an instance of function. * options.webSocketServer should be an object: @@ -777,6 +781,10 @@ exports[`options validate should throw an error on the "webSocketServer" option Details: * options.webSocketServer should be one of these: false | \\"sockjs\\" | \\"ws\\" + Details: + * options.webSocketServer should be false. + * options.webSocketServer should be one of these: + \\"sockjs\\" | \\"ws\\" * options.webSocketServer should be a non-empty string. * options.webSocketServer should be an instance of function. * options.webSocketServer should be an object: From 26875e0b472c1324dea247de0431e5655a78aabf Mon Sep 17 00:00:00 2001 From: evilebottnawi Date: Tue, 5 Oct 2021 14:57:08 +0300 Subject: [PATCH 3/3] test: fix --- bin/cli-flags.js | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/bin/cli-flags.js b/bin/cli-flags.js index 55e36d7bc4..f755b96309 100644 --- a/bin/cli-flags.js +++ b/bin/cli-flags.js @@ -297,21 +297,30 @@ module.exports = { "web-socket-server": { configs: [ { - type: "enum", - values: [false, "sockjs", "ws"], - multiple: false, description: "Allows to set web socket server and options (by default 'ws').", + multiple: false, path: "webSocketServer", + type: "enum", + values: [false], }, { - type: "string", + description: + "Allows to set web socket server and options (by default 'ws').", multiple: false, + path: "webSocketServer", + type: "enum", + values: ["sockjs", "ws"], + }, + { description: "Allows to set web socket server and options (by default 'ws').", + multiple: false, path: "webSocketServer", + type: "string", }, ], + description: "Allows to set web socket server and options (by default 'ws').", simpleType: "string",