Skip to content

Commit 0dd1ee6

Browse files
test: add e2e tests for setupExitSignals option (#4130)
Co-authored-by: Alexander Akait <[email protected]>
1 parent afe4975 commit 0dd1ee6

4 files changed

+164
-76
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`setupExitSignals option should handle 'SIGINT' and 'SIGTERM' signals should close and exit on SIGINT: console messages 1`] = `
4+
Array [
5+
"[HMR] Waiting for update signal from WDS...",
6+
"Hey.",
7+
"[webpack-dev-server] Hot Module Replacement enabled.",
8+
"[webpack-dev-server] Live Reloading enabled.",
9+
]
10+
`;
11+
12+
exports[`setupExitSignals option should handle 'SIGINT' and 'SIGTERM' signals should close and exit on SIGINT: page errors 1`] = `Array []`;
13+
14+
exports[`setupExitSignals option should handle 'SIGINT' and 'SIGTERM' signals should close and exit on SIGINT: response status 1`] = `200`;
15+
16+
exports[`setupExitSignals option should handle 'SIGINT' and 'SIGTERM' signals should close and exit on SIGTERM: console messages 1`] = `
17+
Array [
18+
"[HMR] Waiting for update signal from WDS...",
19+
"Hey.",
20+
"[webpack-dev-server] Hot Module Replacement enabled.",
21+
"[webpack-dev-server] Live Reloading enabled.",
22+
]
23+
`;
24+
25+
exports[`setupExitSignals option should handle 'SIGINT' and 'SIGTERM' signals should close and exit on SIGTERM: page errors 1`] = `Array []`;
26+
27+
exports[`setupExitSignals option should handle 'SIGINT' and 'SIGTERM' signals should close and exit on SIGTERM: response status 1`] = `200`;
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`setupExitSignals option should handle 'SIGINT' and 'SIGTERM' signals should close and exit on SIGINT: console messages 1`] = `
4+
Array [
5+
"[HMR] Waiting for update signal from WDS...",
6+
"Hey.",
7+
"[webpack-dev-server] Hot Module Replacement enabled.",
8+
"[webpack-dev-server] Live Reloading enabled.",
9+
]
10+
`;
11+
12+
exports[`setupExitSignals option should handle 'SIGINT' and 'SIGTERM' signals should close and exit on SIGINT: page errors 1`] = `Array []`;
13+
14+
exports[`setupExitSignals option should handle 'SIGINT' and 'SIGTERM' signals should close and exit on SIGINT: response status 1`] = `200`;
15+
16+
exports[`setupExitSignals option should handle 'SIGINT' and 'SIGTERM' signals should close and exit on SIGTERM: console messages 1`] = `
17+
Array [
18+
"[HMR] Waiting for update signal from WDS...",
19+
"Hey.",
20+
"[webpack-dev-server] Hot Module Replacement enabled.",
21+
"[webpack-dev-server] Live Reloading enabled.",
22+
]
23+
`;
24+
25+
exports[`setupExitSignals option should handle 'SIGINT' and 'SIGTERM' signals should close and exit on SIGTERM: page errors 1`] = `Array []`;
26+
27+
exports[`setupExitSignals option should handle 'SIGINT' and 'SIGTERM' signals should close and exit on SIGTERM: response status 1`] = `200`;

test/e2e/setup-exit-signals.test.js

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
"use strict";
2+
3+
const webpack = require("webpack");
4+
const Server = require("../../lib/Server");
5+
const config = require("../fixtures/simple-config/webpack.config");
6+
const runBrowser = require("../helpers/run-browser");
7+
const port = require("../ports-map")["setup-exit-signals-option"];
8+
9+
describe("setupExitSignals option", () => {
10+
describe("should handle 'SIGINT' and 'SIGTERM' signals", () => {
11+
let compiler;
12+
let server;
13+
let page;
14+
let browser;
15+
let pageErrors;
16+
let consoleMessages;
17+
let doExit;
18+
let exitSpy;
19+
let stopCallbackSpy;
20+
let stdinResumeSpy;
21+
let closeCallbackSpy;
22+
23+
const signals = ["SIGINT", "SIGTERM"];
24+
25+
beforeEach(async () => {
26+
compiler = webpack(config);
27+
28+
server = new Server(
29+
{
30+
setupExitSignals: true,
31+
port,
32+
},
33+
compiler
34+
);
35+
36+
await server.start();
37+
38+
({ page, browser } = await runBrowser());
39+
40+
pageErrors = [];
41+
consoleMessages = [];
42+
doExit = false;
43+
44+
exitSpy = jest.spyOn(process, "exit").mockImplementation(() => {
45+
doExit = true;
46+
});
47+
48+
stdinResumeSpy = jest
49+
.spyOn(process.stdin, "resume")
50+
.mockImplementation(() => {});
51+
52+
stopCallbackSpy = jest.spyOn(server, "stopCallback");
53+
54+
if (server.compiler.close) {
55+
closeCallbackSpy = jest.spyOn(server.compiler, "close");
56+
}
57+
});
58+
59+
afterEach(async () => {
60+
exitSpy.mockReset();
61+
stdinResumeSpy.mockReset();
62+
signals.forEach((signal) => {
63+
process.removeAllListeners(signal);
64+
});
65+
process.stdin.removeAllListeners("end");
66+
await browser.close();
67+
await server.stop();
68+
});
69+
70+
it.each(signals)("should close and exit on %s", async (signal) => {
71+
page
72+
.on("console", (message) => {
73+
consoleMessages.push(message);
74+
})
75+
.on("pageerror", (error) => {
76+
pageErrors.push(error);
77+
});
78+
79+
const response = await page.goto(`http://127.0.0.1:${port}/main`, {
80+
waitUntil: "networkidle0",
81+
});
82+
83+
expect(response.status()).toMatchSnapshot("response status");
84+
85+
process.emit(signal);
86+
87+
await new Promise((resolve) => {
88+
const interval = setInterval(() => {
89+
if (doExit) {
90+
expect(stopCallbackSpy.mock.calls.length).toEqual(1);
91+
92+
if (server.compiler.close) {
93+
expect(closeCallbackSpy.mock.calls.length).toEqual(1);
94+
}
95+
96+
clearInterval(interval);
97+
98+
resolve();
99+
}
100+
}, 100);
101+
});
102+
103+
expect(consoleMessages.map((message) => message.text())).toMatchSnapshot(
104+
"console messages"
105+
);
106+
107+
expect(pageErrors).toMatchSnapshot("page errors");
108+
});
109+
});
110+
});

test/server/setupExitSignals-option.test.js

Lines changed: 0 additions & 76 deletions
This file was deleted.

0 commit comments

Comments
 (0)