Skip to content
This repository has been archived by the owner on Nov 10, 2022. It is now read-only.

Commit

Permalink
test: replace tracer implementation with sinon stub
Browse files Browse the repository at this point in the history
Signed-off-by: naseemkullah <naseem@transit.app>
  • Loading branch information
naseemkullah committed May 14, 2021
1 parent 6c43d6e commit 58cf7eb
Showing 1 changed file with 22 additions and 38 deletions.
60 changes: 22 additions & 38 deletions test/proxy-implementations/proxy-tracer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,46 +94,13 @@ describe('ProxyTracer', () => {

beforeEach(() => {
delegateSpan = new NonRecordingSpan();

delegateTracer = {
startSpan() {
return delegateSpan;
},

startActiveSpan<F extends (span: Span) => ReturnType<F>>(
name: string,
arg2: F | SpanOptions,
arg3?: F | Context,
arg4?: F
): ReturnType<F> | undefined {
let fn: F | undefined,
options: SpanOptions | undefined,
activeContext: Context | undefined;
if (arguments.length === 2 && typeof arg2 === 'function') {
fn = arg2;
} else if (
arguments.length === 3 &&
typeof arg2 === 'object' &&
typeof arg3 === 'function'
) {
options = arg2;
fn = arg3;
} else if (
arguments.length === 4 &&
typeof arg2 === 'object' &&
typeof arg3 === 'object' &&
typeof arg4 === 'function'
) {
options = arg2;
activeContext = arg3;
fn = arg4;
}

const activeCtx = activeContext ?? context.active();
const span = this.startSpan(name, options, activeCtx);

if (fn) {
return context.with(setSpan(activeCtx, span), fn, undefined, span);
}
startActiveSpan() {
return;
},
};
Expand All @@ -155,9 +122,26 @@ describe('ProxyTracer', () => {
});

it('should create active spans using the delegate tracer', () => {
tracer.startActiveSpan('test', span => {
assert.strictEqual(getSpan(context.active()), span);
});
const startActiveSpanStub = sinon.stub();

const name = 'span-name';
const fn = (span: Span) => {
try {
return 1;
} finally {
span.end();
}
};
const opts = { attributes: { foo: 'bar' } };
const ctx = context.active();

startActiveSpanStub.withArgs(name, fn).returns(1);
startActiveSpanStub.withArgs(name, opts, fn).returns(1);
startActiveSpanStub.withArgs(name, opts, ctx, fn).returns(1);

assert.strictEqual(tracer.startActiveSpan(name, fn), 1);
assert.strictEqual(tracer.startActiveSpan(name, opts, fn), 1);
assert.strictEqual(tracer.startActiveSpan(name, opts, ctx, fn), 1);
});

it('should pass original arguments to DelegateTracer#startSpan', () => {
Expand Down

0 comments on commit 58cf7eb

Please sign in to comment.