|
| 1 | +import * as coreSdk from '@sentry/core'; |
| 2 | +import * as sentryTracing from '@sentry/tracing'; |
| 3 | + |
| 4 | +import { withEdgeWrapping } from '../../src/edge/utils/edgeWrapperUtils'; |
| 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 | + |
| 20 | +// @ts-ignore Response does not exist on type Global |
| 21 | +global.Response = class Request {}; |
| 22 | + |
| 23 | +afterAll(() => { |
| 24 | + // @ts-ignore Request does not exist on type Global |
| 25 | + global.Request = origRequest; |
| 26 | + // @ts-ignore Response does not exist on type Global |
| 27 | + global.Response = origResponse; |
| 28 | +}); |
| 29 | + |
| 30 | +beforeEach(() => { |
| 31 | + jest.clearAllMocks(); |
| 32 | + jest.resetAllMocks(); |
| 33 | + jest.spyOn(sentryTracing, 'hasTracingEnabled').mockImplementation(() => true); |
| 34 | +}); |
| 35 | + |
| 36 | +describe('withEdgeWrapping', () => { |
| 37 | + it('should return a function that calls the passed function', async () => { |
| 38 | + const origFunctionReturnValue = new Response(); |
| 39 | + const origFunction = jest.fn(_req => origFunctionReturnValue); |
| 40 | + |
| 41 | + const wrappedFunction = withEdgeWrapping(origFunction, { |
| 42 | + spanDescription: 'some label', |
| 43 | + mechanismFunctionName: 'some name', |
| 44 | + spanOp: 'some op', |
| 45 | + }); |
| 46 | + |
| 47 | + const returnValue = await wrappedFunction(new Request('https://sentry.io/')); |
| 48 | + |
| 49 | + expect(returnValue).toBe(origFunctionReturnValue); |
| 50 | + expect(origFunction).toHaveBeenCalledTimes(1); |
| 51 | + }); |
| 52 | + |
| 53 | + it('should return a function that calls captureException on error', async () => { |
| 54 | + const captureExceptionSpy = jest.spyOn(coreSdk, 'captureException'); |
| 55 | + const error = new Error(); |
| 56 | + const origFunction = jest.fn(_req => { |
| 57 | + throw error; |
| 58 | + }); |
| 59 | + |
| 60 | + const wrappedFunction = withEdgeWrapping(origFunction, { |
| 61 | + spanDescription: 'some label', |
| 62 | + mechanismFunctionName: 'some name', |
| 63 | + spanOp: 'some op', |
| 64 | + }); |
| 65 | + |
| 66 | + await expect(wrappedFunction(new Request('https://sentry.io/'))).rejects.toBe(error); |
| 67 | + expect(captureExceptionSpy).toHaveBeenCalledTimes(1); |
| 68 | + }); |
| 69 | + |
| 70 | + it('should return a function that starts a transaction when a request object is passed', async () => { |
| 71 | + const startTransactionSpy = jest.spyOn(coreSdk, 'startTransaction'); |
| 72 | + |
| 73 | + const origFunctionReturnValue = new Response(); |
| 74 | + const origFunction = jest.fn(_req => origFunctionReturnValue); |
| 75 | + |
| 76 | + const wrappedFunction = withEdgeWrapping(origFunction, { |
| 77 | + spanDescription: 'some label', |
| 78 | + mechanismFunctionName: 'some name', |
| 79 | + spanOp: 'some op', |
| 80 | + }); |
| 81 | + |
| 82 | + const request = new Request('https://sentry.io/'); |
| 83 | + await wrappedFunction(request); |
| 84 | + expect(startTransactionSpy).toHaveBeenCalledTimes(1); |
| 85 | + expect(startTransactionSpy).toHaveBeenCalledWith( |
| 86 | + expect.objectContaining({ metadata: { source: 'route' }, name: 'some label', op: 'some op' }), |
| 87 | + { request }, |
| 88 | + ); |
| 89 | + }); |
| 90 | + |
| 91 | + it("should return a function that doesn't crash when req isn't passed", async () => { |
| 92 | + const origFunctionReturnValue = new Response(); |
| 93 | + const origFunction = jest.fn(() => origFunctionReturnValue); |
| 94 | + |
| 95 | + const wrappedFunction = withEdgeWrapping(origFunction, { |
| 96 | + spanDescription: 'some label', |
| 97 | + mechanismFunctionName: 'some name', |
| 98 | + spanOp: 'some op', |
| 99 | + }); |
| 100 | + |
| 101 | + await expect(wrappedFunction()).resolves.toBe(origFunctionReturnValue); |
| 102 | + expect(origFunction).toHaveBeenCalledTimes(1); |
| 103 | + }); |
| 104 | +}); |
0 commit comments