|  | 
|  | 1 | +import * as SentryCore from '@sentry/core'; | 
|  | 2 | +import * as SentryTracing from '@sentry/tracing'; | 
|  | 3 | +import { IncomingMessage, ServerResponse } from 'http'; | 
|  | 4 | + | 
|  | 5 | +import { | 
|  | 6 | +  withSentryGetServerSideProps, | 
|  | 7 | +  withSentryServerSideGetInitialProps, | 
|  | 8 | +  // TODO: Leaving `withSentryGetStaticProps` out for now until we figure out what to do with it | 
|  | 9 | +  // withSentryGetStaticProps, | 
|  | 10 | +  // TODO: Leaving these out for now until we figure out pages with no data fetchers | 
|  | 11 | +  // withSentryServerSideAppGetInitialProps, | 
|  | 12 | +  // withSentryServerSideDocumentGetInitialProps, | 
|  | 13 | +  // withSentryServerSideErrorGetInitialProps, | 
|  | 14 | +} from '../../src/config/wrappers'; | 
|  | 15 | + | 
|  | 16 | +const startTransactionSpy = jest.spyOn(SentryCore, 'startTransaction'); | 
|  | 17 | +const setMetadataSpy = jest.spyOn(SentryTracing.Transaction.prototype, 'setMetadata'); | 
|  | 18 | + | 
|  | 19 | +describe('data-fetching function wrappers', () => { | 
|  | 20 | +  const route = '/tricks/[trickName]'; | 
|  | 21 | +  let req: IncomingMessage; | 
|  | 22 | +  let res: ServerResponse; | 
|  | 23 | + | 
|  | 24 | +  describe('starts a transaction and puts request in metadata if tracing enabled', () => { | 
|  | 25 | +    beforeEach(() => { | 
|  | 26 | +      req = { headers: {}, url: 'http://dogs.are.great/tricks/kangaroo' } as IncomingMessage; | 
|  | 27 | +      res = {} as ServerResponse; | 
|  | 28 | + | 
|  | 29 | +      jest.spyOn(SentryTracing, 'hasTracingEnabled').mockReturnValueOnce(true); | 
|  | 30 | +    }); | 
|  | 31 | + | 
|  | 32 | +    afterEach(() => { | 
|  | 33 | +      jest.clearAllMocks(); | 
|  | 34 | +    }); | 
|  | 35 | + | 
|  | 36 | +    test('withSentryGetServerSideProps', async () => { | 
|  | 37 | +      const origFunction = jest.fn(async () => ({ props: {} })); | 
|  | 38 | + | 
|  | 39 | +      const wrappedOriginal = withSentryGetServerSideProps(origFunction, route); | 
|  | 40 | +      await wrappedOriginal({ req, res } as any); | 
|  | 41 | + | 
|  | 42 | +      expect(startTransactionSpy).toHaveBeenCalledWith( | 
|  | 43 | +        expect.objectContaining({ | 
|  | 44 | +          name: '/tricks/[trickName]', | 
|  | 45 | +          op: 'nextjs.data.server', | 
|  | 46 | +          metadata: expect.objectContaining({ source: 'route' }), | 
|  | 47 | +        }), | 
|  | 48 | +      ); | 
|  | 49 | + | 
|  | 50 | +      expect(setMetadataSpy).toHaveBeenCalledWith({ request: req }); | 
|  | 51 | +    }); | 
|  | 52 | + | 
|  | 53 | +    test('withSentryServerSideGetInitialProps', async () => { | 
|  | 54 | +      const origFunction = jest.fn(async () => ({})); | 
|  | 55 | + | 
|  | 56 | +      const wrappedOriginal = withSentryServerSideGetInitialProps(origFunction); | 
|  | 57 | +      await wrappedOriginal({ req, res, pathname: route } as any); | 
|  | 58 | + | 
|  | 59 | +      expect(startTransactionSpy).toHaveBeenCalledWith( | 
|  | 60 | +        expect.objectContaining({ | 
|  | 61 | +          name: '/tricks/[trickName]', | 
|  | 62 | +          op: 'nextjs.data.server', | 
|  | 63 | +          metadata: expect.objectContaining({ source: 'route' }), | 
|  | 64 | +        }), | 
|  | 65 | +      ); | 
|  | 66 | + | 
|  | 67 | +      expect(setMetadataSpy).toHaveBeenCalledWith({ request: req }); | 
|  | 68 | +    }); | 
|  | 69 | +  }); | 
|  | 70 | +}); | 
0 commit comments