Skip to content

Commit

Permalink
add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nhulston committed Oct 25, 2024
1 parent 7d54dcc commit 97937ec
Showing 1 changed file with 62 additions and 0 deletions.
62 changes: 62 additions & 0 deletions src/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { TraceSource } from "./trace/trace-context-service";
import { inflateSync } from "zlib";
import { MetricsListener } from "./metrics/listener";
import { SpanOptions, TracerWrapper } from "./trace/tracer-wrapper";
import fs from "fs";

jest.mock("./metrics/enhanced-metrics");

Expand Down Expand Up @@ -623,3 +624,64 @@ describe("emitTelemetryOnErrorOutsideHandler", () => {
expect(mockedStartSpan).toBeCalledTimes(0);
});
});

describe("detectDuplicateInstallations", () => {
jest.mock("fs");

let fsAccessMock: jest.SpyInstance;
let logWarningMock: jest.SpyInstance;

beforeEach(() => {
jest.resetAllMocks();
fsAccessMock = jest.spyOn(fs, "access");
logWarningMock = jest.spyOn(require("./utils"), "logWarning").mockImplementation(() => {});
});

it("should log warning when duplicate installations are detected", async () => {
// Mock fs.access to simulate both paths exist
fsAccessMock.mockImplementation((path: string, callback: any) => {
callback(null); // No error = path exists
});

await datadog(async () => {}, { forceWrap: true })();
expect(logWarningMock).toHaveBeenCalledWith(expect.stringContaining("Detected duplicate installations"));
});

it("should not log warning when only layer installation exists", async () => {
// Simulate layerPath exists, localPath does not exist
fsAccessMock.mockImplementation((path: string, callback: any) => {
if (path.includes("/opt/nodejs")) {
callback(null); // Exists
} else {
callback(new Error("ENOENT")); // Does not exist
}
});

await datadog(async () => {}, { forceWrap: true })();
expect(logWarningMock).not.toHaveBeenCalled();
});

it("should not log warning when only local installation exists", async () => {
// Simulate localPath exists, layerPath does not exist
fsAccessMock.mockImplementation((path: string, callback: any) => {
if (path.includes("/opt/nodejs")) {
callback(new Error("ENOENT")); // Does not exist
} else {
callback(null); // Exists
}
});

await datadog(async () => {}, { forceWrap: true })();
expect(logWarningMock).not.toHaveBeenCalled();
});

it("should not log warning when neither installation exists", async () => {
// Simulate neither path exists
fsAccessMock.mockImplementation((path: string, callback: any) => {
callback(new Error("ENOENT")); // Does not exist
});

await datadog(async () => {}, { forceWrap: true })();
expect(logWarningMock).not.toHaveBeenCalled();
});
});

0 comments on commit 97937ec

Please sign in to comment.