forked from jestjs/jest
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjest_mock.test.js
More file actions
787 lines (650 loc) · 22.3 KB
/
jest_mock.test.js
File metadata and controls
787 lines (650 loc) · 22.3 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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
/**
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
*/
'use strict';
const vm = require('vm');
describe('moduleMocker', () => {
let moduleMocker;
beforeEach(() => {
const mock = require('../');
const global = vm.runInNewContext('this');
moduleMocker = new mock.ModuleMocker(global);
});
describe('getMetadata', () => {
it('returns the function `name` property', () => {
function x() {}
const metadata = moduleMocker.getMetadata(x);
expect(x.name).toBe('x');
expect(metadata.name).toBe('x');
});
it('mocks constant values', () => {
const metadata = moduleMocker.getMetadata(Symbol.for('bowties.are.cool'));
expect(metadata.value).toEqual(Symbol.for('bowties.are.cool'));
expect(moduleMocker.getMetadata('banana').value).toEqual('banana');
expect(moduleMocker.getMetadata(27).value).toEqual(27);
expect(moduleMocker.getMetadata(false).value).toEqual(false);
});
it('does not retrieve metadata for arrays', () => {
const array = [1, 2, 3];
const metadata = moduleMocker.getMetadata(array);
expect(metadata.value).toBeUndefined();
expect(metadata.members).toBeUndefined();
expect(metadata.type).toEqual('array');
});
it('does not retrieve metadata for undefined', () => {
const metadata = moduleMocker.getMetadata(undefined);
expect(metadata.value).toBeUndefined();
expect(metadata.members).toBeUndefined();
expect(metadata.type).toEqual('undefined');
});
it('does not retrieve metadata for null', () => {
const metadata = moduleMocker.getMetadata(null);
expect(metadata.value).toBeNull();
expect(metadata.members).toBeUndefined();
expect(metadata.type).toEqual('null');
});
});
describe('generateFromMetadata', () => {
it('forwards the function name property', () => {
function foo() {}
const mock = moduleMocker.generateFromMetadata(
moduleMocker.getMetadata(foo),
);
expect(mock.name).toBe('foo');
});
it('escapes illegal characters in function name property', () => {
function getMockFnWithOriginalName(name) {
const fn = () => {};
Object.defineProperty(fn, 'name', {value: name});
return moduleMocker.generateFromMetadata(moduleMocker.getMetadata(fn));
}
expect(
getMockFnWithOriginalName('foo-bar').name === 'foo$bar',
).toBeTruthy();
expect(
getMockFnWithOriginalName('foo-bar-2').name === 'foo$bar$2',
).toBeTruthy();
expect(
getMockFnWithOriginalName('foo-bar-3').name === 'foo$bar$3',
).toBeTruthy();
expect(
getMockFnWithOriginalName('foo/bar').name === 'foo$bar',
).toBeTruthy();
expect(
getMockFnWithOriginalName('foo𠮷bar').name === 'foo𠮷bar',
).toBeTruthy();
});
it('special cases the mockConstructor name', () => {
function mockConstructor() {}
const mock = moduleMocker.generateFromMetadata(
moduleMocker.getMetadata(mockConstructor),
);
// Depends on node version
expect(!mock.name || mock.name === 'mockConstructor').toBeTruthy();
});
it('wont interfere with previous mocks on a shared prototype', () => {
const ClassFoo = function() {};
ClassFoo.prototype.x = () => {};
const ClassFooMock = moduleMocker.generateFromMetadata(
moduleMocker.getMetadata(ClassFoo),
);
const foo = new ClassFooMock();
const bar = new ClassFooMock();
foo.x.mockImplementation(() => {
return 'Foo';
});
bar.x.mockImplementation(() => {
return 'Bar';
});
expect(foo.x()).toBe('Foo');
expect(bar.x()).toBe('Bar');
});
it('does not mock non-enumerable getters', () => {
const foo = Object.defineProperties(
{},
{
nonEnumGetter: {
get: () => {
throw new Error();
},
},
nonEnumMethod: {
value: () => {},
},
},
);
const mock = moduleMocker.generateFromMetadata(
moduleMocker.getMetadata(foo),
);
expect(typeof foo.nonEnumMethod).toBe('function');
expect(mock.nonEnumMethod.mock).not.toBeUndefined();
expect(mock.nonEnumGetter).toBeUndefined();
});
it('mocks getters of ES modules', () => {
const foo = Object.defineProperties(
{},
{
__esModule: {
value: true,
},
enumGetter: {
enumerable: true,
get: () => 10,
},
},
);
const mock = moduleMocker.generateFromMetadata(
moduleMocker.getMetadata(foo),
);
expect(mock.enumGetter).toBeDefined();
});
it('mocks ES2015 non-enumerable methods', () => {
class ClassFoo {
foo() {}
toString() {
return 'Foo';
}
}
const ClassFooMock = moduleMocker.generateFromMetadata(
moduleMocker.getMetadata(ClassFoo),
);
const foo = new ClassFooMock();
const instanceFoo = new ClassFoo();
const instanceFooMock = moduleMocker.generateFromMetadata(
moduleMocker.getMetadata(instanceFoo),
);
expect(typeof foo.foo).toBe('function');
expect(typeof instanceFooMock.foo).toBe('function');
expect(instanceFooMock.foo.mock).not.toBeUndefined();
expect(instanceFooMock.toString.mock).not.toBeUndefined();
});
it('mocks methods that are bound multiple times', () => {
const func = function func() {};
const multipleBoundFunc = func.bind(null).bind(null);
const multipleBoundFuncMock = moduleMocker.generateFromMetadata(
moduleMocker.getMetadata(multipleBoundFunc),
);
expect(typeof multipleBoundFuncMock).toBe('function');
});
it('mocks methods that are bound after mocking', () => {
const fooMock = moduleMocker.generateFromMetadata(
moduleMocker.getMetadata(() => {}),
);
const barMock = moduleMocker.generateFromMetadata(
moduleMocker.getMetadata(fooMock.bind(null)),
);
expect(barMock).not.toThrow();
});
it('mocks regexp instances', () => {
expect(() =>
moduleMocker.generateFromMetadata(moduleMocker.getMetadata(/a/)),
).not.toThrow();
});
describe('mocked functions', () => {
it('tracks calls to mocks', () => {
const fn = moduleMocker.fn();
expect(fn.mock.calls).toEqual([]);
fn(1, 2, 3);
expect(fn.mock.calls).toEqual([[1, 2, 3]]);
fn('a', 'b', 'c');
expect(fn.mock.calls).toEqual([[1, 2, 3], ['a', 'b', 'c']]);
});
it('tracks instances made by mocks', () => {
const fn = moduleMocker.fn();
expect(fn.mock.instances).toEqual([]);
const instance1 = new fn();
expect(fn.mock.instances[0]).toBe(instance1);
const instance2 = new fn();
expect(fn.mock.instances[1]).toBe(instance2);
});
it('supports clearing mock calls', () => {
const fn = moduleMocker.fn();
expect(fn.mock.calls).toEqual([]);
fn(1, 2, 3);
expect(fn.mock.calls).toEqual([[1, 2, 3]]);
fn.mockReturnValue('abcd');
fn.mockClear();
expect(fn.mock.calls).toEqual([]);
fn('a', 'b', 'c');
expect(fn.mock.calls).toEqual([['a', 'b', 'c']]);
expect(fn()).toEqual('abcd');
});
it('supports clearing mocks', () => {
const fn = moduleMocker.fn();
expect(fn.mock.calls).toEqual([]);
fn(1, 2, 3);
expect(fn.mock.calls).toEqual([[1, 2, 3]]);
fn.mockClear();
expect(fn.mock.calls).toEqual([]);
fn('a', 'b', 'c');
expect(fn.mock.calls).toEqual([['a', 'b', 'c']]);
});
it('supports clearing all mocks', () => {
const fn1 = moduleMocker.fn();
fn1.mockImplementation(() => 'abcd');
fn1(1, 2, 3);
expect(fn1.mock.calls).toEqual([[1, 2, 3]]);
const fn2 = moduleMocker.fn();
fn2.mockReturnValue('abcde');
fn2('a', 'b', 'c', 'd');
expect(fn2.mock.calls).toEqual([['a', 'b', 'c', 'd']]);
moduleMocker.clearAllMocks();
expect(fn1.mock.calls).toEqual([]);
expect(fn2.mock.calls).toEqual([]);
expect(fn1()).toEqual('abcd');
expect(fn2()).toEqual('abcde');
});
it('supports resetting mock return values', () => {
const fn = moduleMocker.fn();
fn.mockReturnValue('abcd');
const before = fn();
expect(before).toEqual('abcd');
fn.mockReset();
const after = fn();
expect(after).not.toEqual('abcd');
});
it('supports resetting single use mock return values', () => {
const fn = moduleMocker.fn();
fn.mockReturnValueOnce('abcd');
fn.mockReset();
const after = fn();
expect(after).not.toEqual('abcd');
});
it('supports resetting mock implementations', () => {
const fn = moduleMocker.fn();
fn.mockImplementation(() => 'abcd');
const before = fn();
expect(before).toEqual('abcd');
fn.mockReset();
const after = fn();
expect(after).not.toEqual('abcd');
});
it('supports resetting single use mock implementations', () => {
const fn = moduleMocker.fn();
fn.mockImplementationOnce(() => 'abcd');
fn.mockReset();
const after = fn();
expect(after).not.toEqual('abcd');
});
it('supports resetting all mocks', () => {
const fn1 = moduleMocker.fn();
fn1.mockImplementation(() => 'abcd');
fn1(1, 2, 3);
expect(fn1.mock.calls).toEqual([[1, 2, 3]]);
const fn2 = moduleMocker.fn();
fn2.mockReturnValue('abcd');
fn2('a', 'b', 'c');
expect(fn2.mock.calls).toEqual([['a', 'b', 'c']]);
moduleMocker.resetAllMocks();
expect(fn1.mock.calls).toEqual([]);
expect(fn2.mock.calls).toEqual([]);
expect(fn1()).not.toEqual('abcd');
expect(fn2()).not.toEqual('abcd');
});
it('maintains function arity', () => {
const mockFunctionArity1 = moduleMocker.fn(x => x);
const mockFunctionArity2 = moduleMocker.fn((x, y) => y);
expect(mockFunctionArity1.length).toBe(1);
expect(mockFunctionArity2.length).toBe(2);
});
});
it('supports mock value returning undefined', () => {
const obj = {
func: () => 'some text',
};
moduleMocker.spyOn(obj, 'func').mockReturnValue(undefined);
expect(obj.func()).not.toEqual('some text');
});
it('supports mock value once returning undefined', () => {
const obj = {
func: () => 'some text',
};
moduleMocker.spyOn(obj, 'func').mockReturnValueOnce(undefined);
expect(obj.func()).not.toEqual('some text');
});
it('mockReturnValueOnce mocks value just once', () => {
const fake = jest.fn(a => a + 2);
fake.mockReturnValueOnce(42);
expect(fake(2)).toEqual(42);
expect(fake(2)).toEqual(4);
});
describe('timestamps', () => {
const RealDate = Date;
beforeEach(() => {
global.Date = {
now: jest
.fn()
.mockImplementationOnce(() => 978391040765)
.mockImplementationOnce(() => 1262388620765),
};
});
afterEach(() => {
global.Date = RealDate;
});
it('tracks timestamps made by mocks', () => {
const fn = moduleMocker.fn();
expect(fn.mock.timestamps).toEqual([]);
fn(1, 2, 3);
expect(fn.mock.timestamps[0]).toBe(978391040765);
fn('a', 'b', 'c');
expect(fn.mock.timestamps[1]).toBe(1262388620765);
});
it('supports clearing mock timestamps', () => {
const fn = moduleMocker.fn();
expect(fn.mock.timestamps).toEqual([]);
fn(1, 2, 3);
expect(fn.mock.timestamps).toEqual([978391040765]);
fn.mockReturnValue('abcd');
fn.mockClear();
expect(fn.mock.timestamps).toEqual([]);
fn('a', 'b', 'c');
expect(fn.mock.timestamps).toEqual([1262388620765]);
expect(fn()).toEqual('abcd');
});
it('supports clearing all mocks timestamps', () => {
const fn1 = moduleMocker.fn();
fn1.mockImplementation(() => 'abcd');
fn1(1, 2, 3);
expect(fn1.mock.timestamps).toEqual([978391040765]);
const fn2 = moduleMocker.fn();
fn2.mockReturnValue('abcde');
fn2('a', 'b', 'c', 'd');
expect(fn2.mock.timestamps).toEqual([1262388620765]);
moduleMocker.clearAllMocks();
expect(fn1.mock.timestamps).toEqual([]);
expect(fn2.mock.timestamps).toEqual([]);
expect(fn1()).toEqual('abcd');
expect(fn2()).toEqual('abcde');
});
});
});
describe('getMockImplementation', () => {
it('should mock calls to a mock function', () => {
const mockFn = moduleMocker.fn();
mockFn.mockImplementation(() => {
return 'Foo';
});
expect(typeof mockFn.getMockImplementation()).toBe('function');
expect(mockFn.getMockImplementation()()).toBe('Foo');
});
});
describe('mockImplementationOnce', () => {
it('should mock constructor', () => {
const mock1 = jest.fn();
const mock2 = jest.fn();
const Module = jest.fn(() => ({someFn: mock1}));
const testFn = function() {
const m = new Module();
m.someFn();
};
Module.mockImplementationOnce(() => ({someFn: mock2}));
testFn();
expect(mock2).toHaveBeenCalled();
expect(mock1).not.toHaveBeenCalled();
testFn();
expect(mock1).toHaveBeenCalled();
});
it('should mock single call to a mock function', () => {
const mockFn = moduleMocker.fn();
mockFn
.mockImplementationOnce(() => {
return 'Foo';
})
.mockImplementationOnce(() => {
return 'Bar';
});
expect(mockFn()).toBe('Foo');
expect(mockFn()).toBe('Bar');
expect(mockFn()).toBeUndefined();
});
it('should fallback to default mock function when no specific mock is available', () => {
const mockFn = moduleMocker.fn();
mockFn
.mockImplementationOnce(() => {
return 'Foo';
})
.mockImplementationOnce(() => {
return 'Bar';
})
.mockImplementation(() => {
return 'Default';
});
expect(mockFn()).toBe('Foo');
expect(mockFn()).toBe('Bar');
expect(mockFn()).toBe('Default');
expect(mockFn()).toBe('Default');
});
});
test('mockImplementation resets the mock', () => {
const fn = jest.fn();
expect(fn()).toBeUndefined();
fn.mockReturnValue('returnValue');
fn.mockImplementation(() => 'foo');
expect(fn()).toBe('foo');
});
it('should recognize a mocked function', () => {
const mockFn = moduleMocker.fn();
expect(moduleMocker.isMockFunction(() => {})).toBe(false);
expect(moduleMocker.isMockFunction(mockFn)).toBe(true);
});
test('default mockName is jest.fn()', () => {
const fn = jest.fn();
expect(fn.getMockName()).toBe('jest.fn()');
});
test('mockName sets the mock name', () => {
const fn = jest.fn();
fn.mockName('myMockFn');
expect(fn.getMockName()).toBe('myMockFn');
});
test('mockName gets reset by mockReset', () => {
const fn = jest.fn();
expect(fn.getMockName()).toBe('jest.fn()');
fn.mockName('myMockFn');
expect(fn.getMockName()).toBe('myMockFn');
fn.mockReset();
expect(fn.getMockName()).toBe('jest.fn()');
});
test('mockName is not reset by mockRestore', () => {
const fn = jest.fn(() => false);
fn.mockName('myMockFn');
expect(fn.getMockName()).toBe('myMockFn');
fn.mockRestore();
expect(fn.getMockName()).toBe('myMockFn');
});
test('mockName is not reset by mockClear', () => {
const fn = jest.fn(() => false);
fn.mockName('myMockFn');
expect(fn.getMockName()).toBe('myMockFn');
fn.mockClear();
expect(fn.getMockName()).toBe('myMockFn');
});
describe('spyOn', () => {
it('should work', () => {
let isOriginalCalled = false;
let originalCallThis;
let originalCallArguments;
const obj = {
method() {
isOriginalCalled = true;
originalCallThis = this;
originalCallArguments = arguments;
},
};
const spy = moduleMocker.spyOn(obj, 'method');
const thisArg = {this: true};
const firstArg = {first: true};
const secondArg = {second: true};
obj.method.call(thisArg, firstArg, secondArg);
expect(isOriginalCalled).toBe(true);
expect(originalCallThis).toBe(thisArg);
expect(originalCallArguments.length).toBe(2);
expect(originalCallArguments[0]).toBe(firstArg);
expect(originalCallArguments[1]).toBe(secondArg);
expect(spy).toHaveBeenCalled();
isOriginalCalled = false;
originalCallThis = null;
originalCallArguments = null;
spy.mockReset();
spy.mockRestore();
obj.method.call(thisArg, firstArg, secondArg);
expect(isOriginalCalled).toBe(true);
expect(originalCallThis).toBe(thisArg);
expect(originalCallArguments.length).toBe(2);
expect(originalCallArguments[0]).toBe(firstArg);
expect(originalCallArguments[1]).toBe(secondArg);
expect(spy).not.toHaveBeenCalled();
});
it('should throw on invalid input', () => {
expect(() => {
moduleMocker.spyOn(null, 'method');
}).toThrow();
expect(() => {
moduleMocker.spyOn({}, 'method');
}).toThrow();
expect(() => {
moduleMocker.spyOn({method: 10}, 'method');
}).toThrow();
});
it('supports restoring all spies', () => {
let methodOneCalls = 0;
let methodTwoCalls = 0;
const obj = {
methodOne() {
methodOneCalls++;
},
methodTwo() {
methodTwoCalls++;
},
};
const spy1 = moduleMocker.spyOn(obj, 'methodOne');
const spy2 = moduleMocker.spyOn(obj, 'methodTwo');
// First, we call with the spies: both spies and both original functions
// should be called.
obj.methodOne();
obj.methodTwo();
expect(methodOneCalls).toBe(1);
expect(methodTwoCalls).toBe(1);
expect(spy1.mock.calls.length).toBe(1);
expect(spy2.mock.calls.length).toBe(1);
moduleMocker.restoreAllMocks();
// Then, after resetting all mocks, we call methods again. Only the real
// methods should bump their count, not the spies.
obj.methodOne();
obj.methodTwo();
expect(methodOneCalls).toBe(2);
expect(methodTwoCalls).toBe(2);
expect(spy1.mock.calls.length).toBe(1);
expect(spy2.mock.calls.length).toBe(1);
});
});
describe('spyOnProperty', () => {
it('should work - getter', () => {
let isOriginalCalled = false;
let originalCallThis;
let originalCallArguments;
const obj = {
get method() {
return function() {
isOriginalCalled = true;
originalCallThis = this;
originalCallArguments = arguments;
};
},
};
const spy = moduleMocker.spyOn(obj, 'method', 'get');
const thisArg = {this: true};
const firstArg = {first: true};
const secondArg = {second: true};
obj.method.call(thisArg, firstArg, secondArg);
expect(isOriginalCalled).toBe(true);
expect(originalCallThis).toBe(thisArg);
expect(originalCallArguments.length).toBe(2);
expect(originalCallArguments[0]).toBe(firstArg);
expect(originalCallArguments[1]).toBe(secondArg);
expect(spy).toHaveBeenCalled();
isOriginalCalled = false;
originalCallThis = null;
originalCallArguments = null;
spy.mockReset();
spy.mockRestore();
obj.method.call(thisArg, firstArg, secondArg);
expect(isOriginalCalled).toBe(true);
expect(originalCallThis).toBe(thisArg);
expect(originalCallArguments.length).toBe(2);
expect(originalCallArguments[0]).toBe(firstArg);
expect(originalCallArguments[1]).toBe(secondArg);
expect(spy).not.toHaveBeenCalled();
});
it('should work - setter', () => {
const obj = {
_property: false,
set property(value) {
this._property = value;
},
get property() {
return this._property;
},
};
const spy = moduleMocker.spyOn(obj, 'property', 'set');
obj.property = true;
expect(spy).toHaveBeenCalled();
expect(obj.property).toBe(true);
obj.property = false;
spy.mockReset();
spy.mockRestore();
obj.property = true;
expect(spy).not.toHaveBeenCalled();
expect(obj.property).toBe(true);
});
it('should throw on invalid input', () => {
expect(() => {
moduleMocker.spyOn(null, 'method');
}).toThrow();
expect(() => {
moduleMocker.spyOn({}, 'method');
}).toThrow();
expect(() => {
moduleMocker.spyOn({method: 10}, 'method');
}).toThrow();
});
it('supports restoring all spies', () => {
let methodOneCalls = 0;
let methodTwoCalls = 0;
const obj = {
get methodOne() {
return function() {
methodOneCalls++;
};
},
get methodTwo() {
return function() {
methodTwoCalls++;
};
},
};
const spy1 = moduleMocker.spyOn(obj, 'methodOne', 'get');
const spy2 = moduleMocker.spyOn(obj, 'methodTwo', 'get');
// First, we call with the spies: both spies and both original functions
// should be called.
obj.methodOne();
obj.methodTwo();
expect(methodOneCalls).toBe(1);
expect(methodTwoCalls).toBe(1);
expect(spy1.mock.calls.length).toBe(1);
expect(spy2.mock.calls.length).toBe(1);
moduleMocker.restoreAllMocks();
// Then, after resetting all mocks, we call methods again. Only the real
// methods should bump their count, not the spies.
obj.methodOne();
obj.methodTwo();
expect(methodOneCalls).toBe(2);
expect(methodTwoCalls).toBe(2);
expect(spy1.mock.calls.length).toBe(1);
expect(spy2.mock.calls.length).toBe(1);
});
});
});