/
setupExitSignals-option.test.js
76 lines (60 loc) · 1.71 KB
/
setupExitSignals-option.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
"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);
});
});
});