From 666674327f009e9b1013218fc384f193b64c6997 Mon Sep 17 00:00:00 2001 From: Elizabeth Hunt Date: Sun, 14 Dec 2025 22:39:18 -0800 Subject: Adds unit tests --- tst/trace_util.test.ts | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 tst/trace_util.test.ts (limited to 'tst/trace_util.test.ts') diff --git a/tst/trace_util.test.ts b/tst/trace_util.test.ts new file mode 100644 index 0000000..d36226d --- /dev/null +++ b/tst/trace_util.test.ts @@ -0,0 +1,37 @@ +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); + }); +}); -- cgit v1.2.3-70-g09d2