|
| 1 | +import * as coreSdk from '@sentry/core'; |
| 2 | +import * as sentryTracing from '@sentry/tracing'; |
| 3 | + |
| 4 | +import { withSentryAPI } from '../../src/edge'; |
| 5 | + |
| 6 | +// @ts-ignore Request does not exist on type Global |
| 7 | +const origRequest = global.Request; |
| 8 | +// @ts-ignore Response does not exist on type Global |
| 9 | +const origResponse = global.Response; |
| 10 | + |
| 11 | +// @ts-ignore Request does not exist on type Global |
| 12 | +global.Request = class Request { |
| 13 | + headers = { |
| 14 | + get() { |
| 15 | + return null; |
| 16 | + }, |
| 17 | + }; |
| 18 | + |
| 19 | + method = 'POST'; |
| 20 | +}; |
| 21 | + |
| 22 | +// @ts-ignore Response does not exist on type Global |
| 23 | +global.Response = class Request {}; |
| 24 | + |
| 25 | +afterAll(() => { |
| 26 | + // @ts-ignore Request does not exist on type Global |
| 27 | + global.Request = origRequest; |
| 28 | + // @ts-ignore Response does not exist on type Global |
| 29 | + global.Response = origResponse; |
| 30 | +}); |
| 31 | + |
| 32 | +beforeEach(() => { |
| 33 | + jest.resetAllMocks(); |
| 34 | + jest.restoreAllMocks(); |
| 35 | + jest.spyOn(sentryTracing, 'hasTracingEnabled').mockImplementation(() => true); |
| 36 | +}); |
| 37 | + |
| 38 | +describe('withSentryAPI', () => { |
| 39 | + it('should return a function that starts a transaction with the correct name when there is no active transaction and a request is being passed', async () => { |
| 40 | + const startTransactionSpy = jest.spyOn(coreSdk, 'startTransaction'); |
| 41 | + |
| 42 | + const origFunctionReturnValue = new Response(); |
| 43 | + const origFunction = jest.fn(_req => origFunctionReturnValue); |
| 44 | + |
| 45 | + const wrappedFunction = withSentryAPI(origFunction, '/user/[userId]/post/[postId]'); |
| 46 | + |
| 47 | + const request = new Request('https://sentry.io/'); |
| 48 | + await wrappedFunction(request); |
| 49 | + expect(startTransactionSpy).toHaveBeenCalledTimes(1); |
| 50 | + expect(startTransactionSpy).toHaveBeenCalledWith( |
| 51 | + expect.objectContaining({ |
| 52 | + metadata: { source: 'route' }, |
| 53 | + name: 'POST /user/[userId]/post/[postId]', |
| 54 | + op: 'http.server', |
| 55 | + }), |
| 56 | + { request }, |
| 57 | + ); |
| 58 | + }); |
| 59 | + |
| 60 | + it('should return a function that should not start a transaction when there is no active span and no request is being passed', async () => { |
| 61 | + const startTransactionSpy = jest.spyOn(coreSdk, 'startTransaction'); |
| 62 | + |
| 63 | + const origFunctionReturnValue = new Response(); |
| 64 | + const origFunction = jest.fn(() => origFunctionReturnValue); |
| 65 | + |
| 66 | + const wrappedFunction = withSentryAPI(origFunction, '/user/[userId]/post/[postId]'); |
| 67 | + |
| 68 | + await wrappedFunction(); |
| 69 | + expect(startTransactionSpy).not.toHaveBeenCalled(); |
| 70 | + }); |
| 71 | + |
| 72 | + it('should return a function that starts a span on the current transaction with the correct description when there is an active transaction and no request is being passed', async () => { |
| 73 | + const testTransaction = coreSdk.startTransaction({ name: 'testTransaction' }); |
| 74 | + coreSdk.getCurrentHub().getScope()?.setSpan(testTransaction); |
| 75 | + |
| 76 | + const startChildSpy = jest.spyOn(testTransaction, 'startChild'); |
| 77 | + |
| 78 | + const origFunctionReturnValue = new Response(); |
| 79 | + const origFunction = jest.fn(() => origFunctionReturnValue); |
| 80 | + |
| 81 | + const wrappedFunction = withSentryAPI(origFunction, '/user/[userId]/post/[postId]'); |
| 82 | + |
| 83 | + await wrappedFunction(); |
| 84 | + expect(startChildSpy).toHaveBeenCalledTimes(1); |
| 85 | + expect(startChildSpy).toHaveBeenCalledWith( |
| 86 | + expect.objectContaining({ |
| 87 | + description: 'handler (/user/[userId]/post/[postId])', |
| 88 | + op: 'function', |
| 89 | + }), |
| 90 | + ); |
| 91 | + |
| 92 | + testTransaction.finish(); |
| 93 | + coreSdk.getCurrentHub().getScope()?.setSpan(undefined); |
| 94 | + }); |
| 95 | +}); |
0 commit comments