|
1 |
| -import { registerRequestInstrumentation } from '../../src/browser/request'; |
| 1 | +import { BrowserClient } from '@sentry/browser'; |
| 2 | +import { Hub, makeMain } from '@sentry/hub'; |
| 3 | + |
| 4 | +import { Span, Transaction } from '../../src'; |
| 5 | +import { _fetchCallback, FetchData, registerRequestInstrumentation } from '../../src/browser/request'; |
| 6 | +import { addExtensionMethods } from '../../src/hubextensions'; |
| 7 | + |
| 8 | +declare global { |
| 9 | + namespace NodeJS { |
| 10 | + // tslint:disable-next-line: completed-docs |
| 11 | + interface Global { |
| 12 | + // Have to mock out Request because it is not defined in jest environment |
| 13 | + Request: Request; |
| 14 | + } |
| 15 | + } |
| 16 | +} |
| 17 | + |
| 18 | +beforeAll(() => { |
| 19 | + addExtensionMethods(); |
| 20 | + // @ts-ignore |
| 21 | + global.Request = {}; |
| 22 | +}); |
2 | 23 |
|
3 | 24 | const mockAddInstrumentationHandler = jest.fn();
|
4 | 25 | let mockFetchCallback = jest.fn();
|
@@ -47,3 +68,91 @@ describe('registerRequestInstrumentation', () => {
|
47 | 68 | expect(mockXHRCallback()).toBe(undefined);
|
48 | 69 | });
|
49 | 70 | });
|
| 71 | + |
| 72 | +describe('_fetchCallback()', () => { |
| 73 | + let hub: Hub; |
| 74 | + let transaction: Transaction; |
| 75 | + beforeAll(() => { |
| 76 | + hub = new Hub(new BrowserClient({ tracesSampleRate: 1 })); |
| 77 | + makeMain(hub); |
| 78 | + }); |
| 79 | + |
| 80 | + beforeEach(() => { |
| 81 | + transaction = hub.startTransaction({ name: 'organizations/users/:userid', op: 'pageload' }) as Transaction; |
| 82 | + hub.configureScope(scope => scope.setSpan(transaction)); |
| 83 | + }); |
| 84 | + |
| 85 | + it('does not create span if it should not be created', () => { |
| 86 | + const shouldCreateSpan = (url: string): boolean => url === '/organizations'; |
| 87 | + const data: FetchData = { |
| 88 | + args: ['/users'], |
| 89 | + fetchData: { |
| 90 | + method: 'GET', |
| 91 | + url: '/users', |
| 92 | + }, |
| 93 | + startTimestamp: 1595509730275, |
| 94 | + }; |
| 95 | + const spans = {}; |
| 96 | + |
| 97 | + _fetchCallback(data, shouldCreateSpan, spans); |
| 98 | + expect(spans).toEqual({}); |
| 99 | + }); |
| 100 | + |
| 101 | + it('does not create span if there is no fetch data', () => { |
| 102 | + const shouldCreateSpan = (_: string): boolean => true; |
| 103 | + const data: FetchData = { |
| 104 | + args: [], |
| 105 | + startTimestamp: 1595509730275, |
| 106 | + }; |
| 107 | + const spans = {}; |
| 108 | + |
| 109 | + _fetchCallback(data, shouldCreateSpan, spans); |
| 110 | + expect(spans).toEqual({}); |
| 111 | + }); |
| 112 | + |
| 113 | + it('creates and finishes fetch span on active transaction', () => { |
| 114 | + const shouldCreateSpan = (_: string): boolean => true; |
| 115 | + const data: FetchData = { |
| 116 | + args: ['/users'], |
| 117 | + fetchData: { |
| 118 | + method: 'GET', |
| 119 | + url: '/users', |
| 120 | + }, |
| 121 | + startTimestamp: 1595509730275, |
| 122 | + }; |
| 123 | + const spans: Record<string, Span> = {}; |
| 124 | + |
| 125 | + // Start fetch request |
| 126 | + _fetchCallback(data, shouldCreateSpan, spans); |
| 127 | + const spanKey = Object.keys(spans)[0]; |
| 128 | + |
| 129 | + const fetchSpan = spans[spanKey]; |
| 130 | + expect(fetchSpan).toBeInstanceOf(Span); |
| 131 | + expect(fetchSpan.data).toEqual({ |
| 132 | + method: 'GET', |
| 133 | + type: 'fetch', |
| 134 | + url: '/users', |
| 135 | + }); |
| 136 | + expect(fetchSpan.description).toBe('GET /users'); |
| 137 | + expect(fetchSpan.op).toBe('http'); |
| 138 | + if (data.fetchData) { |
| 139 | + expect(data.fetchData.__span).toBeDefined(); |
| 140 | + } else { |
| 141 | + fail('Fetch data does not exist'); |
| 142 | + } |
| 143 | + |
| 144 | + const newData = { |
| 145 | + ...data, |
| 146 | + endTimestamp: data.startTimestamp + 12343234, |
| 147 | + }; |
| 148 | + |
| 149 | + // End fetch request |
| 150 | + _fetchCallback(newData, shouldCreateSpan, spans); |
| 151 | + expect(spans).toEqual({}); |
| 152 | + if (transaction.spanRecorder) { |
| 153 | + expect(transaction.spanRecorder.spans[1].endTimestamp).toBeDefined(); |
| 154 | + } else { |
| 155 | + fail('Transaction does not have span recorder'); |
| 156 | + } |
| 157 | + }); |
| 158 | +}); |
0 commit comments