summaryrefslogtreecommitdiff
path: root/tst/metric.test.ts
blob: b55be408c8e07c17bbd9df540f6e83936c44b938 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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');
    });
});