Skip to content

Commit

Permalink
add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
TalUsvyatsky committed May 7, 2024
1 parent 431707b commit 02e9ae7
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 1 deletion.
58 changes: 58 additions & 0 deletions src/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
sendDistributionMetric,
sendDistributionMetricWithDate,
_metricsQueue,
emitTelemetryOnErrorOutsideHandler,
} from "./index";
import {
incrementErrorsMetric,
Expand All @@ -21,6 +22,8 @@ import { DatadogTraceHeaders } from "./trace/context/extractor";
import { SpanContextWrapper } from "./trace/span-context-wrapper";
import { TraceSource } from "./trace/trace-context-service";
import { inflateSync } from "zlib";
import { MetricsListener } from "./metrics/listener";
import { SpanOptions, TracerWrapper } from "./trace/tracer-wrapper";

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

Expand Down Expand Up @@ -536,3 +539,58 @@ describe("sendDistributionMetricWithDate", () => {
expect(_metricsQueue.length).toBe(1);
});
});

describe("emitTelemetryOnErrorOutsideHandler", () => {
let mockedStartSpan = jest.spyOn(TracerWrapper.prototype, "startSpan");
beforeEach(() => {
jest.spyOn(MetricsListener.prototype, "onStartInvocation").mockImplementation();
jest.spyOn(TracerWrapper.prototype, "isTracerAvailable", "get").mockImplementation(() => true);
});
afterEach(() => {
mockedIncrementErrors.mockClear();
mockedStartSpan.mockClear();
});
it("emits a metric when enhanced metrics are enabled", async () => {
process.env.DD_ENHANCED_METRICS = "true";
await emitTelemetryOnErrorOutsideHandler(new ReferenceError("some error"), "myFunction", Date.now());
expect(mockedIncrementErrors).toBeCalledTimes(1);
});

it("does not emit a metric when enhanced metrics are disabled", async () => {
process.env.DD_ENHANCED_METRICS = "false";
await emitTelemetryOnErrorOutsideHandler(new ReferenceError("some error"), "myFunction", Date.now());
expect(mockedIncrementErrors).toBeCalledTimes(0);
});

it("creates a span when tracing is enabled", async () => {
process.env.DD_TRACE_ENABLED = "true";
const functionName = "myFunction";
const startTime = Date.now();
const fakeError = new ReferenceError("some error");
const spanName = "aws.lambda";

await emitTelemetryOnErrorOutsideHandler(fakeError, functionName, startTime);

const options: SpanOptions = {
tags: {
service: spanName,
operation_name: spanName,
resource_names: functionName,
"resource.name": functionName,
"span.type": "serverless",
"error.status": 500,
"error.type": fakeError.name,
"error.message": fakeError.message,
"error.stack": fakeError.stack,
},
startTime,
};
expect(mockedStartSpan).toBeCalledWith(spanName, options);
});

it("does not create a span when tracing is disabled", async () => {
process.env.DD_TRACE_ENABLED = "false";
await emitTelemetryOnErrorOutsideHandler(new ReferenceError("some error"), "myFunction", Date.now());
expect(mockedStartSpan).toBeCalledTimes(0);
});
});
1 change: 0 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,6 @@ export async function emitTelemetryOnErrorOutsideHandler(
"error.type": error.name,
"error.message": error.message,
"error.stack": error.stack,
status: error,
},
startTime,
};
Expand Down

0 comments on commit 02e9ae7

Please sign in to comment.