summaryrefslogtreecommitdiff
path: root/tst/metric.test.ts
diff options
context:
space:
mode:
Diffstat (limited to 'tst/metric.test.ts')
-rw-r--r--tst/metric.test.ts53
1 files changed, 53 insertions, 0 deletions
diff --git a/tst/metric.test.ts b/tst/metric.test.ts
new file mode 100644
index 0000000..b55be40
--- /dev/null
+++ b/tst/metric.test.ts
@@ -0,0 +1,53 @@
+import { EmittableMetric, Metric, MetricValueTag, ResultMetric, Unit, isIMetric, isMetricValue } from '../lib/index';
+
+describe('trace/metric', () => {
+ beforeEach(() => {
+ jest.useFakeTimers();
+ jest.setSystemTime(new Date('2020-01-01T00:00:00.000Z'));
+ });
+
+ afterEach(() => {
+ jest.useRealTimers();
+ });
+
+ test('EmittableMetric.withValue creates MetricValue', () => {
+ const m = new EmittableMetric('x', Unit.COUNT);
+ const v = m.withValue(2.5);
+ expect(isMetricValue(v)).toBe(true);
+ expect(v).toEqual({
+ _tag: MetricValueTag,
+ name: 'x',
+ unit: Unit.COUNT,
+ value: 2.5,
+ emissionTimestamp: 1577836800000,
+ });
+ });
+
+ test('Metric.join/fromName/child set names and parents', () => {
+ expect(Metric.join('a', 'b', 'c')).toBe('a.b.c');
+
+ const root = Metric.fromName('root');
+ expect(isIMetric(root)).toBe(true);
+ expect(root.count.name).toBe('root.count');
+ expect(root.time.name).toBe('root.time');
+ expect(root.parent).toBeUndefined();
+
+ const child = root.child('leaf');
+ expect(child.name).toBe('root.leaf');
+ expect(child.parent).toBe(root);
+ });
+
+ test('ResultMetric.from creates failure/success/warn children', () => {
+ const metric = Metric.fromName('work');
+ const result = ResultMetric.from(metric);
+
+ expect(result.name).toBe('work');
+ expect(result.failure.name).toBe('work.failure');
+ expect(result.success.name).toBe('work.success');
+ expect(result.warn.name).toBe('work.warn');
+
+ expect(result.failure.parent!.name).toBe('work');
+ expect(result.success.parent!.name).toBe('work');
+ expect(result.warn.parent!.name).toBe('work');
+ });
+});