summaryrefslogtreecommitdiff
path: root/tst/trace_util.test.ts
blob: d36226d8b78dd4368f7e6205f6e8fa7c66715632 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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);
    });
});