@@ -10,7 +10,7 @@ describe('instrumentDurableObjectWithSentry', () => {
10
10
} ) ;
11
11
12
12
it ( 'Generic functionality' , ( ) => {
13
- const options = vi . fn ( ) ;
13
+ const options = vi . fn ( ) . mockReturnValue ( { } ) ;
14
14
const instrumented = instrumentDurableObjectWithSentry ( options , vi . fn ( ) ) ;
15
15
expect ( instrumented ) . toBeTypeOf ( 'function' ) ;
16
16
expect ( ( ) => Reflect . construct ( instrumented , [ ] ) ) . not . toThrow ( ) ;
@@ -23,7 +23,7 @@ describe('instrumentDurableObjectWithSentry', () => {
23
23
return 'sync-result' ;
24
24
}
25
25
} ;
26
- const obj = Reflect . construct ( instrumentDurableObjectWithSentry ( vi . fn ( ) , testClass as any ) , [ ] ) as any ;
26
+ const obj = Reflect . construct ( instrumentDurableObjectWithSentry ( vi . fn ( ) . mockReturnValue ( { } ) , testClass as any ) , [ ] ) as any ;
27
27
expect ( obj . method ) . toBe ( obj . method ) ;
28
28
29
29
const result = obj . method ( ) ;
@@ -37,7 +37,7 @@ describe('instrumentDurableObjectWithSentry', () => {
37
37
return 'async-result' ;
38
38
}
39
39
} ;
40
- const obj = Reflect . construct ( instrumentDurableObjectWithSentry ( vi . fn ( ) , testClass as any ) , [ ] ) as any ;
40
+ const obj = Reflect . construct ( instrumentDurableObjectWithSentry ( vi . fn ( ) . mockReturnValue ( { } ) , testClass as any ) , [ ] ) as any ;
41
41
expect ( obj . asyncMethod ) . toBe ( obj . asyncMethod ) ;
42
42
43
43
const result = obj . asyncMethod ( ) ;
@@ -56,9 +56,11 @@ describe('instrumentDurableObjectWithSentry', () => {
56
56
. fn ( )
57
57
. mockReturnValueOnce ( {
58
58
orgId : 1 ,
59
+ instrumentPrototypeMethods : true ,
59
60
} )
60
61
. mockReturnValueOnce ( {
61
62
orgId : 2 ,
63
+ instrumentPrototypeMethods : true ,
62
64
} ) ;
63
65
const testClass = class {
64
66
method ( ) { }
@@ -79,7 +81,7 @@ describe('instrumentDurableObjectWithSentry', () => {
79
81
expect ( initCore ) . nthCalledWith ( 2 , expect . any ( Function ) , expect . objectContaining ( { orgId : 2 } ) ) ;
80
82
} ) ;
81
83
82
- it ( 'All available durable object methods are instrumented' , ( ) => {
84
+ it ( 'All available durable object methods are instrumented when instrumentPrototypeMethods is enabled ' , ( ) => {
83
85
const testClass = class {
84
86
propertyFunction = vi . fn ( ) ;
85
87
@@ -95,7 +97,7 @@ describe('instrumentDurableObjectWithSentry', () => {
95
97
96
98
webSocketError ( ) { }
97
99
} ;
98
- const instrumented = instrumentDurableObjectWithSentry ( vi . fn ( ) , testClass as any ) ;
100
+ const instrumented = instrumentDurableObjectWithSentry ( vi . fn ( ) . mockReturnValue ( { instrumentPrototypeMethods : true } ) , testClass as any ) ;
99
101
const obj = Reflect . construct ( instrumented , [ ] ) ;
100
102
for ( const method_name of [
101
103
'propertyFunction' ,
@@ -135,4 +137,93 @@ describe('instrumentDurableObjectWithSentry', () => {
135
137
await Promise . all ( waitUntil . mock . calls . map ( ( [ p ] ) => p ) ) ;
136
138
expect ( flush ) . toBeCalled ( ) ;
137
139
} ) ;
140
+
141
+ describe ( 'instrumentPrototypeMethods option' , ( ) => {
142
+ it ( 'does not instrument prototype methods when option is not set' , ( ) => {
143
+ const testClass = class {
144
+ prototypeMethod ( ) {
145
+ return 'prototype-result' ;
146
+ }
147
+ } ;
148
+ const options = vi . fn ( ) . mockReturnValue ( { } ) ;
149
+ const instrumented = instrumentDurableObjectWithSentry ( options , testClass as any ) ;
150
+ const obj = Reflect . construct ( instrumented , [ ] ) as any ;
151
+
152
+ expect ( isInstrumented ( obj . prototypeMethod ) ) . toBeFalsy ( ) ;
153
+ } ) ;
154
+
155
+ it ( 'does not instrument prototype methods when option is false' , ( ) => {
156
+ const testClass = class {
157
+ prototypeMethod ( ) {
158
+ return 'prototype-result' ;
159
+ }
160
+ } ;
161
+ const options = vi . fn ( ) . mockReturnValue ( { instrumentPrototypeMethods : false } ) ;
162
+ const instrumented = instrumentDurableObjectWithSentry ( options , testClass as any ) ;
163
+ const obj = Reflect . construct ( instrumented , [ ] ) as any ;
164
+
165
+ expect ( isInstrumented ( obj . prototypeMethod ) ) . toBeFalsy ( ) ;
166
+ } ) ;
167
+
168
+ it ( 'instruments all prototype methods when option is true' , ( ) => {
169
+ const testClass = class {
170
+ methodOne ( ) {
171
+ return 'one' ;
172
+ }
173
+ methodTwo ( ) {
174
+ return 'two' ;
175
+ }
176
+ } ;
177
+ const options = vi . fn ( ) . mockReturnValue ( { instrumentPrototypeMethods : true } ) ;
178
+ const instrumented = instrumentDurableObjectWithSentry ( options , testClass as any ) ;
179
+ const obj = Reflect . construct ( instrumented , [ ] ) as any ;
180
+
181
+ expect ( isInstrumented ( obj . methodOne ) ) . toBeTruthy ( ) ;
182
+ expect ( isInstrumented ( obj . methodTwo ) ) . toBeTruthy ( ) ;
183
+ } ) ;
184
+
185
+ it ( 'instruments only specified methods when option is array' , ( ) => {
186
+ const testClass = class {
187
+ methodOne ( ) {
188
+ return 'one' ;
189
+ }
190
+ methodTwo ( ) {
191
+ return 'two' ;
192
+ }
193
+ methodThree ( ) {
194
+ return 'three' ;
195
+ }
196
+ } ;
197
+ const options = vi . fn ( ) . mockReturnValue ( { instrumentPrototypeMethods : [ 'methodOne' , 'methodThree' ] } ) ;
198
+ const instrumented = instrumentDurableObjectWithSentry ( options , testClass as any ) ;
199
+ const obj = Reflect . construct ( instrumented , [ ] ) as any ;
200
+
201
+ expect ( isInstrumented ( obj . methodOne ) ) . toBeTruthy ( ) ;
202
+ expect ( isInstrumented ( obj . methodTwo ) ) . toBeFalsy ( ) ;
203
+ expect ( isInstrumented ( obj . methodThree ) ) . toBeTruthy ( ) ;
204
+ } ) ;
205
+
206
+ it ( 'still instruments instance methods regardless of prototype option' , ( ) => {
207
+ const testClass = class {
208
+ propertyFunction = vi . fn ( ) ;
209
+
210
+ fetch ( ) { }
211
+ alarm ( ) { }
212
+ webSocketMessage ( ) { }
213
+ webSocketClose ( ) { }
214
+ webSocketError ( ) { }
215
+ } ;
216
+ const options = vi . fn ( ) . mockReturnValue ( { instrumentPrototypeMethods : false } ) ;
217
+ const instrumented = instrumentDurableObjectWithSentry ( options , testClass as any ) ;
218
+ const obj = Reflect . construct ( instrumented , [ ] ) as any ;
219
+
220
+ // Instance methods should still be instrumented
221
+ expect ( isInstrumented ( obj . propertyFunction ) ) . toBeTruthy ( ) ;
222
+ expect ( isInstrumented ( obj . fetch ) ) . toBeTruthy ( ) ;
223
+ expect ( isInstrumented ( obj . alarm ) ) . toBeTruthy ( ) ;
224
+ expect ( isInstrumented ( obj . webSocketMessage ) ) . toBeTruthy ( ) ;
225
+ expect ( isInstrumented ( obj . webSocketClose ) ) . toBeTruthy ( ) ;
226
+ expect ( isInstrumented ( obj . webSocketError ) ) . toBeTruthy ( ) ;
227
+ } ) ;
228
+ } ) ;
138
229
} ) ;
0 commit comments