From 195f099d37a4139bbf9cd290acf41abb493461cc Mon Sep 17 00:00:00 2001 From: Nitin Kumar Date: Sat, 25 Sep 2021 07:48:41 +0530 Subject: [PATCH 1/4] test: add e2e tests for `client` option --- .../client.test.js.snap.webpack4 | 23 ++ .../client.test.js.snap.webpack5 | 23 ++ test/e2e/client.test.js | 264 ++++++++++++++++++ 3 files changed, 310 insertions(+) create mode 100644 test/e2e/__snapshots__/client.test.js.snap.webpack4 create mode 100644 test/e2e/__snapshots__/client.test.js.snap.webpack5 create mode 100644 test/e2e/client.test.js diff --git a/test/e2e/__snapshots__/client.test.js.snap.webpack4 b/test/e2e/__snapshots__/client.test.js.snap.webpack4 new file mode 100644 index 0000000000..9b56611e80 --- /dev/null +++ b/test/e2e/__snapshots__/client.test.js.snap.webpack4 @@ -0,0 +1,23 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`client option configure client entry should disable client entry: console messages 1`] = ` +Array [ + "Failed to load resource: the server responded with a status of 404 (Not Found)", +] +`; + +exports[`client option configure client entry should disable client entry: page errors 1`] = `Array []`; + +exports[`client option configure client entry should disable client entry: response status 1`] = `404`; + +exports[`client option default behaviour responds with a 200 status code for /ws path: console messages 1`] = `Array []`; + +exports[`client option default behaviour responds with a 200 status code for /ws path: page errors 1`] = `Array []`; + +exports[`client option default behaviour responds with a 200 status code for /ws path: response status 1`] = `200`; + +exports[`client option should respect path option responds with a 200 status code for /foo/test/bar path: console messages 1`] = `Array []`; + +exports[`client option should respect path option responds with a 200 status code for /foo/test/bar path: page errors 1`] = `Array []`; + +exports[`client option should respect path option responds with a 200 status code for /foo/test/bar path: response status 1`] = `200`; diff --git a/test/e2e/__snapshots__/client.test.js.snap.webpack5 b/test/e2e/__snapshots__/client.test.js.snap.webpack5 new file mode 100644 index 0000000000..9b56611e80 --- /dev/null +++ b/test/e2e/__snapshots__/client.test.js.snap.webpack5 @@ -0,0 +1,23 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`client option configure client entry should disable client entry: console messages 1`] = ` +Array [ + "Failed to load resource: the server responded with a status of 404 (Not Found)", +] +`; + +exports[`client option configure client entry should disable client entry: page errors 1`] = `Array []`; + +exports[`client option configure client entry should disable client entry: response status 1`] = `404`; + +exports[`client option default behaviour responds with a 200 status code for /ws path: console messages 1`] = `Array []`; + +exports[`client option default behaviour responds with a 200 status code for /ws path: page errors 1`] = `Array []`; + +exports[`client option default behaviour responds with a 200 status code for /ws path: response status 1`] = `200`; + +exports[`client option should respect path option responds with a 200 status code for /foo/test/bar path: console messages 1`] = `Array []`; + +exports[`client option should respect path option responds with a 200 status code for /foo/test/bar path: page errors 1`] = `Array []`; + +exports[`client option should respect path option responds with a 200 status code for /foo/test/bar path: response status 1`] = `200`; diff --git a/test/e2e/client.test.js b/test/e2e/client.test.js new file mode 100644 index 0000000000..a2721af77e --- /dev/null +++ b/test/e2e/client.test.js @@ -0,0 +1,264 @@ +"use strict"; + +const webpack = require("webpack"); +const Server = require("../../lib/Server"); +const config = require("../fixtures/simple-config-other/webpack.config"); +const runBrowser = require("../helpers/run-browser"); +const port = require("../ports-map")["client-option"]; + +describe("client option", () => { + describe("default behaviour", () => { + let compiler; + let server; + let page; + let browser; + let pageErrors; + let consoleMessages; + + beforeEach(async () => { + compiler = webpack(config); + + server = new Server( + { + client: { + webSocketTransport: "sockjs", + }, + webSocketServer: "sockjs", + port, + }, + compiler + ); + + await server.start(); + + ({ page, browser } = await runBrowser()); + + pageErrors = []; + consoleMessages = []; + }); + + afterEach(async () => { + await browser.close(); + await server.stop(); + }); + + it("overlay should be true by default", () => { + expect(server.options.client.overlay).toBe(true); + }); + + it("responds with a 200 status code for /ws path", async () => { + page + .on("console", (message) => { + consoleMessages.push(message); + }) + .on("pageerror", (error) => { + pageErrors.push(error); + }); + + const response = await page.goto(`http://127.0.0.1:${port}/ws`, { + waitUntil: "networkidle0", + }); + + expect(response.status()).toMatchSnapshot("response status"); + + expect(consoleMessages.map((message) => message.text())).toMatchSnapshot( + "console messages" + ); + + expect(pageErrors).toMatchSnapshot("page errors"); + }); + }); + + describe("should respect path option", () => { + let compiler; + let server; + let page; + let browser; + let pageErrors; + let consoleMessages; + + beforeEach(async () => { + compiler = webpack(config); + + server = new Server( + { + client: { + webSocketTransport: "sockjs", + }, + webSocketServer: { + type: "sockjs", + options: { + host: "localhost", + port, + path: "/foo/test/bar", + }, + }, + port, + }, + compiler + ); + + await server.start(); + + ({ page, browser } = await runBrowser()); + + pageErrors = []; + consoleMessages = []; + }); + + afterEach(async () => { + await browser.close(); + await server.stop(); + }); + + it("responds with a 200 status code for /foo/test/bar path", async () => { + page + .on("console", (message) => { + consoleMessages.push(message); + }) + .on("pageerror", (error) => { + pageErrors.push(error); + }); + + const response = await page.goto( + `http://127.0.0.1:${port}/foo/test/bar`, + { + waitUntil: "networkidle0", + } + ); + + expect(response.status()).toMatchSnapshot("response status"); + + expect(consoleMessages.map((message) => message.text())).toMatchSnapshot( + "console messages" + ); + + expect(pageErrors).toMatchSnapshot("page errors"); + }); + }); + + describe("configure client entry", () => { + let compiler; + let server; + let page; + let browser; + let pageErrors; + let consoleMessages; + + beforeEach(async () => { + compiler = webpack(config); + + server = new Server( + { + client: false, + port, + }, + compiler + ); + + await server.start(); + + ({ page, browser } = await runBrowser()); + + pageErrors = []; + consoleMessages = []; + }); + + afterEach(async () => { + await browser.close(); + await server.stop(); + }); + + it("should disable client entry", async () => { + page + .on("console", (message) => { + consoleMessages.push(message); + }) + .on("pageerror", (error) => { + pageErrors.push(error); + }); + + const response = await page.goto( + `http://127.0.0.1:${port}/foo/test/bar`, + { + waitUntil: "networkidle0", + } + ); + + expect(response.status()).toMatchSnapshot("response status"); + + expect(await response.text()).not.toMatch(/client\/index\.js/); + + expect(consoleMessages.map((message) => message.text())).toMatchSnapshot( + "console messages" + ); + + expect(pageErrors).toMatchSnapshot("page errors"); + }); + }); + + describe("webSocketTransport", () => { + const clientModes = [ + { + title: 'as a string ("sockjs")', + client: { + webSocketTransport: "sockjs", + }, + webSocketServer: "sockjs", + shouldThrow: false, + }, + { + title: 'as a path ("sockjs")', + client: { + webSocketTransport: require.resolve( + "../../client-src/clients/SockJSClient" + ), + }, + webSocketServer: "sockjs", + shouldThrow: false, + }, + { + title: "as a nonexistent path", + client: { + webSocketTransport: "/bad/path/to/implementation", + }, + webSocketServer: "sockjs", + shouldThrow: true, + }, + ]; + + describe("passed to server", () => { + clientModes.forEach((data) => { + it(`${data.title} ${ + data.shouldThrow ? "should throw" : "should not throw" + }`, async () => { + const compiler = webpack(config); + + const server = new Server( + { + client: data.client, + port, + }, + compiler + ); + + let thrownError; + + try { + await server.start(); + } catch (error) { + thrownError = error; + } + + if (data.shouldThrow) { + expect(thrownError.message).toMatch( + /client\.webSocketTransport must be a string/ + ); + } + + await server.stop(); + }); + }); + }); + }); +}); From 9a082006b3e85df99ca87e9e1704f3a82789dd40 Mon Sep 17 00:00:00 2001 From: Nitin Kumar Date: Sat, 25 Sep 2021 08:10:31 +0530 Subject: [PATCH 2/4] test: add e2e tests for `client` option --- test/e2e/__snapshots__/client.test.js.snap.webpack4 | 8 ++------ test/e2e/__snapshots__/client.test.js.snap.webpack5 | 8 ++------ test/e2e/client.test.js | 9 +++------ 3 files changed, 7 insertions(+), 18 deletions(-) diff --git a/test/e2e/__snapshots__/client.test.js.snap.webpack4 b/test/e2e/__snapshots__/client.test.js.snap.webpack4 index 9b56611e80..d6f85f5a2f 100644 --- a/test/e2e/__snapshots__/client.test.js.snap.webpack4 +++ b/test/e2e/__snapshots__/client.test.js.snap.webpack4 @@ -1,14 +1,10 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`client option configure client entry should disable client entry: console messages 1`] = ` -Array [ - "Failed to load resource: the server responded with a status of 404 (Not Found)", -] -`; +exports[`client option configure client entry should disable client entry: console messages 1`] = `Array []`; exports[`client option configure client entry should disable client entry: page errors 1`] = `Array []`; -exports[`client option configure client entry should disable client entry: response status 1`] = `404`; +exports[`client option configure client entry should disable client entry: response status 1`] = `200`; exports[`client option default behaviour responds with a 200 status code for /ws path: console messages 1`] = `Array []`; diff --git a/test/e2e/__snapshots__/client.test.js.snap.webpack5 b/test/e2e/__snapshots__/client.test.js.snap.webpack5 index 9b56611e80..d6f85f5a2f 100644 --- a/test/e2e/__snapshots__/client.test.js.snap.webpack5 +++ b/test/e2e/__snapshots__/client.test.js.snap.webpack5 @@ -1,14 +1,10 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`client option configure client entry should disable client entry: console messages 1`] = ` -Array [ - "Failed to load resource: the server responded with a status of 404 (Not Found)", -] -`; +exports[`client option configure client entry should disable client entry: console messages 1`] = `Array []`; exports[`client option configure client entry should disable client entry: page errors 1`] = `Array []`; -exports[`client option configure client entry should disable client entry: response status 1`] = `404`; +exports[`client option configure client entry should disable client entry: response status 1`] = `200`; exports[`client option default behaviour responds with a 200 status code for /ws path: console messages 1`] = `Array []`; diff --git a/test/e2e/client.test.js b/test/e2e/client.test.js index a2721af77e..4d17ee2cf2 100644 --- a/test/e2e/client.test.js +++ b/test/e2e/client.test.js @@ -178,12 +178,9 @@ describe("client option", () => { pageErrors.push(error); }); - const response = await page.goto( - `http://127.0.0.1:${port}/foo/test/bar`, - { - waitUntil: "networkidle0", - } - ); + const response = await page.goto(`http://127.0.0.1:${port}/main.js`, { + waitUntil: "networkidle0", + }); expect(response.status()).toMatchSnapshot("response status"); From 881b4b8a458f90b2ac8c4d43b3a16eb190b37bfd Mon Sep 17 00:00:00 2001 From: Nitin Kumar Date: Sun, 26 Sep 2021 07:07:32 +0530 Subject: [PATCH 3/4] test: add more cases for ws --- test/e2e/client.test.js | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/test/e2e/client.test.js b/test/e2e/client.test.js index 4d17ee2cf2..176a27af88 100644 --- a/test/e2e/client.test.js +++ b/test/e2e/client.test.js @@ -204,6 +204,14 @@ describe("client option", () => { webSocketServer: "sockjs", shouldThrow: false, }, + { + title: 'as a string ("ws")', + client: { + webSocketTransport: "ws", + }, + webSocketServer: "ws", + shouldThrow: false, + }, { title: 'as a path ("sockjs")', client: { @@ -214,6 +222,16 @@ describe("client option", () => { webSocketServer: "sockjs", shouldThrow: false, }, + { + title: 'as a path ("ws")', + client: { + webSocketTransport: require.resolve( + "../../client-src/clients/WebSocketClient" + ), + }, + webSocketServer: "ws", + shouldThrow: false, + }, { title: "as a nonexistent path", client: { @@ -222,6 +240,14 @@ describe("client option", () => { webSocketServer: "sockjs", shouldThrow: true, }, + { + title: "as a nonexistent path", + client: { + webSocketTransport: "/bad/path/to/implementation", + }, + webSocketServer: "ws", + shouldThrow: true, + }, ]; describe("passed to server", () => { From 4eca98cc3219b2a035294bc966c8efeb809af1cb Mon Sep 17 00:00:00 2001 From: Nitin Kumar Date: Thu, 30 Sep 2021 18:15:23 +0530 Subject: [PATCH 4/4] chore: update test case title --- test/e2e/client.test.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/e2e/client.test.js b/test/e2e/client.test.js index 176a27af88..ea4d0a3cef 100644 --- a/test/e2e/client.test.js +++ b/test/e2e/client.test.js @@ -233,7 +233,7 @@ describe("client option", () => { shouldThrow: false, }, { - title: "as a nonexistent path", + title: "as a nonexistent path (sockjs)", client: { webSocketTransport: "/bad/path/to/implementation", }, @@ -241,7 +241,7 @@ describe("client option", () => { shouldThrow: true, }, { - title: "as a nonexistent path", + title: "as a nonexistent path (ws)", client: { webSocketTransport: "/bad/path/to/implementation", },