-
Notifications
You must be signed in to change notification settings - Fork 188
Expand file tree
/
Copy pathinstrumentMethod.spec.ts
More file actions
418 lines (317 loc) · 13.6 KB
/
Copy pathinstrumentMethod.spec.ts
File metadata and controls
418 lines (317 loc) · 13.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
import { mockClock, mockZoneJs } from '../../test'
import type { Clock, MockZoneJs } from '../../test'
import type { InstrumentedMethodCall } from './instrumentMethod'
import { instrumentMethod, instrumentSetter } from './instrumentMethod'
import { noop } from './utils/functionUtils'
describe('instrumentMethod', () => {
const THIRD_PARTY_RESULT = 42
it('replaces the original method', () => {
const original = () => 1
const object = { method: original }
instrumentMethod(object, 'method', noop)
expect(object.method).not.toBe(original)
})
it('calls the instrumentation before the original method', () => {
const originalSpy = jasmine.createSpy()
const instrumentationSpy = jasmine.createSpy()
const object = { method: originalSpy }
instrumentMethod(object, 'method', instrumentationSpy)
object.method()
expect(instrumentationSpy).toHaveBeenCalledBefore(originalSpy)
})
it('does not set a method originally undefined', () => {
const object: { method?: () => number } = {}
instrumentMethod(object, 'method', noop)
expect(object.method).toBeUndefined()
})
it('sets an event handler even if it was originally undefined', () => {
const object: { onevent?: () => void } = { onevent: undefined }
const instrumentationSpy = jasmine.createSpy()
instrumentMethod(object, 'onevent', instrumentationSpy)
expect(object.onevent).toBeDefined()
object.onevent!()
expect(instrumentationSpy).toHaveBeenCalled()
})
it('do not set an event handler even if the event is not supported (i.e. property does not exist on object)', () => {
const object: { onevent?: () => void } = {}
const instrumentationSpy = jasmine.createSpy()
instrumentMethod(object, 'onevent', instrumentationSpy)
expect('onevent' in object).toBeFalse()
})
it('skips instrumentation on readonly methods', () => {
const originalMethod = () => 1
const object = {} as { method: () => number }
Object.defineProperty(object, 'method', {
value: originalMethod,
writable: false,
configurable: true,
})
const instrumentationSpy = jasmine.createSpy()
const { stop } = instrumentMethod(object, 'method', instrumentationSpy)
expect(object.method).toBe(originalMethod)
expect(object.method()).toBe(1)
expect(instrumentationSpy).not.toHaveBeenCalled()
expect(stop).not.toThrow()
})
it('skips instrumentation on readonly methods defined on the prototype chain', () => {
const originalMethod = jasmine.createSpy().and.returnValue(1)
const prototype = {} as { method: () => number }
Object.defineProperty(prototype, 'method', {
value: originalMethod,
writable: false,
configurable: true,
})
const object = Object.create(prototype) as { method: () => number }
const instrumentationSpy = jasmine.createSpy()
const { stop } = instrumentMethod(object, 'method', instrumentationSpy)
expect(object.method()).toBe(1)
expect(instrumentationSpy).not.toHaveBeenCalled()
expect(Object.prototype.hasOwnProperty.call(object, 'method')).toBeFalse()
expect(stop).not.toThrow()
})
it('calls the instrumentation with method target and parameters', () => {
const object = { method: (a: number, b: number) => a + b }
const instrumentationSpy = jasmine.createSpy<(call: InstrumentedMethodCall<typeof object, 'method'>) => void>()
instrumentMethod(object, 'method', instrumentationSpy)
object.method(2, 3)
expect(instrumentationSpy).toHaveBeenCalledOnceWith({
target: object,
parameters: jasmine.any(Object),
onPostCall: jasmine.any(Function),
handlingStack: undefined,
})
expect(instrumentationSpy.calls.mostRecent().args[0].parameters[0]).toBe(2)
expect(instrumentationSpy.calls.mostRecent().args[0].parameters[1]).toBe(3)
})
it('allows replacing a parameter', () => {
const object = { method: (a: number) => a }
instrumentMethod(object, 'method', ({ parameters }) => {
parameters[0] = 2
})
expect(object.method(1)).toBe(2)
})
it('allows adding a parameter', () => {
const object = { method: (a?: number) => a }
instrumentMethod(object, 'method', ({ parameters }) => {
parameters[0] = 2
})
expect(object.method()).toBe(2)
})
it('calls the "onPostCall" callback with the original method result', () => {
const object = { method: () => 1 }
const onPostCallSpy = jasmine.createSpy()
instrumentMethod(object, 'method', ({ onPostCall }) => onPostCall(onPostCallSpy))
object.method()
expect(onPostCallSpy).toHaveBeenCalledOnceWith(1)
})
it('allows other instrumentations from third parties', () => {
const object = { method: () => 1 }
const instrumentationSpy = jasmine.createSpy()
instrumentMethod(object, 'method', instrumentationSpy)
thirdPartyInstrumentation(object)
expect(object.method()).toBe(THIRD_PARTY_RESULT)
expect(instrumentationSpy).toHaveBeenCalled()
})
it('computes the handling stack', () => {
const object = { method: () => 1 }
const instrumentationSpy = jasmine.createSpy()
instrumentMethod(object, 'method', instrumentationSpy, { computeHandlingStack: true })
function foo() {
object.method()
}
foo()
expect(instrumentationSpy.calls.mostRecent().args[0].handlingStack).toEqual(
jasmine.stringMatching(/^HandlingStack: instrumented method\n {2}at foo @/)
)
})
describe('stop()', () => {
it('does not call the instrumentation anymore', () => {
const object = { method: () => 1 }
const instrumentationSpy = jasmine.createSpy()
const { stop } = instrumentMethod(object, 'method', () => instrumentationSpy)
stop()
object.method()
expect(instrumentationSpy).not.toHaveBeenCalled()
})
describe('when the method has been instrumented by a third party', () => {
it('should not break the third party instrumentation', () => {
const object = { method: () => 1 }
const { stop } = instrumentMethod(object, 'method', noop)
thirdPartyInstrumentation(object)
const instrumentedMethod = object.method
stop()
expect(object.method).toBe(instrumentedMethod)
})
it('does not call the instrumentation', () => {
const object = { method: () => 1 }
const instrumentationSpy = jasmine.createSpy()
const { stop } = instrumentMethod(object, 'method', instrumentationSpy)
thirdPartyInstrumentation(object)
stop()
expect(instrumentationSpy).not.toHaveBeenCalled()
})
it('should not throw errors if original method was undefined', () => {
const object: { onevent?: () => number } = {}
const instrumentationStub = () => 2
const { stop } = instrumentMethod(object, 'onevent', instrumentationStub)
thirdPartyInstrumentation(object)
stop()
expect(object.onevent).not.toThrow()
})
})
})
function thirdPartyInstrumentation(object: { method?: () => number; onevent?: () => void }) {
const originalMethod = object.method
if (typeof originalMethod === 'function') {
object.method = () => {
originalMethod()
return THIRD_PARTY_RESULT
}
}
const originalOnEvent = object.onevent
object.onevent = () => {
if (originalOnEvent) {
originalOnEvent()
}
}
}
})
describe('instrumentSetter', () => {
let clock: Clock
let zoneJs: MockZoneJs
beforeEach(() => {
clock = mockClock()
zoneJs = mockZoneJs()
})
it('replaces the original setter', () => {
const originalSetter = () => {
// do nothing particular, only used to test if this setter gets replaced
}
const object = {} as { foo: number }
Object.defineProperty(object, 'foo', { set: originalSetter, configurable: true })
instrumentSetter(object, 'foo', noop)
// eslint-disable-next-line @typescript-eslint/unbound-method
expect(Object.getOwnPropertyDescriptor(object, 'foo')!.set).not.toBe(originalSetter)
})
it('skips instrumentation if there is no original setter', () => {
const object = { foo: 1 }
instrumentSetter(object, 'foo', noop)
// eslint-disable-next-line @typescript-eslint/unbound-method
expect(Object.getOwnPropertyDescriptor(object, 'foo')!.set).toBeUndefined()
})
it('skips instrumentation if the descriptor is not configurable', () => {
const originalSetter = () => {
// do nothing particular, only used to test if this setter gets replaced
}
const object = {} as { foo: number }
Object.defineProperty(object, 'foo', { set: originalSetter, configurable: false })
instrumentSetter(object, 'foo', noop)
// eslint-disable-next-line @typescript-eslint/unbound-method
expect(Object.getOwnPropertyDescriptor(object, 'foo')!.set).toBe(originalSetter)
})
it('calls the original setter', () => {
const originalSetterSpy = jasmine.createSpy()
const object = {} as { foo: number }
Object.defineProperty(object, 'foo', { set: originalSetterSpy, configurable: true })
instrumentSetter(object, 'foo', noop)
object.foo = 1
expect(originalSetterSpy).toHaveBeenCalledOnceWith(1)
})
it('calls the instrumentation asynchronously', () => {
const instrumentationSetterSpy = jasmine.createSpy()
const object = {} as { foo: number }
Object.defineProperty(object, 'foo', { set: noop, configurable: true })
instrumentSetter(object, 'foo', instrumentationSetterSpy)
object.foo = 1
expect(instrumentationSetterSpy).not.toHaveBeenCalled()
clock.tick(0)
expect(instrumentationSetterSpy).toHaveBeenCalledOnceWith(object, 1)
})
it('does not use the Zone.js setTimeout function', () => {
const zoneJsSetTimeoutSpy = jasmine.createSpy()
zoneJs.replaceProperty(window, 'setTimeout', zoneJsSetTimeoutSpy)
const object = {} as { foo: number }
Object.defineProperty(object, 'foo', { set: noop, configurable: true })
instrumentSetter(object, 'foo', noop)
object.foo = 2
clock.tick(0)
expect(zoneJsSetTimeoutSpy).not.toHaveBeenCalled()
})
it('allows other instrumentations from third parties', () => {
const object = {} as { foo: number }
Object.defineProperty(object, 'foo', { set: noop, configurable: true })
const instrumentationSetterSpy = jasmine.createSpy()
instrumentSetter(object, 'foo', instrumentationSetterSpy)
const thirdPartyInstrumentationSpy = thirdPartyInstrumentation(object)
object.foo = 2
expect(thirdPartyInstrumentationSpy).toHaveBeenCalledOnceWith(2)
clock.tick(0)
expect(instrumentationSetterSpy).toHaveBeenCalledOnceWith(object, 2)
})
describe('stop()', () => {
it('restores the original behavior', () => {
const object = {} as { foo: number }
const originalSetter = () => {
// do nothing particular, only used to test if this setter gets replaced
}
Object.defineProperty(object, 'foo', { set: originalSetter, configurable: true })
const { stop } = instrumentSetter(object, 'foo', noop)
stop()
// eslint-disable-next-line @typescript-eslint/unbound-method
expect(Object.getOwnPropertyDescriptor(object, 'foo')!.set).toBe(originalSetter)
})
it('does not call the instrumentation anymore', () => {
const object = {} as { foo: number }
Object.defineProperty(object, 'foo', { set: noop, configurable: true })
const instrumentationSetterSpy = jasmine.createSpy()
const { stop } = instrumentSetter(object, 'foo', instrumentationSetterSpy)
stop()
object.foo = 2
clock.tick(0)
expect(instrumentationSetterSpy).not.toHaveBeenCalled()
})
it('does not call instrumentation pending in the event loop via setTimeout', () => {
const object = {} as { foo: number }
Object.defineProperty(object, 'foo', { set: noop, configurable: true })
const instrumentationSetterSpy = jasmine.createSpy()
const { stop } = instrumentSetter(object, 'foo', instrumentationSetterSpy)
object.foo = 2
stop()
clock.tick(0)
expect(instrumentationSetterSpy).not.toHaveBeenCalled()
})
describe('when the method has been instrumented by a third party', () => {
it('should not break the third party instrumentation', () => {
const object = {} as { foo: number }
Object.defineProperty(object, 'foo', { set: noop, configurable: true })
const { stop } = instrumentSetter(object, 'foo', noop)
const thirdPartyInstrumentationSpy = thirdPartyInstrumentation(object)
stop()
// eslint-disable-next-line @typescript-eslint/unbound-method
expect(Object.getOwnPropertyDescriptor(object, 'foo')!.set).toBe(thirdPartyInstrumentationSpy)
})
it('does not call the instrumentation', () => {
const object = {} as { foo: number }
Object.defineProperty(object, 'foo', { set: noop, configurable: true })
const instrumentationSetterSpy = jasmine.createSpy()
const { stop } = instrumentSetter(object, 'foo', instrumentationSetterSpy)
thirdPartyInstrumentation(object)
stop()
object.foo = 2
clock.tick(0)
expect(instrumentationSetterSpy).not.toHaveBeenCalled()
})
})
})
function thirdPartyInstrumentation(object: { foo: number }) {
// eslint-disable-next-line @typescript-eslint/unbound-method
const originalSetter = Object.getOwnPropertyDescriptor(object, 'foo')!.set
const thirdPartyInstrumentationSpy = jasmine.createSpy().and.callFake(function (this: any, value) {
if (originalSetter) {
originalSetter.call(this, value)
}
})
Object.defineProperty(object, 'foo', { set: thirdPartyInstrumentationSpy })
return thirdPartyInstrumentationSpy
}
})