|
| 1 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
| 2 | + |
| 3 | +const callHook = vi.fn(); |
| 4 | + |
| 5 | +vi.mock("../../src/runtime/internal/app.ts", () => ({ |
| 6 | + useNitroApp: () => ({ |
| 7 | + hooks: { callHook }, |
| 8 | + }), |
| 9 | +})); |
| 10 | + |
| 11 | +import { setupShutdownHooks } from "../../src/runtime/internal/shutdown.ts"; |
| 12 | + |
| 13 | +describe("setupShutdownHooks", () => { |
| 14 | + let savedSIGTERM: Function[]; |
| 15 | + let savedSIGINT: Function[]; |
| 16 | + |
| 17 | + beforeEach(() => { |
| 18 | + savedSIGTERM = process.listeners("SIGTERM").slice(); |
| 19 | + savedSIGINT = process.listeners("SIGINT").slice(); |
| 20 | + callHook.mockClear(); |
| 21 | + }); |
| 22 | + |
| 23 | + afterEach(() => { |
| 24 | + process.removeAllListeners("SIGTERM"); |
| 25 | + process.removeAllListeners("SIGINT"); |
| 26 | + for (const fn of savedSIGTERM) process.on("SIGTERM", fn as NodeJS.SignalsListener); |
| 27 | + for (const fn of savedSIGINT) process.on("SIGINT", fn as NodeJS.SignalsListener); |
| 28 | + }); |
| 29 | + |
| 30 | + it("registers SIGTERM and SIGINT handlers", () => { |
| 31 | + const beforeTERM = process.listenerCount("SIGTERM"); |
| 32 | + const beforeINT = process.listenerCount("SIGINT"); |
| 33 | + setupShutdownHooks(); |
| 34 | + expect(process.listenerCount("SIGTERM")).toBe(beforeTERM + 1); |
| 35 | + expect(process.listenerCount("SIGINT")).toBe(beforeINT + 1); |
| 36 | + }); |
| 37 | + |
| 38 | + it("calls close hook on SIGTERM", () => { |
| 39 | + setupShutdownHooks(); |
| 40 | + process.emit("SIGTERM", "SIGTERM"); |
| 41 | + expect(callHook).toHaveBeenCalledWith("close"); |
| 42 | + }); |
| 43 | + |
| 44 | + it("calls close hook on SIGINT", () => { |
| 45 | + setupShutdownHooks(); |
| 46 | + process.emit("SIGINT", "SIGINT"); |
| 47 | + expect(callHook).toHaveBeenCalledWith("close"); |
| 48 | + }); |
| 49 | +}); |
0 commit comments