diff options
Diffstat (limited to 'tst/trace_util.test.ts')
| -rw-r--r-- | tst/trace_util.test.ts | 37 |
1 files changed, 37 insertions, 0 deletions
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<any>(); + 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<any>(); + + const ok = TestTraceable.of(Either.right<Error, string>('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<any>(); + const bad = TestTraceable.of(Either.left<Error, string>(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); + }); +}); |
