Skip to content

Commit 83a9a96

Browse files
committed
fix: apm tests
1 parent 6dec1a8 commit 83a9a96

File tree

3 files changed

+63
-14
lines changed

3 files changed

+63
-14
lines changed

packages/apm/src/hubextensions.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { getCurrentHub, getMainCarrier, Hub } from '@sentry/hub';
1+
import { getMainCarrier, Hub } from '@sentry/hub';
22
import { SpanContext, TransactionContext } from '@sentry/types';
33
import { logger } from '@sentry/utils';
44

@@ -62,7 +62,7 @@ function startSpan(this: Hub, context: SpanContext): Transaction | Span {
6262
// We have the check of not undefined since we defined it's ok to start a transaction with an empty name
6363
// tslint:disable-next-line: strict-type-predicates
6464
if ((context as TransactionContext).name !== undefined) {
65-
return getCurrentHub().startTransaction(context as TransactionContext);
65+
return this.startTransaction(context as TransactionContext);
6666
}
6767

6868
const scope = this.getScope();

packages/apm/test/hub.test.ts

+8-2
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,19 @@ describe('Hub', () => {
2121
});
2222
test('set tracesSampleRate 0 on transaction', () => {
2323
const hub = new Hub(new BrowserClient({ tracesSampleRate: 0 }));
24+
// @ts-ignore
2425
const transaction = hub.startSpan({ name: 'foo' }) as any;
2526
expect(transaction.sampled).toBe(false);
2627
});
2728
test('set tracesSampleRate 1 on transaction', () => {
2829
const hub = new Hub(new BrowserClient({ tracesSampleRate: 1 }));
30+
// @ts-ignore
2931
const transaction = hub.startSpan({ name: 'foo' }) as any;
3032
expect(transaction.sampled).toBeTruthy();
3133
});
3234
test('set tracesSampleRate should be propergated to children', () => {
3335
const hub = new Hub(new BrowserClient({ tracesSampleRate: 0 }));
36+
// @ts-ignore
3437
const transaction = hub.startSpan({ name: 'foo' }) as any;
3538
const child = transaction.startChild({ op: 1 });
3639
expect(child.sampled).toBeFalsy();
@@ -46,6 +49,7 @@ describe('Hub', () => {
4649

4750
test('simple standalone Transaction', () => {
4851
const hub = new Hub(new BrowserClient({ tracesSampleRate: 1 }));
52+
// @ts-ignore
4953
const transaction = hub.startSpan({ name: 'transaction' }) as Transaction;
5054
expect(transaction.spanId).toBeTruthy();
5155
// tslint:disable-next-line: no-unbound-method
@@ -59,24 +63,26 @@ describe('Hub', () => {
5963
hub.configureScope(scope => {
6064
scope.setSpan(parentSpan);
6165
});
66+
// @ts-ignore
6267
const span = hub.startSpan({ name: 'test' }) as any;
6368
expect(span.trace_id).toEqual(parentSpan.trace_id);
6469
});
6570

6671
test('create a child if there is a Span already on the scope', () => {
6772
const myScope = new Scope();
6873
const hub = new Hub(new BrowserClient({ tracesSampleRate: 1 }), myScope);
74+
// @ts-ignore
6975
const transaction = hub.startSpan({ name: 'transaction' }) as Transaction;
7076
hub.configureScope(scope => {
7177
scope.setSpan(transaction);
7278
});
73-
const span = hub.startSpan({}) as Span;
79+
const span = hub.startSpan({});
7480
expect(span.traceId).toEqual(transaction.traceId);
7581
expect(span.parentSpanId).toEqual(transaction.spanId);
7682
hub.configureScope(scope => {
7783
scope.setSpan(span);
7884
});
79-
const span2 = hub.startSpan({}) as Span;
85+
const span2 = hub.startSpan({});
8086
expect(span2.traceId).toEqual(span.traceId);
8187
expect(span2.parentSpanId).toEqual(span.spanId);
8288
});

packages/apm/test/span.test.ts

+53-10
Original file line numberDiff line numberDiff line change
@@ -180,10 +180,10 @@ describe('Span', () => {
180180
expect(span.endTimestamp).toBeGreaterThan(1);
181181
});
182182

183-
describe('hub.startSpan', () => {
183+
describe('hub.startTransaction', () => {
184184
test('finish a transaction', () => {
185185
const spy = jest.spyOn(hub as any, 'captureEvent') as any;
186-
const transaction = hub.startSpan({ name: 'test' });
186+
const transaction = hub.startTransaction({ name: 'test' });
187187
transaction.finish();
188188
expect(spy).toHaveBeenCalled();
189189
expect(spy.mock.calls[0][0].spans).toHaveLength(0);
@@ -194,7 +194,7 @@ describe('Span', () => {
194194

195195
test('finish a transaction + child span', () => {
196196
const spy = jest.spyOn(hub as any, 'captureEvent') as any;
197-
const transaction = hub.startSpan({ name: 'test' });
197+
const transaction = hub.startTransaction({ name: 'test' });
198198
const childSpan = transaction.startChild();
199199
childSpan.finish();
200200
transaction.finish();
@@ -205,16 +205,15 @@ describe('Span', () => {
205205

206206
test("finish a child span shouldn't trigger captureEvent", () => {
207207
const spy = jest.spyOn(hub as any, 'captureEvent') as any;
208-
const transaction = hub.startSpan({ name: 'test' });
208+
const transaction = hub.startTransaction({ name: 'test' });
209209
const childSpan = transaction.startChild();
210210
childSpan.finish();
211211
expect(spy).not.toHaveBeenCalled();
212212
});
213213

214214
test("finish a span with another one on the scope shouldn't override contexts.trace", () => {
215215
const spy = jest.spyOn(hub as any, 'captureEvent') as any;
216-
217-
const transaction = hub.startSpan({ name: 'test' });
216+
const transaction = hub.startTransaction({ name: 'test' });
218217
const childSpanOne = transaction.startChild();
219218
childSpanOne.finish();
220219

@@ -239,7 +238,7 @@ describe('Span', () => {
239238
}),
240239
);
241240
const spy = jest.spyOn(_hub as any, 'captureEvent') as any;
242-
const transaction = _hub.startSpan({ name: 'test' });
241+
const transaction = _hub.startTransaction({ name: 'test' });
243242
for (let i = 0; i < 10; i++) {
244243
const child = transaction.startChild();
245244
child.finish();
@@ -255,7 +254,7 @@ describe('Span', () => {
255254
}),
256255
);
257256
const spy = jest.spyOn(_hub as any, 'captureEvent') as any;
258-
const transaction = _hub.startSpan({ name: 'test' });
257+
const transaction = _hub.startTransaction({ name: 'test' });
259258
for (let i = 0; i < 10; i++) {
260259
const child = transaction.startChild();
261260
child.finish();
@@ -274,7 +273,7 @@ describe('Span', () => {
274273
);
275274
const spy = jest.spyOn(_hub as any, 'captureEvent') as any;
276275

277-
const transaction = _hub.startSpan({ name: 'test' });
276+
const transaction = _hub.startTransaction({ name: 'test' });
278277
const childSpanOne = transaction.startChild({ op: '1' });
279278
childSpanOne.finish();
280279

@@ -297,7 +296,7 @@ describe('Span', () => {
297296
test('tree structure of spans should be correct when mixing it with span on scope', () => {
298297
const spy = jest.spyOn(hub as any, 'captureEvent') as any;
299298

300-
const transaction = hub.startSpan({ name: 'test' });
299+
const transaction = hub.startTransaction({ name: 'test' });
301300
const childSpanOne = transaction.startChild();
302301

303302
const childSpanTwo = childSpanOne.startChild();
@@ -321,6 +320,50 @@ describe('Span', () => {
321320
expect(spanTwo.toJSON().parent_span_id).toEqual(transaction.toJSON().span_id);
322321
});
323322
});
323+
324+
describe('hub.startSpan', () => {
325+
test('finish a transaction', () => {
326+
const spy = jest.spyOn(hub as any, 'captureEvent') as any;
327+
// @ts-ignore
328+
const transaction = hub.startSpan({ name: 'test' });
329+
transaction.finish();
330+
expect(spy).toHaveBeenCalled();
331+
expect(spy.mock.calls[0][0].spans).toHaveLength(0);
332+
expect(spy.mock.calls[0][0].timestamp).toBeTruthy();
333+
expect(spy.mock.calls[0][0].start_timestamp).toBeTruthy();
334+
expect(spy.mock.calls[0][0].contexts.trace).toEqual(transaction.getTraceContext());
335+
});
336+
337+
test('finish a transaction (deprecated way)', () => {
338+
const spy = jest.spyOn(hub as any, 'captureEvent') as any;
339+
// @ts-ignore
340+
const transaction = hub.startSpan({ transaction: 'test' });
341+
transaction.finish();
342+
expect(spy).toHaveBeenCalled();
343+
expect(spy.mock.calls[0][0].spans).toHaveLength(0);
344+
expect(spy.mock.calls[0][0].timestamp).toBeTruthy();
345+
expect(spy.mock.calls[0][0].start_timestamp).toBeTruthy();
346+
expect(spy.mock.calls[0][0].contexts.trace).toEqual(transaction.getTraceContext());
347+
});
348+
349+
test('startSpan with Span on the Scope should be a child', () => {
350+
const spy = jest.spyOn(hub as any, 'captureEvent') as any;
351+
const transaction = hub.startTransaction({ name: 'test' });
352+
const child1 = transaction.startChild();
353+
hub.configureScope(scope => {
354+
scope.setSpan(child1);
355+
});
356+
357+
const child2 = hub.startSpan({});
358+
child1.finish();
359+
child2.finish();
360+
transaction.finish();
361+
362+
expect(spy).toHaveBeenCalled();
363+
expect(spy.mock.calls[0][0].spans).toHaveLength(2);
364+
expect(child2.parentSpanId).toEqual(child1.spanId);
365+
});
366+
});
324367
});
325368

326369
describe('getTraceContext', () => {

0 commit comments

Comments
 (0)