nestjs-otel
Version:
NestJS OpenTelemetry Library
43 lines (42 loc) • 1.95 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const api_1 = require("@opentelemetry/api");
const baggage_1 = require("./baggage");
describe("Baggage Decorator", () => {
// Mock ExecutionContext as it's not used in the factory but required by type
const mockCtx = {};
afterEach(() => {
jest.restoreAllMocks();
});
it("should return undefined if no baggage is present", () => {
jest.spyOn(api_1.propagation, "getBaggage").mockReturnValue(undefined);
const result = (0, baggage_1.baggageParamFactory)(undefined, mockCtx);
expect(result).toBeUndefined();
});
it("should return the full baggage object if no key is provided", () => {
const baggage = api_1.propagation.createBaggage({
"test-key": { value: "test-value" },
});
jest.spyOn(api_1.propagation, "getBaggage").mockReturnValue(baggage);
const result = (0, baggage_1.baggageParamFactory)(undefined, mockCtx);
expect(result).toBeDefined();
// @ts-expect-error
expect(result.getEntry("test-key")?.value).toBe("test-value");
});
it("should return a specific value if key is provided", () => {
const baggage = api_1.propagation.createBaggage({
"test-key": { value: "test-value" },
});
jest.spyOn(api_1.propagation, "getBaggage").mockReturnValue(baggage);
const result = (0, baggage_1.baggageParamFactory)("test-key", mockCtx);
expect(result).toBe("test-value");
});
it("should return undefined if key is provided but not in baggage", () => {
const baggage = api_1.propagation.createBaggage({
"other-key": { value: "other-value" },
});
jest.spyOn(api_1.propagation, "getBaggage").mockReturnValue(baggage);
const result = (0, baggage_1.baggageParamFactory)("test-key", mockCtx);
expect(result).toBeUndefined();
});
});