|
| 1 | +import { BrowserClient } from '@sentry/browser'; |
| 2 | +import { Hub, makeMain, Scope } from '@sentry/hub'; |
| 3 | + |
| 4 | +import { Span, withSpan, withTransaction } from '../src'; |
| 5 | + |
| 6 | +describe('APM Helpers', () => { |
| 7 | + let hub: Hub; |
| 8 | + |
| 9 | + beforeEach(() => { |
| 10 | + jest.resetAllMocks(); |
| 11 | + const myScope = new Scope(); |
| 12 | + hub = new Hub(new BrowserClient({ tracesSampleRate: 1 }), myScope); |
| 13 | + makeMain(hub); |
| 14 | + }); |
| 15 | + |
| 16 | + describe('helpers', () => { |
| 17 | + test('withTransaction', async () => { |
| 18 | + const spy = jest.spyOn(hub as any, 'captureEvent') as any; |
| 19 | + let capturedTransaction: Span; |
| 20 | + await withTransaction('a', { op: 'op' }, async (transaction: Span) => { |
| 21 | + expect(transaction.op).toEqual('op'); |
| 22 | + capturedTransaction = transaction; |
| 23 | + }); |
| 24 | + expect(spy).toHaveBeenCalled(); |
| 25 | + expect(spy.mock.calls[0][0].spans).toHaveLength(0); |
| 26 | + expect(spy.mock.calls[0][0].contexts.trace).toEqual(capturedTransaction!.getTraceContext()); |
| 27 | + }); |
| 28 | + |
| 29 | + test('withTransaction + withSpan', async () => { |
| 30 | + const spy = jest.spyOn(hub as any, 'captureEvent') as any; |
| 31 | + await withTransaction('a', { op: 'op' }, async (transaction: Span) => { |
| 32 | + await transaction.withChild({ |
| 33 | + op: 'sub', |
| 34 | + }); |
| 35 | + }); |
| 36 | + expect(spy).toHaveBeenCalled(); |
| 37 | + expect(spy.mock.calls[0][0].spans).toHaveLength(1); |
| 38 | + expect(spy.mock.calls[0][0].spans[0].op).toEqual('sub'); |
| 39 | + }); |
| 40 | + |
| 41 | + test('withSpan', async () => { |
| 42 | + const spy = jest.spyOn(hub as any, 'captureEvent') as any; |
| 43 | + |
| 44 | + // Setting transaction on the scope |
| 45 | + const transaction = hub.startSpan({ |
| 46 | + transaction: 'transaction', |
| 47 | + }); |
| 48 | + hub.configureScope((scope: Scope) => { |
| 49 | + scope.setSpan(transaction); |
| 50 | + }); |
| 51 | + |
| 52 | + let capturedSpan: Span; |
| 53 | + await withSpan({ op: 'op' }, async (span: Span) => { |
| 54 | + expect(span.op).toEqual('op'); |
| 55 | + capturedSpan = span; |
| 56 | + }); |
| 57 | + expect(spy).not.toHaveBeenCalled(); |
| 58 | + expect(capturedSpan!.op).toEqual('op'); |
| 59 | + }); |
| 60 | + |
| 61 | + test('withTransaction + withSpan + timing', async () => { |
| 62 | + jest.useRealTimers(); |
| 63 | + const spy = jest.spyOn(hub as any, 'captureEvent') as any; |
| 64 | + await withTransaction('a', { op: 'op' }, async (transaction: Span) => { |
| 65 | + await transaction.withChild( |
| 66 | + { |
| 67 | + op: 'sub', |
| 68 | + }, |
| 69 | + async () => { |
| 70 | + const ret = new Promise<void>((resolve: any) => { |
| 71 | + setTimeout(() => { |
| 72 | + resolve(); |
| 73 | + }, 1100); |
| 74 | + }); |
| 75 | + return ret; |
| 76 | + }, |
| 77 | + ); |
| 78 | + }); |
| 79 | + expect(spy).toHaveBeenCalled(); |
| 80 | + expect(spy.mock.calls[0][0].spans).toHaveLength(1); |
| 81 | + expect(spy.mock.calls[0][0].spans[0].op).toEqual('sub'); |
| 82 | + const duration = spy.mock.calls[0][0].spans[0].timestamp - spy.mock.calls[0][0].spans[0].startTimestamp; |
| 83 | + expect(duration).toBeGreaterThanOrEqual(1); |
| 84 | + }); |
| 85 | + }); |
| 86 | +}); |
0 commit comments