@@ -2,28 +2,30 @@ import * as coreSdk from '@sentry/core';
22
33import { wrapApiHandlerWithSentry } from '../../src/edge' ;
44
5- // The wrap* functions require the hub to have tracing extensions. This is normally called by the EdgeClient
6- // constructor but the client isn't used in these tests.
7- coreSdk . addTracingExtensions ( ) ;
8-
95// @ts -expect-error Request does not exist on type Global
106const origRequest = global . Request ;
117// @ts -expect-error Response does not exist on type Global
128const origResponse = global . Response ;
139
1410// @ts -expect-error Request does not exist on type Global
1511global . Request = class Request {
16- headers = {
12+ public url : string ;
13+
14+ public headers = {
1715 get ( ) {
1816 return null ;
1917 } ,
2018 } ;
2119
22- method = 'POST' ;
20+ public method = 'POST' ;
21+
22+ public constructor ( input : string ) {
23+ this . url = input ;
24+ }
2325} ;
2426
2527// @ts -expect-error Response does not exist on type Global
26- global . Response = class Request { } ;
28+ global . Response = class Response { } ;
2729
2830afterAll ( ( ) => {
2931 // @ts -expect-error Request does not exist on type Global
@@ -32,66 +34,51 @@ afterAll(() => {
3234 global . Response = origResponse ;
3335} ) ;
3436
35- beforeEach ( ( ) => {
36- jest . resetAllMocks ( ) ;
37- jest . restoreAllMocks ( ) ;
38- jest . spyOn ( coreSdk , 'hasTracingEnabled' ) . mockImplementation ( ( ) => true ) ;
37+ const traceSpy = jest . spyOn ( coreSdk , 'trace' ) ;
38+
39+ afterEach ( ( ) => {
40+ jest . clearAllMocks ( ) ;
3941} ) ;
4042
4143describe ( 'wrapApiHandlerWithSentry' , ( ) => {
42- 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 ( ) => {
43- const startTransactionSpy = jest . spyOn ( coreSdk , 'startTransaction' ) ;
44-
45- const origFunctionReturnValue = new Response ( ) ;
46- const origFunction = jest . fn ( _req => origFunctionReturnValue ) ;
44+ it ( 'should return a function that calls trace' , async ( ) => {
45+ const request = new Request ( 'https://sentry.io/' ) ;
46+ const origFunction = jest . fn ( _req => new Response ( ) ) ;
4747
4848 const wrappedFunction = wrapApiHandlerWithSentry ( origFunction , '/user/[userId]/post/[postId]' ) ;
4949
50- const request = new Request ( 'https://sentry.io/' ) ;
5150 await wrappedFunction ( request ) ;
52- expect ( startTransactionSpy ) . toHaveBeenCalledTimes ( 1 ) ;
53- expect ( startTransactionSpy ) . toHaveBeenCalledWith (
51+
52+ expect ( traceSpy ) . toHaveBeenCalledTimes ( 1 ) ;
53+ expect ( traceSpy ) . toHaveBeenCalledWith (
5454 expect . objectContaining ( {
55- metadata : expect . objectContaining ( { source : 'route' } ) ,
55+ metadata : { request : { headers : { } , method : 'POST' , url : 'https://sentry.io/' } , source : 'route' } ,
5656 name : 'POST /user/[userId]/post/[postId]' ,
5757 op : 'http.server' ,
58+ origin : 'auto.function.nextjs.withEdgeWrapping' ,
5859 } ) ,
60+ expect . any ( Function ) ,
61+ expect . any ( Function ) ,
5962 ) ;
6063 } ) ;
6164
62- it ( 'should return a function that should not start a transaction when there is no active span and no request is being passed' , async ( ) => {
63- const startTransactionSpy = jest . spyOn ( coreSdk , 'startTransaction' ) ;
64-
65- const origFunctionReturnValue = new Response ( ) ;
66- const origFunction = jest . fn ( ( ) => origFunctionReturnValue ) ;
65+ it ( 'should return a function that calls trace without throwing when no request is passed' , async ( ) => {
66+ const origFunction = jest . fn ( ( ) => new Response ( ) ) ;
6767
6868 const wrappedFunction = wrapApiHandlerWithSentry ( origFunction , '/user/[userId]/post/[postId]' ) ;
6969
7070 await wrappedFunction ( ) ;
71- expect ( startTransactionSpy ) . not . toHaveBeenCalled ( ) ;
72- } ) ;
73-
74- 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 ( ) => {
75- const testTransaction = coreSdk . startTransaction ( { name : 'testTransaction' } ) ;
76- coreSdk . getCurrentHub ( ) . getScope ( ) . setSpan ( testTransaction ) ;
77-
78- const startChildSpy = jest . spyOn ( testTransaction , 'startChild' ) ;
7971
80- const origFunctionReturnValue = new Response ( ) ;
81- const origFunction = jest . fn ( ( ) => origFunctionReturnValue ) ;
82-
83- const wrappedFunction = wrapApiHandlerWithSentry ( origFunction , '/user/[userId]/post/[postId]' ) ;
84-
85- await wrappedFunction ( ) ;
86- expect ( startChildSpy ) . toHaveBeenCalledTimes ( 1 ) ;
87- expect ( startChildSpy ) . toHaveBeenCalledWith (
72+ expect ( traceSpy ) . toHaveBeenCalledTimes ( 1 ) ;
73+ expect ( traceSpy ) . toHaveBeenCalledWith (
8874 expect . objectContaining ( {
89- description : 'handler (/user/[userId]/post/[postId])' ,
90- op : 'function' ,
75+ metadata : { source : 'route' } ,
76+ name : 'handler (/user/[userId]/post/[postId])' ,
77+ op : 'http.server' ,
78+ origin : 'auto.function.nextjs.withEdgeWrapping' ,
9179 } ) ,
80+ expect . any ( Function ) ,
81+ expect . any ( Function ) ,
9282 ) ;
93-
94- testTransaction . end ( ) ;
95- coreSdk . getCurrentHub ( ) . getScope ( ) . setSpan ( undefined ) ;
9683 } ) ;
9784} ) ;
0 commit comments