From 0dd1ee6dcff7245eb15b0ca980953e2154cf77a5 Mon Sep 17 00:00:00 2001 From: Nitin Kumar Date: Wed, 22 Dec 2021 23:29:25 +0530 Subject: [PATCH] test: add e2e tests for `setupExitSignals` option (#4130) Co-authored-by: Alexander Akait <4567934+alexander-akait@users.noreply.github.com> --- .../setup-exit-signals.test.js.snap.webpack4 | 27 +++++ .../setup-exit-signals.test.js.snap.webpack5 | 27 +++++ test/e2e/setup-exit-signals.test.js | 110 ++++++++++++++++++ test/server/setupExitSignals-option.test.js | 76 ------------ 4 files changed, 164 insertions(+), 76 deletions(-) create mode 100644 test/e2e/__snapshots__/setup-exit-signals.test.js.snap.webpack4 create mode 100644 test/e2e/__snapshots__/setup-exit-signals.test.js.snap.webpack5 create mode 100644 test/e2e/setup-exit-signals.test.js delete mode 100644 test/server/setupExitSignals-option.test.js diff --git a/test/e2e/__snapshots__/setup-exit-signals.test.js.snap.webpack4 b/test/e2e/__snapshots__/setup-exit-signals.test.js.snap.webpack4 new file mode 100644 index 0000000000..e4534097ac --- /dev/null +++ b/test/e2e/__snapshots__/setup-exit-signals.test.js.snap.webpack4 @@ -0,0 +1,27 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`setupExitSignals option should handle 'SIGINT' and 'SIGTERM' signals should close and exit on SIGINT: 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[`setupExitSignals option should handle 'SIGINT' and 'SIGTERM' signals should close and exit on SIGINT: page errors 1`] = `Array []`; + +exports[`setupExitSignals option should handle 'SIGINT' and 'SIGTERM' signals should close and exit on SIGINT: response status 1`] = `200`; + +exports[`setupExitSignals option should handle 'SIGINT' and 'SIGTERM' signals should close and exit on SIGTERM: 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[`setupExitSignals option should handle 'SIGINT' and 'SIGTERM' signals should close and exit on SIGTERM: page errors 1`] = `Array []`; + +exports[`setupExitSignals option should handle 'SIGINT' and 'SIGTERM' signals should close and exit on SIGTERM: response status 1`] = `200`; diff --git a/test/e2e/__snapshots__/setup-exit-signals.test.js.snap.webpack5 b/test/e2e/__snapshots__/setup-exit-signals.test.js.snap.webpack5 new file mode 100644 index 0000000000..e4534097ac --- /dev/null +++ b/test/e2e/__snapshots__/setup-exit-signals.test.js.snap.webpack5 @@ -0,0 +1,27 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`setupExitSignals option should handle 'SIGINT' and 'SIGTERM' signals should close and exit on SIGINT: 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[`setupExitSignals option should handle 'SIGINT' and 'SIGTERM' signals should close and exit on SIGINT: page errors 1`] = `Array []`; + +exports[`setupExitSignals option should handle 'SIGINT' and 'SIGTERM' signals should close and exit on SIGINT: response status 1`] = `200`; + +exports[`setupExitSignals option should handle 'SIGINT' and 'SIGTERM' signals should close and exit on SIGTERM: 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[`setupExitSignals option should handle 'SIGINT' and 'SIGTERM' signals should close and exit on SIGTERM: page errors 1`] = `Array []`; + +exports[`setupExitSignals option should handle 'SIGINT' and 'SIGTERM' signals should close and exit on SIGTERM: response status 1`] = `200`; diff --git a/test/e2e/setup-exit-signals.test.js b/test/e2e/setup-exit-signals.test.js new file mode 100644 index 0000000000..d3fae081f8 --- /dev/null +++ b/test/e2e/setup-exit-signals.test.js @@ -0,0 +1,110 @@ +"use strict"; + +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")["setup-exit-signals-option"]; + +describe("setupExitSignals option", () => { + describe("should handle 'SIGINT' and 'SIGTERM' signals", () => { + let compiler; + let server; + let page; + let browser; + let pageErrors; + let consoleMessages; + let doExit; + let exitSpy; + let stopCallbackSpy; + let stdinResumeSpy; + let closeCallbackSpy; + + const signals = ["SIGINT", "SIGTERM"]; + + beforeEach(async () => { + compiler = webpack(config); + + server = new Server( + { + setupExitSignals: true, + port, + }, + compiler + ); + + await server.start(); + + ({ page, browser } = await runBrowser()); + + pageErrors = []; + consoleMessages = []; + doExit = false; + + exitSpy = jest.spyOn(process, "exit").mockImplementation(() => { + doExit = true; + }); + + stdinResumeSpy = jest + .spyOn(process.stdin, "resume") + .mockImplementation(() => {}); + + stopCallbackSpy = jest.spyOn(server, "stopCallback"); + + if (server.compiler.close) { + closeCallbackSpy = jest.spyOn(server.compiler, "close"); + } + }); + + afterEach(async () => { + exitSpy.mockReset(); + stdinResumeSpy.mockReset(); + signals.forEach((signal) => { + process.removeAllListeners(signal); + }); + process.stdin.removeAllListeners("end"); + await browser.close(); + await server.stop(); + }); + + it.each(signals)("should close and exit on %s", async (signal) => { + 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`, { + waitUntil: "networkidle0", + }); + + expect(response.status()).toMatchSnapshot("response status"); + + process.emit(signal); + + await new Promise((resolve) => { + const interval = setInterval(() => { + if (doExit) { + expect(stopCallbackSpy.mock.calls.length).toEqual(1); + + if (server.compiler.close) { + expect(closeCallbackSpy.mock.calls.length).toEqual(1); + } + + clearInterval(interval); + + resolve(); + } + }, 100); + }); + + expect(consoleMessages.map((message) => message.text())).toMatchSnapshot( + "console messages" + ); + + expect(pageErrors).toMatchSnapshot("page errors"); + }); + }); +}); diff --git a/test/server/setupExitSignals-option.test.js b/test/server/setupExitSignals-option.test.js deleted file mode 100644 index 8cd84ed906..0000000000 --- a/test/server/setupExitSignals-option.test.js +++ /dev/null @@ -1,76 +0,0 @@ -"use strict"; - -const webpack = require("webpack"); -const Server = require("../../lib/Server"); -const config = require("../fixtures/simple-config/webpack.config"); -const port = require("../ports-map")["setup-exit-signals-option"]; - -describe("setupExitSignals option", () => { - let server; - let doExit; - let exitSpy; - let stopCallbackSpy; - let stdinResumeSpy; - let closeCallbackSpy; - - const signals = ["SIGINT", "SIGTERM"]; - - beforeEach(async () => { - const compiler = webpack(config); - - server = new Server( - { - setupExitSignals: true, - port, - }, - compiler - ); - - await server.start(); - - doExit = false; - - exitSpy = jest.spyOn(process, "exit").mockImplementation(() => { - doExit = true; - }); - stdinResumeSpy = jest - .spyOn(process.stdin, "resume") - .mockImplementation(() => {}); - stopCallbackSpy = jest.spyOn(server, "stopCallback"); - - if (server.compiler.close) { - closeCallbackSpy = jest.spyOn(server.compiler, "close"); - } - }); - - afterEach(async () => { - exitSpy.mockReset(); - stdinResumeSpy.mockReset(); - signals.forEach((signal) => { - process.removeAllListeners(signal); - }); - process.stdin.removeAllListeners("end"); - - await server.stop(); - }); - - it.each(signals)("should close and exit on %s", async (signal) => { - process.emit(signal); - - await new Promise((resolve) => { - const interval = setInterval(() => { - if (doExit) { - expect(stopCallbackSpy.mock.calls.length).toEqual(1); - - if (server.compiler.close) { - expect(closeCallbackSpy.mock.calls.length).toEqual(1); - } - - clearInterval(interval); - - resolve(); - } - }, 100); - }); - }); -});