Skip to content

Commit

Permalink
test: add e2e tests for setupExitSignals option (#4130)
Browse files Browse the repository at this point in the history
Co-authored-by: Alexander Akait <4567934+alexander-akait@users.noreply.github.com>
  • Loading branch information
snitin315 and alexander-akait committed Dec 22, 2021
1 parent afe4975 commit 0dd1ee6
Show file tree
Hide file tree
Showing 4 changed files with 164 additions and 76 deletions.
27 changes: 27 additions & 0 deletions 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`;
27 changes: 27 additions & 0 deletions 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`;
110 changes: 110 additions & 0 deletions 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");
});
});
});
76 changes: 0 additions & 76 deletions test/server/setupExitSignals-option.test.js

This file was deleted.

0 comments on commit 0dd1ee6

Please sign in to comment.