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'); }); });