-
-
Notifications
You must be signed in to change notification settings - Fork 814
Expand file tree
/
Copy pathshutdown.test.ts
More file actions
49 lines (40 loc) · 1.48 KB
/
shutdown.test.ts
File metadata and controls
49 lines (40 loc) · 1.48 KB
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
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
const callHook = vi.fn();
vi.mock("../../src/runtime/internal/app.ts", () => ({
useNitroApp: () => ({
hooks: { callHook },
}),
}));
import { setupShutdownHooks } from "../../src/runtime/internal/shutdown.ts";
describe("setupShutdownHooks", () => {
let savedSIGTERM: Function[];
let savedSIGINT: Function[];
beforeEach(() => {
savedSIGTERM = process.listeners("SIGTERM").slice();
savedSIGINT = process.listeners("SIGINT").slice();
callHook.mockClear();
});
afterEach(() => {
process.removeAllListeners("SIGTERM");
process.removeAllListeners("SIGINT");
for (const fn of savedSIGTERM) process.on("SIGTERM", fn as NodeJS.SignalsListener);
for (const fn of savedSIGINT) process.on("SIGINT", fn as NodeJS.SignalsListener);
});
it("registers SIGTERM and SIGINT handlers", () => {
const beforeTERM = process.listenerCount("SIGTERM");
const beforeINT = process.listenerCount("SIGINT");
setupShutdownHooks();
expect(process.listenerCount("SIGTERM")).toBe(beforeTERM + 1);
expect(process.listenerCount("SIGINT")).toBe(beforeINT + 1);
});
it("calls close hook on SIGTERM", () => {
setupShutdownHooks();
process.emit("SIGTERM", "SIGTERM");
expect(callHook).toHaveBeenCalledWith("close");
});
it("calls close hook on SIGINT", () => {
setupShutdownHooks();
process.emit("SIGINT", "SIGINT");
expect(callHook).toHaveBeenCalledWith("close");
});
});