Skip to content

test: add e2e tests for setupExitSignals option #4130

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions test/e2e/__snapshots__/setup-exit-signals.test.js.snap.webpack4
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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.