diff options
| author | Elizabeth Hunt <me@liz.coffee> | 2025-12-14 22:39:18 -0800 |
|---|---|---|
| committer | Elizabeth Hunt <me@liz.coffee> | 2025-12-14 22:39:18 -0800 |
| commit | 666674327f009e9b1013218fc384f193b64c6997 (patch) | |
| tree | acebae7b425b469584eb0a5bec396899c2739501 /tst/trace_util.test.ts | |
| parent | 594ce452693a71b501d3aff3f35ef3732c06c341 (diff) | |
| download | pengueno-666674327f009e9b1013218fc384f193b64c6997.tar.gz pengueno-666674327f009e9b1013218fc384f193b64c6997.zip | |
Adds unit tests
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); + }); +}); |
