@ogcio/o11y-sdk-node
Version:
Opentelemetry standard instrumentation SDK for NodeJS based project
46 lines (35 loc) • 1.32 kB
text/typescript
import { describe, it, expect, vi } from "vitest";
import { _shutdownHook } from "../../lib/internals/hooks";
describe("shutdown hook", () => {
it("should call sdk.shutdown and log success on SIGTERM", async () => {
const shutdownMock = vi.fn().mockResolvedValue(undefined);
const consoleLogMock = vi
.spyOn(console, "log")
.mockImplementation(() => {});
_shutdownHook({ shutdown: shutdownMock });
process.emit("SIGTERM");
// Wait for async logic
await Promise.resolve();
expect(shutdownMock).toHaveBeenCalled();
expect(consoleLogMock).toHaveBeenCalledWith(
"NodeJS OpenTelemetry instrumentation shutdown successfully",
);
consoleLogMock.mockRestore();
});
it("should catch and log errors on shutdown failure", async () => {
const error = new Error("Shutdown failed");
const shutdownMock = vi.fn().mockRejectedValue(error);
const consoleErrorMock = vi
.spyOn(console, "error")
.mockImplementation(() => {});
_shutdownHook({ shutdown: shutdownMock });
process.emit("SIGTERM");
// Wait for async logic
await Promise.resolve();
expect(consoleErrorMock).toHaveBeenCalledWith(
"Error shutting down NodeJS OpenTelemetry instrumentation:",
error,
);
consoleErrorMock.mockRestore();
});
});