import { Either, Metric, TraceUtil } from '../lib/index'; import { CollectingTrace, TestTraceable } from './test_utils'; describe('trace/util (TraceUtil)', () => { test('withFunctionTrace adds fn scope', () => { const trace = new CollectingTrace(); const t = TestTraceable.of(1, trace); const out = t.flatMap(TraceUtil.withFunctionTrace(function hello() {})); out.trace.trace('x'); const flattened = trace.events.flatMap((e) => e); expect(flattened.some((x) => x === 'fn.hello')).toBe(true); }); test('traceResultingEither emits metric success/failure', () => { const metric = Metric.fromName('Job').asResult(); const trace = new CollectingTrace(); const ok = TestTraceable.of(Either.right('ok'), trace).map( TraceUtil.traceResultingEither(metric), ); ok.get(); const flattened = trace.events.flatMap((e) => e); expect(flattened.some((x: any) => typeof x === 'object' && x?.name === 'Job.success')).toBe(true); const trace2 = new CollectingTrace(); const bad = TestTraceable.of(Either.left(new Error('nope')), trace2).map( TraceUtil.traceResultingEither(metric), ); bad.get(); const flattened2 = trace2.events.flatMap((e) => e); expect(flattened2.some((x: any) => typeof x === 'object' && x?.name === 'Job.failure')).toBe(true); }); });