From 45b981bdb3cdcfff82286986fcd943818e9cf6e3 Mon Sep 17 00:00:00 2001 From: Nitin Kumar Date: Fri, 5 Nov 2021 17:59:52 -0700 Subject: [PATCH 1/2] test: add e2e tests for `bonjour` option --- .../bonjour.test.js.snap.webpack5 | 19 ++ test/e2e/bonjour.test.js | 230 ++++++++++++++++++ 2 files changed, 249 insertions(+) create mode 100644 test/e2e/__snapshots__/bonjour.test.js.snap.webpack5 create mode 100644 test/e2e/bonjour.test.js diff --git a/test/e2e/__snapshots__/bonjour.test.js.snap.webpack5 b/test/e2e/__snapshots__/bonjour.test.js.snap.webpack5 new file mode 100644 index 0000000000..6ad7cfc78e --- /dev/null +++ b/test/e2e/__snapshots__/bonjour.test.js.snap.webpack5 @@ -0,0 +1,19 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`bonjour option as object should apply bonjour options: console messages 1`] = `Array []`; + +exports[`bonjour option as object should apply bonjour options: page errors 1`] = `Array []`; + +exports[`bonjour option as object should apply bonjour options: response status 1`] = `200`; + +exports[`bonjour option as true should call bonjour with correct params: console messages 1`] = `Array []`; + +exports[`bonjour option as true should call bonjour with correct params: page errors 1`] = `Array []`; + +exports[`bonjour option as true should call bonjour with correct params: response status 1`] = `200`; + +exports[`bonjour option with 'https' option should call bonjour with 'https' type: console messages 1`] = `Array []`; + +exports[`bonjour option with 'https' option should call bonjour with 'https' type: page errors 1`] = `Array []`; + +exports[`bonjour option with 'https' option should call bonjour with 'https' type: response status 1`] = `200`; diff --git a/test/e2e/bonjour.test.js b/test/e2e/bonjour.test.js new file mode 100644 index 0000000000..c89856067a --- /dev/null +++ b/test/e2e/bonjour.test.js @@ -0,0 +1,230 @@ +"use strict"; + +const os = require("os"); +const webpack = require("webpack"); +const Server = require("../../lib/Server"); +const config = require("../fixtures/simple-config/webpack.config"); +const runBrowser = require("../helpers/run-browser"); +const port = require("../ports-map").bonjour; + +describe("bonjour option", () => { + const mockPublish = jest.fn(); + const mockUnpublishAll = jest.fn(); + + describe("as true", () => { + let compiler; + let server; + let page; + let browser; + let pageErrors; + let consoleMessages; + + beforeEach(async () => { + jest.mock("bonjour", () => () => { + return { + publish: mockPublish, + unpublishAll: mockUnpublishAll, + }; + }); + + compiler = webpack(config); + + server = new Server({ port, bonjour: true }, compiler); + + await server.start(); + + ({ page, browser } = await runBrowser()); + + pageErrors = []; + consoleMessages = []; + }); + + afterEach(async () => { + await browser.close(); + await server.stop(); + mockPublish.mockReset(); + mockUnpublishAll.mockReset(); + }); + + it("should call bonjour with correct params", 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}/main.js`, { + waitUntil: "networkidle0", + }); + + expect(mockPublish).toHaveBeenCalledTimes(1); + + expect(mockPublish).toHaveBeenCalledWith({ + name: `Webpack Dev Server ${os.hostname()}:${port}`, + port, + type: "http", + subtypes: ["webpack"], + }); + + expect(mockUnpublishAll).toHaveBeenCalledTimes(0); + + expect(response.status()).toMatchSnapshot("response status"); + + expect(consoleMessages.map((message) => message.text())).toMatchSnapshot( + "console messages" + ); + + expect(pageErrors).toMatchSnapshot("page errors"); + }); + }); + + describe("with 'https' option", () => { + let compiler; + let server; + let page; + let browser; + let pageErrors; + let consoleMessages; + + beforeEach(async () => { + jest.mock("bonjour", () => () => { + return { + publish: mockPublish, + unpublishAll: mockUnpublishAll, + }; + }); + + compiler = webpack(config); + + server = new Server({ bonjour: true, port, https: true }, compiler); + + await server.start(); + + ({ page, browser } = await runBrowser()); + + pageErrors = []; + consoleMessages = []; + }); + + afterEach(async () => { + await browser.close(); + await server.stop(); + mockPublish.mockReset(); + mockUnpublishAll.mockReset(); + }); + + it("should call bonjour with 'https' type", async () => { + page + .on("console", (message) => { + consoleMessages.push(message); + }) + .on("pageerror", (error) => { + pageErrors.push(error); + }); + + const response = await page.goto(`https://127.0.0.1:${port}/main.js`, { + waitUntil: "networkidle0", + }); + + expect(mockPublish).toHaveBeenCalledTimes(1); + + expect(mockPublish).toHaveBeenCalledWith({ + name: `Webpack Dev Server ${os.hostname()}:${port}`, + port, + type: "https", + subtypes: ["webpack"], + }); + + expect(mockUnpublishAll).toHaveBeenCalledTimes(0); + + expect(response.status()).toMatchSnapshot("response status"); + + expect(consoleMessages.map((message) => message.text())).toMatchSnapshot( + "console messages" + ); + + expect(pageErrors).toMatchSnapshot("page errors"); + }); + }); + + describe("as object", () => { + let compiler; + let server; + let page; + let browser; + let pageErrors; + let consoleMessages; + + beforeEach(async () => { + jest.mock("bonjour", () => () => { + return { + publish: mockPublish, + unpublishAll: mockUnpublishAll, + }; + }); + + compiler = webpack(config); + + server = new Server( + { + port, + bonjour: { + type: "https", + protocol: "udp", + }, + }, + compiler + ); + + await server.start(); + + ({ page, browser } = await runBrowser()); + + pageErrors = []; + consoleMessages = []; + }); + + afterEach(async () => { + await browser.close(); + await server.stop(); + mockPublish.mockReset(); + mockUnpublishAll.mockReset(); + }); + + it("should apply bonjour options", 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}/main.js`, { + waitUntil: "networkidle0", + }); + + expect(mockPublish).toHaveBeenCalledTimes(1); + + expect(mockPublish).toHaveBeenCalledWith({ + name: `Webpack Dev Server ${os.hostname()}:${port}`, + port, + type: "https", + protocol: "udp", + subtypes: ["webpack"], + }); + + expect(mockUnpublishAll).toHaveBeenCalledTimes(0); + + expect(response.status()).toMatchSnapshot("response status"); + + expect(consoleMessages.map((message) => message.text())).toMatchSnapshot( + "console messages" + ); + + expect(pageErrors).toMatchSnapshot("page errors"); + }); + }); +}); From 76ae83a52017e75def2021ad8e334c2f3c58e369 Mon Sep 17 00:00:00 2001 From: Nitin Kumar Date: Fri, 5 Nov 2021 18:07:39 -0700 Subject: [PATCH 2/2] test: add more cases for bounjour --- .../bonjour.test.js.snap.webpack4 | 37 +++ .../bonjour.test.js.snap.webpack5 | 18 ++ test/e2e/bonjour.test.js | 229 ++++++++++++++++++ test/server/bonjour-option.test.js | 148 ----------- 4 files changed, 284 insertions(+), 148 deletions(-) create mode 100644 test/e2e/__snapshots__/bonjour.test.js.snap.webpack4 delete mode 100644 test/server/bonjour-option.test.js diff --git a/test/e2e/__snapshots__/bonjour.test.js.snap.webpack4 b/test/e2e/__snapshots__/bonjour.test.js.snap.webpack4 new file mode 100644 index 0000000000..f1699a3b3e --- /dev/null +++ b/test/e2e/__snapshots__/bonjour.test.js.snap.webpack4 @@ -0,0 +1,37 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`bonjour option as object should apply bonjour options: console messages 1`] = `Array []`; + +exports[`bonjour option as object should apply bonjour options: page errors 1`] = `Array []`; + +exports[`bonjour option as object should apply bonjour options: response status 1`] = `200`; + +exports[`bonjour option as true should call bonjour with correct params: console messages 1`] = `Array []`; + +exports[`bonjour option as true should call bonjour with correct params: page errors 1`] = `Array []`; + +exports[`bonjour option as true should call bonjour with correct params: response status 1`] = `200`; + +exports[`bonjour option bonjour object and 'https' should apply bonjour options: console messages 1`] = `Array []`; + +exports[`bonjour option bonjour object and 'https' should apply bonjour options: page errors 1`] = `Array []`; + +exports[`bonjour option bonjour object and 'https' should apply bonjour options: response status 1`] = `200`; + +exports[`bonjour option bonjour object and 'server' option should apply bonjour options: console messages 1`] = `Array []`; + +exports[`bonjour option bonjour object and 'server' option should apply bonjour options: page errors 1`] = `Array []`; + +exports[`bonjour option bonjour object and 'server' option should apply bonjour options: response status 1`] = `200`; + +exports[`bonjour option with 'https' option should call bonjour with 'https' type: console messages 1`] = `Array []`; + +exports[`bonjour option with 'https' option should call bonjour with 'https' type: page errors 1`] = `Array []`; + +exports[`bonjour option with 'https' option should call bonjour with 'https' type: response status 1`] = `200`; + +exports[`bonjour option with 'server' option should call bonjour with 'https' type: console messages 1`] = `Array []`; + +exports[`bonjour option with 'server' option should call bonjour with 'https' type: page errors 1`] = `Array []`; + +exports[`bonjour option with 'server' option should call bonjour with 'https' type: response status 1`] = `200`; diff --git a/test/e2e/__snapshots__/bonjour.test.js.snap.webpack5 b/test/e2e/__snapshots__/bonjour.test.js.snap.webpack5 index 6ad7cfc78e..f1699a3b3e 100644 --- a/test/e2e/__snapshots__/bonjour.test.js.snap.webpack5 +++ b/test/e2e/__snapshots__/bonjour.test.js.snap.webpack5 @@ -12,8 +12,26 @@ exports[`bonjour option as true should call bonjour with correct params: page er exports[`bonjour option as true should call bonjour with correct params: response status 1`] = `200`; +exports[`bonjour option bonjour object and 'https' should apply bonjour options: console messages 1`] = `Array []`; + +exports[`bonjour option bonjour object and 'https' should apply bonjour options: page errors 1`] = `Array []`; + +exports[`bonjour option bonjour object and 'https' should apply bonjour options: response status 1`] = `200`; + +exports[`bonjour option bonjour object and 'server' option should apply bonjour options: console messages 1`] = `Array []`; + +exports[`bonjour option bonjour object and 'server' option should apply bonjour options: page errors 1`] = `Array []`; + +exports[`bonjour option bonjour object and 'server' option should apply bonjour options: response status 1`] = `200`; + exports[`bonjour option with 'https' option should call bonjour with 'https' type: console messages 1`] = `Array []`; exports[`bonjour option with 'https' option should call bonjour with 'https' type: page errors 1`] = `Array []`; exports[`bonjour option with 'https' option should call bonjour with 'https' type: response status 1`] = `200`; + +exports[`bonjour option with 'server' option should call bonjour with 'https' type: console messages 1`] = `Array []`; + +exports[`bonjour option with 'server' option should call bonjour with 'https' type: page errors 1`] = `Array []`; + +exports[`bonjour option with 'server' option should call bonjour with 'https' type: response status 1`] = `200`; diff --git a/test/e2e/bonjour.test.js b/test/e2e/bonjour.test.js index c89856067a..8f93c9c15f 100644 --- a/test/e2e/bonjour.test.js +++ b/test/e2e/bonjour.test.js @@ -149,6 +149,75 @@ describe("bonjour option", () => { }); }); + describe("with 'server' option", () => { + let compiler; + let server; + let page; + let browser; + let pageErrors; + let consoleMessages; + + beforeEach(async () => { + jest.mock("bonjour", () => () => { + return { + publish: mockPublish, + unpublishAll: mockUnpublishAll, + }; + }); + + compiler = webpack(config); + + server = new Server({ bonjour: true, port, server: "https" }, compiler); + + await server.start(); + + ({ page, browser } = await runBrowser()); + + pageErrors = []; + consoleMessages = []; + }); + + afterEach(async () => { + await browser.close(); + await server.stop(); + mockPublish.mockReset(); + mockUnpublishAll.mockReset(); + }); + + it("should call bonjour with 'https' type", async () => { + page + .on("console", (message) => { + consoleMessages.push(message); + }) + .on("pageerror", (error) => { + pageErrors.push(error); + }); + + const response = await page.goto(`https://127.0.0.1:${port}/main.js`, { + waitUntil: "networkidle0", + }); + + expect(mockPublish).toHaveBeenCalledTimes(1); + + expect(mockPublish).toHaveBeenCalledWith({ + name: `Webpack Dev Server ${os.hostname()}:${port}`, + port, + type: "https", + subtypes: ["webpack"], + }); + + expect(mockUnpublishAll).toHaveBeenCalledTimes(0); + + expect(response.status()).toMatchSnapshot("response status"); + + expect(consoleMessages.map((message) => message.text())).toMatchSnapshot( + "console messages" + ); + + expect(pageErrors).toMatchSnapshot("page errors"); + }); + }); + describe("as object", () => { let compiler; let server; @@ -227,4 +296,164 @@ describe("bonjour option", () => { expect(pageErrors).toMatchSnapshot("page errors"); }); }); + + describe("bonjour object and 'https'", () => { + let compiler; + let server; + let page; + let browser; + let pageErrors; + let consoleMessages; + + beforeEach(async () => { + jest.mock("bonjour", () => () => { + return { + publish: mockPublish, + unpublishAll: mockUnpublishAll, + }; + }); + + compiler = webpack(config); + + server = new Server( + { + port, + bonjour: { + type: "http", + protocol: "udp", + }, + https: true, + }, + compiler + ); + + await server.start(); + + ({ page, browser } = await runBrowser()); + + pageErrors = []; + consoleMessages = []; + }); + + afterEach(async () => { + await browser.close(); + await server.stop(); + mockPublish.mockReset(); + mockUnpublishAll.mockReset(); + }); + + it("should apply bonjour options", async () => { + page + .on("console", (message) => { + consoleMessages.push(message); + }) + .on("pageerror", (error) => { + pageErrors.push(error); + }); + + const response = await page.goto(`https://127.0.0.1:${port}/main.js`, { + waitUntil: "networkidle0", + }); + + expect(mockPublish).toHaveBeenCalledTimes(1); + + expect(mockPublish).toHaveBeenCalledWith({ + name: `Webpack Dev Server ${os.hostname()}:${port}`, + port, + type: "http", + protocol: "udp", + subtypes: ["webpack"], + }); + + expect(mockUnpublishAll).toHaveBeenCalledTimes(0); + + expect(response.status()).toMatchSnapshot("response status"); + + expect(consoleMessages.map((message) => message.text())).toMatchSnapshot( + "console messages" + ); + + expect(pageErrors).toMatchSnapshot("page errors"); + }); + }); + + describe("bonjour object and 'server' option", () => { + let compiler; + let server; + let page; + let browser; + let pageErrors; + let consoleMessages; + + beforeEach(async () => { + jest.mock("bonjour", () => () => { + return { + publish: mockPublish, + unpublishAll: mockUnpublishAll, + }; + }); + + compiler = webpack(config); + + server = new Server( + { + port, + bonjour: { + type: "http", + protocol: "udp", + }, + server: "https", + }, + compiler + ); + + await server.start(); + + ({ page, browser } = await runBrowser()); + + pageErrors = []; + consoleMessages = []; + }); + + afterEach(async () => { + await browser.close(); + await server.stop(); + mockPublish.mockReset(); + mockUnpublishAll.mockReset(); + }); + + it("should apply bonjour options", async () => { + page + .on("console", (message) => { + consoleMessages.push(message); + }) + .on("pageerror", (error) => { + pageErrors.push(error); + }); + + const response = await page.goto(`https://127.0.0.1:${port}/main.js`, { + waitUntil: "networkidle0", + }); + + expect(mockPublish).toHaveBeenCalledTimes(1); + + expect(mockPublish).toHaveBeenCalledWith({ + name: `Webpack Dev Server ${os.hostname()}:${port}`, + port, + type: "http", + protocol: "udp", + subtypes: ["webpack"], + }); + + expect(mockUnpublishAll).toHaveBeenCalledTimes(0); + + expect(response.status()).toMatchSnapshot("response status"); + + expect(consoleMessages.map((message) => message.text())).toMatchSnapshot( + "console messages" + ); + + expect(pageErrors).toMatchSnapshot("page errors"); + }); + }); }); diff --git a/test/server/bonjour-option.test.js b/test/server/bonjour-option.test.js deleted file mode 100644 index b40d8f7ab9..0000000000 --- a/test/server/bonjour-option.test.js +++ /dev/null @@ -1,148 +0,0 @@ -"use strict"; - -const os = require("os"); -const config = require("../fixtures/simple-config/webpack.config"); -const testServer = require("../helpers/test-server"); -const port = require("../ports-map").bonjour; - -describe("bonjour option", () => { - let server; - const mockPublish = jest.fn(); - const mockUnpublishAll = jest.fn(); - - beforeAll(() => { - jest.mock("bonjour", () => () => { - return { - publish: mockPublish, - unpublishAll: mockUnpublishAll, - }; - }); - }); - - afterEach(() => { - mockPublish.mockReset(); - mockUnpublishAll.mockReset(); - }); - - describe("http", () => { - beforeEach((done) => { - server = testServer.start( - config, - { - bonjour: true, - port, - }, - done - ); - }); - - afterEach(async () => { - await server.stop(); - }); - - it("should call bonjour with correct params", () => { - expect(mockPublish).toHaveBeenCalledTimes(1); - expect(mockPublish).toHaveBeenCalledWith({ - name: `Webpack Dev Server ${os.hostname()}:${port}`, - port, - type: "http", - subtypes: ["webpack"], - }); - expect(mockUnpublishAll).toHaveBeenCalledTimes(0); - }); - }); - - describe("https option", () => { - beforeEach((done) => { - server = testServer.start( - config, - { - bonjour: true, - port, - https: true, - }, - done - ); - }); - - afterEach(async () => { - await server.stop(); - }); - - it("bonjour should use https when passed in option", () => { - expect(mockPublish).toHaveBeenCalledTimes(1); - expect(mockPublish).toHaveBeenCalledWith({ - name: `Webpack Dev Server ${os.hostname()}:${port}`, - port, - type: "https", - subtypes: ["webpack"], - }); - expect(mockUnpublishAll).toHaveBeenCalledTimes(0); - }); - }); - - describe("bonjour object", () => { - beforeEach((done) => { - server = testServer.start( - config, - { - bonjour: { - type: "https", - protocol: "udp", - }, - port, - }, - done - ); - }); - - afterEach(async () => { - await server.stop(); - }); - - it("applies bonjour options", () => { - expect(mockPublish).toHaveBeenCalledTimes(1); - expect(mockPublish).toHaveBeenCalledWith({ - name: `Webpack Dev Server ${os.hostname()}:${port}`, - port, - type: "https", - protocol: "udp", - subtypes: ["webpack"], - }); - expect(mockUnpublishAll).toHaveBeenCalledTimes(0); - }); - }); - - describe("bonjour object and https", () => { - beforeEach((done) => { - server = testServer.start( - config, - { - bonjour: { - type: "http", - protocol: "udp", - }, - https: true, - port, - }, - done - ); - }); - - afterEach(async () => { - await server.stop(); - }); - - it("prefers bonjour options over devServer.https", () => { - expect(mockPublish).toHaveBeenCalledTimes(1); - expect(mockPublish).toHaveBeenCalledWith({ - name: `Webpack Dev Server ${os.hostname()}:${port}`, - port, - type: "http", - protocol: "udp", - subtypes: ["webpack"], - }); - expect(mockUnpublishAll).toHaveBeenCalledTimes(0); - }); - }); -});