Skip to content

Commit c98fc56

Browse files
authoredJul 13, 2022
Update spyOn docs (#13000)
1 parent ba443a5 commit c98fc56

File tree

7 files changed

+127
-42
lines changed

7 files changed

+127
-42
lines changed
 

‎CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
### Chore & Maintenance
1515

1616
- `[*]` Replace internal usage of `pretty-format/ConvertAnsi` with `jest-serializer-ansi-escapes` ([#12935](https://github.com/facebook/jest/pull/12935), [#13004](https://github.com/facebook/jest/pull/13004))
17+
- `[docs]` Update spyOn docs ([#13000](https://github.com/facebook/jest/pull/13000))
1718

1819
### Performance
1920

‎docs/JestObjectAPI.md

+21-7
Original file line numberDiff line numberDiff line change
@@ -481,7 +481,17 @@ Determines if the given function is a mocked function.
481481

482482
Creates a mock function similar to `jest.fn` but also tracks calls to `object[methodName]`. Returns a Jest [mock function](MockFunctionAPI.md).
483483

484-
_Note: By default, `jest.spyOn` also calls the **spied** method. This is different behavior from most other test libraries. If you want to overwrite the original function, you can use `jest.spyOn(object, methodName).mockImplementation(() => customImplementation)` or `object[methodName] = jest.fn(() => customImplementation);`_
484+
:::note
485+
486+
By default, `jest.spyOn` also calls the **spied** method. This is different behavior from most other test libraries. If you want to overwrite the original function, you can use `jest.spyOn(object, methodName).mockImplementation(() => customImplementation)` or `object[methodName] = jest.fn(() => customImplementation);`
487+
488+
:::
489+
490+
:::tip
491+
492+
Since `jest.spyOn` is a mock. You could restore the initial state calling [jest.restoreAllMocks](#jestrestoreallmocks) on [afterEach](GlobalAPI.md#aftereachfn-timeout) method.
493+
494+
:::
485495

486496
Example:
487497

@@ -500,14 +510,17 @@ Example test:
500510
```js
501511
const video = require('./video');
502512

513+
afterEach(() => {
514+
// restore the spy created with spyOn
515+
jest.restoreAllMocks();
516+
});
517+
503518
test('plays video', () => {
504519
const spy = jest.spyOn(video, 'play');
505520
const isPlaying = video.play();
506521

507522
expect(spy).toHaveBeenCalled();
508523
expect(isPlaying).toBe(true);
509-
510-
spy.mockRestore();
511524
});
512525
```
513526

@@ -547,14 +560,17 @@ Example test:
547560
const audio = require('./audio');
548561
const video = require('./video');
549562

563+
afterEach(() => {
564+
// restore the spy created with spyOn
565+
jest.restoreAllMocks();
566+
});
567+
550568
test('plays video', () => {
551569
const spy = jest.spyOn(video, 'play', 'get'); // we pass 'get'
552570
const isPlaying = video.play;
553571

554572
expect(spy).toHaveBeenCalled();
555573
expect(isPlaying).toBe(true);
556-
557-
spy.mockRestore();
558574
});
559575

560576
test('plays audio', () => {
@@ -563,8 +579,6 @@ test('plays audio', () => {
563579

564580
expect(spy).toHaveBeenCalled();
565581
expect(audio.volume).toBe(100);
566-
567-
spy.mockRestore();
568582
});
569583
```
570584

‎website/versioned_docs/version-25.x/JestObjectAPI.md

+21-7
Original file line numberDiff line numberDiff line change
@@ -471,7 +471,17 @@ Determines if the given function is a mocked function.
471471

472472
Creates a mock function similar to `jest.fn` but also tracks calls to `object[methodName]`. Returns a Jest [mock function](MockFunctionAPI.md).
473473

474-
_Note: By default, `jest.spyOn` also calls the **spied** method. This is different behavior from most other test libraries. If you want to overwrite the original function, you can use `jest.spyOn(object, methodName).mockImplementation(() => customImplementation)` or `object[methodName] = jest.fn(() => customImplementation);`_
474+
:::note
475+
476+
By default, `jest.spyOn` also calls the **spied** method. This is different behavior from most other test libraries. If you want to overwrite the original function, you can use `jest.spyOn(object, methodName).mockImplementation(() => customImplementation)` or `object[methodName] = jest.fn(() => customImplementation);`
477+
478+
:::
479+
480+
:::tip
481+
482+
Since `jest.spyOn` is a mock. You could restore the initial state calling [jest.restoreAllMocks](#jestrestoreallmocks) on [afterEach](GlobalAPI.md#aftereachfn-timeout) method.
483+
484+
:::
475485

476486
Example:
477487

@@ -490,14 +500,17 @@ Example test:
490500
```js
491501
const video = require('./video');
492502

503+
afterEach(() => {
504+
// restore the spy created with spyOn
505+
jest.restoreAllMocks();
506+
});
507+
493508
test('plays video', () => {
494509
const spy = jest.spyOn(video, 'play');
495510
const isPlaying = video.play();
496511

497512
expect(spy).toHaveBeenCalled();
498513
expect(isPlaying).toBe(true);
499-
500-
spy.mockRestore();
501514
});
502515
```
503516

@@ -537,14 +550,17 @@ Example test:
537550
const audio = require('./audio');
538551
const video = require('./video');
539552

553+
afterEach(() => {
554+
// restore the spy created with spyOn
555+
jest.restoreAllMocks();
556+
});
557+
540558
test('plays video', () => {
541559
const spy = jest.spyOn(video, 'play', 'get'); // we pass 'get'
542560
const isPlaying = video.play;
543561

544562
expect(spy).toHaveBeenCalled();
545563
expect(isPlaying).toBe(true);
546-
547-
spy.mockRestore();
548564
});
549565

550566
test('plays audio', () => {
@@ -553,8 +569,6 @@ test('plays audio', () => {
553569

554570
expect(spy).toHaveBeenCalled();
555571
expect(audio.volume).toBe(100);
556-
557-
spy.mockRestore();
558572
});
559573
```
560574

‎website/versioned_docs/version-26.x/JestObjectAPI.md

+21-7
Original file line numberDiff line numberDiff line change
@@ -475,7 +475,17 @@ Determines if the given function is a mocked function.
475475

476476
Creates a mock function similar to `jest.fn` but also tracks calls to `object[methodName]`. Returns a Jest [mock function](MockFunctionAPI.md).
477477

478-
_Note: By default, `jest.spyOn` also calls the **spied** method. This is different behavior from most other test libraries. If you want to overwrite the original function, you can use `jest.spyOn(object, methodName).mockImplementation(() => customImplementation)` or `object[methodName] = jest.fn(() => customImplementation);`_
478+
:::note
479+
480+
By default, `jest.spyOn` also calls the **spied** method. This is different behavior from most other test libraries. If you want to overwrite the original function, you can use `jest.spyOn(object, methodName).mockImplementation(() => customImplementation)` or `object[methodName] = jest.fn(() => customImplementation);`
481+
482+
:::
483+
484+
:::tip
485+
486+
Since `jest.spyOn` is a mock. You could restore the initial state calling [jest.restoreAllMocks](#jestrestoreallmocks) on [afterEach](GlobalAPI.md#aftereachfn-timeout) method.
487+
488+
:::
479489

480490
Example:
481491

@@ -494,14 +504,17 @@ Example test:
494504
```js
495505
const video = require('./video');
496506

507+
afterEach(() => {
508+
// restore the spy created with spyOn
509+
jest.restoreAllMocks();
510+
});
511+
497512
test('plays video', () => {
498513
const spy = jest.spyOn(video, 'play');
499514
const isPlaying = video.play();
500515

501516
expect(spy).toHaveBeenCalled();
502517
expect(isPlaying).toBe(true);
503-
504-
spy.mockRestore();
505518
});
506519
```
507520

@@ -541,14 +554,17 @@ Example test:
541554
const audio = require('./audio');
542555
const video = require('./video');
543556

557+
afterEach(() => {
558+
// restore the spy created with spyOn
559+
jest.restoreAllMocks();
560+
});
561+
544562
test('plays video', () => {
545563
const spy = jest.spyOn(video, 'play', 'get'); // we pass 'get'
546564
const isPlaying = video.play;
547565

548566
expect(spy).toHaveBeenCalled();
549567
expect(isPlaying).toBe(true);
550-
551-
spy.mockRestore();
552568
});
553569

554570
test('plays audio', () => {
@@ -557,8 +573,6 @@ test('plays audio', () => {
557573

558574
expect(spy).toHaveBeenCalled();
559575
expect(audio.volume).toBe(100);
560-
561-
spy.mockRestore();
562576
});
563577
```
564578

‎website/versioned_docs/version-27.x/JestObjectAPI.md

+21-7
Original file line numberDiff line numberDiff line change
@@ -475,7 +475,17 @@ Determines if the given function is a mocked function.
475475

476476
Creates a mock function similar to `jest.fn` but also tracks calls to `object[methodName]`. Returns a Jest [mock function](MockFunctionAPI.md).
477477

478-
_Note: By default, `jest.spyOn` also calls the **spied** method. This is different behavior from most other test libraries. If you want to overwrite the original function, you can use `jest.spyOn(object, methodName).mockImplementation(() => customImplementation)` or `object[methodName] = jest.fn(() => customImplementation);`_
478+
:::note
479+
480+
By default, `jest.spyOn` also calls the **spied** method. This is different behavior from most other test libraries. If you want to overwrite the original function, you can use `jest.spyOn(object, methodName).mockImplementation(() => customImplementation)` or `object[methodName] = jest.fn(() => customImplementation);`
481+
482+
:::
483+
484+
:::tip
485+
486+
Since `jest.spyOn` is a mock. You could restore the initial state calling [jest.restoreAllMocks](#jestrestoreallmocks) on [afterEach](GlobalAPI.md#aftereachfn-timeout) method.
487+
488+
:::
479489

480490
Example:
481491

@@ -494,14 +504,17 @@ Example test:
494504
```js
495505
const video = require('./video');
496506

507+
afterEach(() => {
508+
// restore the spy created with spyOn
509+
jest.restoreAllMocks();
510+
});
511+
497512
test('plays video', () => {
498513
const spy = jest.spyOn(video, 'play');
499514
const isPlaying = video.play();
500515

501516
expect(spy).toHaveBeenCalled();
502517
expect(isPlaying).toBe(true);
503-
504-
spy.mockRestore();
505518
});
506519
```
507520

@@ -541,14 +554,17 @@ Example test:
541554
const audio = require('./audio');
542555
const video = require('./video');
543556

557+
afterEach(() => {
558+
// restore the spy created with spyOn
559+
jest.restoreAllMocks();
560+
});
561+
544562
test('plays video', () => {
545563
const spy = jest.spyOn(video, 'play', 'get'); // we pass 'get'
546564
const isPlaying = video.play;
547565

548566
expect(spy).toHaveBeenCalled();
549567
expect(isPlaying).toBe(true);
550-
551-
spy.mockRestore();
552568
});
553569

554570
test('plays audio', () => {
@@ -557,8 +573,6 @@ test('plays audio', () => {
557573

558574
expect(spy).toHaveBeenCalled();
559575
expect(audio.volume).toBe(100);
560-
561-
spy.mockRestore();
562576
});
563577
```
564578

‎website/versioned_docs/version-28.0/JestObjectAPI.md

+21-7
Original file line numberDiff line numberDiff line change
@@ -481,7 +481,17 @@ Determines if the given function is a mocked function.
481481

482482
Creates a mock function similar to `jest.fn` but also tracks calls to `object[methodName]`. Returns a Jest [mock function](MockFunctionAPI.md).
483483

484-
_Note: By default, `jest.spyOn` also calls the **spied** method. This is different behavior from most other test libraries. If you want to overwrite the original function, you can use `jest.spyOn(object, methodName).mockImplementation(() => customImplementation)` or `object[methodName] = jest.fn(() => customImplementation);`_
484+
:::note
485+
486+
By default, `jest.spyOn` also calls the **spied** method. This is different behavior from most other test libraries. If you want to overwrite the original function, you can use `jest.spyOn(object, methodName).mockImplementation(() => customImplementation)` or `object[methodName] = jest.fn(() => customImplementation);`
487+
488+
:::
489+
490+
:::tip
491+
492+
Since `jest.spyOn` is a mock. You could restore the initial state calling [jest.restoreAllMocks](#jestrestoreallmocks) on [afterEach](GlobalAPI.md#aftereachfn-timeout) method.
493+
494+
:::
485495

486496
Example:
487497

@@ -500,14 +510,17 @@ Example test:
500510
```js
501511
const video = require('./video');
502512

513+
afterEach(() => {
514+
// restore the spy created with spyOn
515+
jest.restoreAllMocks();
516+
});
517+
503518
test('plays video', () => {
504519
const spy = jest.spyOn(video, 'play');
505520
const isPlaying = video.play();
506521

507522
expect(spy).toHaveBeenCalled();
508523
expect(isPlaying).toBe(true);
509-
510-
spy.mockRestore();
511524
});
512525
```
513526

@@ -547,14 +560,17 @@ Example test:
547560
const audio = require('./audio');
548561
const video = require('./video');
549562

563+
afterEach(() => {
564+
// restore the spy created with spyOn
565+
jest.restoreAllMocks();
566+
});
567+
550568
test('plays video', () => {
551569
const spy = jest.spyOn(video, 'play', 'get'); // we pass 'get'
552570
const isPlaying = video.play;
553571

554572
expect(spy).toHaveBeenCalled();
555573
expect(isPlaying).toBe(true);
556-
557-
spy.mockRestore();
558574
});
559575

560576
test('plays audio', () => {
@@ -563,8 +579,6 @@ test('plays audio', () => {
563579

564580
expect(spy).toHaveBeenCalled();
565581
expect(audio.volume).toBe(100);
566-
567-
spy.mockRestore();
568582
});
569583
```
570584

‎website/versioned_docs/version-28.1/JestObjectAPI.md

+21-7
Original file line numberDiff line numberDiff line change
@@ -481,7 +481,17 @@ Determines if the given function is a mocked function.
481481

482482
Creates a mock function similar to `jest.fn` but also tracks calls to `object[methodName]`. Returns a Jest [mock function](MockFunctionAPI.md).
483483

484-
_Note: By default, `jest.spyOn` also calls the **spied** method. This is different behavior from most other test libraries. If you want to overwrite the original function, you can use `jest.spyOn(object, methodName).mockImplementation(() => customImplementation)` or `object[methodName] = jest.fn(() => customImplementation);`_
484+
:::note
485+
486+
By default, `jest.spyOn` also calls the **spied** method. This is different behavior from most other test libraries. If you want to overwrite the original function, you can use `jest.spyOn(object, methodName).mockImplementation(() => customImplementation)` or `object[methodName] = jest.fn(() => customImplementation);`
487+
488+
:::
489+
490+
:::tip
491+
492+
Since `jest.spyOn` is a mock. You could restore the initial state calling [jest.restoreAllMocks](#jestrestoreallmocks) on [afterEach](GlobalAPI.md#aftereachfn-timeout) method.
493+
494+
:::
485495

486496
Example:
487497

@@ -500,14 +510,17 @@ Example test:
500510
```js
501511
const video = require('./video');
502512

513+
afterEach(() => {
514+
// restore the spy created with spyOn
515+
jest.restoreAllMocks();
516+
});
517+
503518
test('plays video', () => {
504519
const spy = jest.spyOn(video, 'play');
505520
const isPlaying = video.play();
506521

507522
expect(spy).toHaveBeenCalled();
508523
expect(isPlaying).toBe(true);
509-
510-
spy.mockRestore();
511524
});
512525
```
513526

@@ -547,14 +560,17 @@ Example test:
547560
const audio = require('./audio');
548561
const video = require('./video');
549562

563+
afterEach(() => {
564+
// restore the spy created with spyOn
565+
jest.restoreAllMocks();
566+
});
567+
550568
test('plays video', () => {
551569
const spy = jest.spyOn(video, 'play', 'get'); // we pass 'get'
552570
const isPlaying = video.play;
553571

554572
expect(spy).toHaveBeenCalled();
555573
expect(isPlaying).toBe(true);
556-
557-
spy.mockRestore();
558574
});
559575

560576
test('plays audio', () => {
@@ -563,8 +579,6 @@ test('plays audio', () => {
563579

564580
expect(spy).toHaveBeenCalled();
565581
expect(audio.volume).toBe(100);
566-
567-
spy.mockRestore();
568582
});
569583
```
570584

0 commit comments

Comments
 (0)
Please sign in to comment.