You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/JestObjectAPI.md
+57Lines changed: 57 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -418,3 +418,60 @@ test('plays video', () => {
418
418
spy.mockRestore();
419
419
});
420
420
```
421
+
422
+
### `jest.spyOn(object, methodName, accessType?)`
423
+
##### available in Jest **x.x.x+**
424
+
425
+
Since Jest x.x.x+, the `jest.spyOn` method takes an optional third argument that can be `'get'` or `'get'` in order to install a spy as a getter or a setter respectively. This is also needed when you need a spy an existing getter/setter method.
426
+
427
+
Example:
428
+
429
+
```js
430
+
constvideo= {
431
+
getplay() { // it's a getter!
432
+
returntrue;
433
+
},
434
+
};
435
+
436
+
module.exports= video;
437
+
438
+
constaudio= {
439
+
_volume:false,
440
+
setvolume(value) { // it's a setter!
441
+
this._volume= value;
442
+
},
443
+
getvolume() {
444
+
returnthis._volume;
445
+
}
446
+
};
447
+
448
+
module.exports= video;
449
+
```
450
+
451
+
Example test:
452
+
453
+
```js
454
+
constvideo=require('./video');
455
+
456
+
test('plays video', () => {
457
+
constspy=jest.spyOn(video, 'play', 'get'); // we pass 'get'
458
+
constisPlaying=video.play;
459
+
460
+
expect(spy).toHaveBeenCalled();
461
+
expect(isPlaying).toBe(true);
462
+
463
+
spy.mockReset();
464
+
spy.mockRestore();
465
+
});
466
+
467
+
test('plays audio', () => {
468
+
constspy=jest.spyOn(video, 'play', 'set'); // we pass 'set'
0 commit comments