@@ -2,28 +2,30 @@ import * as coreSdk from '@sentry/core';
2
2
3
3
import { wrapApiHandlerWithSentry } from '../../src/edge' ;
4
4
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
-
9
5
// @ts -expect-error Request does not exist on type Global
10
6
const origRequest = global . Request ;
11
7
// @ts -expect-error Response does not exist on type Global
12
8
const origResponse = global . Response ;
13
9
14
10
// @ts -expect-error Request does not exist on type Global
15
11
global . Request = class Request {
16
- headers = {
12
+ public url : string ;
13
+
14
+ public headers = {
17
15
get ( ) {
18
16
return null ;
19
17
} ,
20
18
} ;
21
19
22
- method = 'POST' ;
20
+ public method = 'POST' ;
21
+
22
+ public constructor ( input : string ) {
23
+ this . url = input ;
24
+ }
23
25
} ;
24
26
25
27
// @ts -expect-error Response does not exist on type Global
26
- global . Response = class Request { } ;
28
+ global . Response = class Response { } ;
27
29
28
30
afterAll ( ( ) => {
29
31
// @ts -expect-error Request does not exist on type Global
@@ -32,66 +34,51 @@ afterAll(() => {
32
34
global . Response = origResponse ;
33
35
} ) ;
34
36
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 ( ) ;
39
41
} ) ;
40
42
41
43
describe ( '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 ( ) ) ;
47
47
48
48
const wrappedFunction = wrapApiHandlerWithSentry ( origFunction , '/user/[userId]/post/[postId]' ) ;
49
49
50
- const request = new Request ( 'https://sentry.io/' ) ;
51
50
await wrappedFunction ( request ) ;
52
- expect ( startTransactionSpy ) . toHaveBeenCalledTimes ( 1 ) ;
53
- expect ( startTransactionSpy ) . toHaveBeenCalledWith (
51
+
52
+ expect ( traceSpy ) . toHaveBeenCalledTimes ( 1 ) ;
53
+ expect ( traceSpy ) . toHaveBeenCalledWith (
54
54
expect . objectContaining ( {
55
- metadata : expect . objectContaining ( { source : 'route' } ) ,
55
+ metadata : { request : { headers : { } , method : 'POST' , url : 'https://sentry.io/' } , source : 'route' } ,
56
56
name : 'POST /user/[userId]/post/[postId]' ,
57
57
op : 'http.server' ,
58
+ origin : 'auto.function.nextjs.withEdgeWrapping' ,
58
59
} ) ,
60
+ expect . any ( Function ) ,
61
+ expect . any ( Function ) ,
59
62
) ;
60
63
} ) ;
61
64
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 ( ) ) ;
67
67
68
68
const wrappedFunction = wrapApiHandlerWithSentry ( origFunction , '/user/[userId]/post/[postId]' ) ;
69
69
70
70
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' ) ;
79
71
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 (
88
74
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' ,
91
79
} ) ,
80
+ expect . any ( Function ) ,
81
+ expect . any ( Function ) ,
92
82
) ;
93
-
94
- testTransaction . end ( ) ;
95
- coreSdk . getCurrentHub ( ) . getScope ( ) . setSpan ( undefined ) ;
96
83
} ) ;
97
84
} ) ;
0 commit comments