From 9f8a78162a213bcf2d9827489c62e3e9faddb150 Mon Sep 17 00:00:00 2001 From: Laurynas Keturakis Date: Wed, 12 Jul 2023 14:49:13 +0200 Subject: [PATCH] add tests --- packages/lib/tests/integration.test.ts | 37 +++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/packages/lib/tests/integration.test.ts b/packages/lib/tests/integration.test.ts index d8c010d6..c30c8029 100644 --- a/packages/lib/tests/integration.test.ts +++ b/packages/lib/tests/integration.test.ts @@ -1,4 +1,4 @@ -import { autometrics, init } from "../src"; +import { Autometrics, autometrics, init } from "../src"; import { describe, test, expect, beforeAll, afterEach } from "vitest"; import { AggregationTemporality, @@ -57,4 +57,39 @@ describe("Autometrics integration test", () => { expect(serialized).toMatch(errorCountMetric); }); + + test("class method", async () => { + const callCountMetric = + /function_calls_count_total\{\S*function="helloWorld"\S*module="\/packages\/lib\/tests\/integration.test.ts"\S*\} 2/gm; + const durationMetric = + /function_calls_duration_bucket\{\S*function="helloWorld"\S*module="\/packages\/lib\/tests\/integration.test.ts"\S*\}/gm; + + // @Autometrics decorator is likely to be used along-side other decorators + // this tests for any conflicts + function Bar() { + return function Bar( + target: Object, + propertyKey: string, + descriptor: PropertyDescriptor, + ) { + const originalMethod = descriptor.value; + descriptor.value = originalMethod; + }; + } + + @Autometrics() + class Foo { + @Bar() + helloWorld() {} + } + + const foo = new Foo(); + foo.helloWorld(); + foo.helloWorld(); + + const serialized = await collectAndSerialize(exporter); + + expect(serialized).toMatch(callCountMetric); + expect(serialized).toMatch(durationMetric); + }); });