From e3d6de7301b4723765e2fb06eefcffb02a8e988c Mon Sep 17 00:00:00 2001 From: Miguel Yax Date: Thu, 7 Jul 2022 19:54:15 -0600 Subject: [PATCH 1/6] Update spyOn docs #4828 --- docs/JestObjectAPI.md | 28 ++++++++++++------- .../version-25.x/JestObjectAPI.md | 18 +++++++++--- .../version-26.x/JestObjectAPI.md | 19 ++++++++++--- .../version-27.x/JestObjectAPI.md | 19 ++++++++++--- .../version-28.0/JestObjectAPI.md | 19 ++++++++++--- .../version-28.1/JestObjectAPI.md | 21 +++++++++++--- 6 files changed, 94 insertions(+), 30 deletions(-) diff --git a/docs/JestObjectAPI.md b/docs/JestObjectAPI.md index 9d3629f2a7a5..a55eca97a629 100644 --- a/docs/JestObjectAPI.md +++ b/docs/JestObjectAPI.md @@ -481,7 +481,13 @@ Determines if the given function is a mocked function. Creates a mock function similar to `jest.fn` but also tracks calls to `object[methodName]`. Returns a Jest [mock function](MockFunctionAPI.md). -_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);`_ +:::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);` +::: + +:::tip +Since `jest.spyOn` is a mock. You could restore the initial state calling [jest.restoreAllMocks](#jestrestoreallmocks) on [afterEach](GlobalAPI.md#aftereachfn-timeout) method. +::: Example: @@ -500,14 +506,16 @@ Example test: ```js const video = require('./video'); +afterEach(() => { + jest.restoreAllMocks(); +}); + test('plays video', () => { const spy = jest.spyOn(video, 'play'); const isPlaying = video.play(); expect(spy).toHaveBeenCalled(); - expect(isPlaying).toBe(true); - - spy.mockRestore(); + expect(isPlaying).toBe(true); }); ``` @@ -547,14 +555,16 @@ Example test: const audio = require('./audio'); const video = require('./video'); +afterEach(() => { + jest.restoreAllMocks(); +}); + test('plays video', () => { const spy = jest.spyOn(video, 'play', 'get'); // we pass 'get' const isPlaying = video.play; expect(spy).toHaveBeenCalled(); - expect(isPlaying).toBe(true); - - spy.mockRestore(); + expect(isPlaying).toBe(true); }); test('plays audio', () => { @@ -562,9 +572,7 @@ test('plays audio', () => { audio.volume = 100; expect(spy).toHaveBeenCalled(); - expect(audio.volume).toBe(100); - - spy.mockRestore(); + expect(audio.volume).toBe(100); }); ``` diff --git a/website/versioned_docs/version-25.x/JestObjectAPI.md b/website/versioned_docs/version-25.x/JestObjectAPI.md index 59e0eaa9068f..e4567765fa05 100644 --- a/website/versioned_docs/version-25.x/JestObjectAPI.md +++ b/website/versioned_docs/version-25.x/JestObjectAPI.md @@ -470,8 +470,13 @@ Determines if the given function is a mocked function. ### `jest.spyOn(object, methodName)` Creates a mock function similar to `jest.fn` but also tracks calls to `object[methodName]`. Returns a Jest [mock function](MockFunctionAPI.md). +:::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);` +::: -_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);`_ +:::tip +Since `jest.spyOn` is a mock. You could restore the initial state calling [jest.restoreAllMocks](#jestrestoreallmocks) on [afterEach](GlobalAPI.md#aftereachfn-timeout) method. +::: Example: @@ -490,6 +495,10 @@ Example test: ```js const video = require('./video'); +afterEach(() => { + jest.restoreAllMocks(); +}); + test('plays video', () => { const spy = jest.spyOn(video, 'play'); const isPlaying = video.play(); @@ -497,7 +506,6 @@ test('plays video', () => { expect(spy).toHaveBeenCalled(); expect(isPlaying).toBe(true); - spy.mockRestore(); }); ``` @@ -537,6 +545,10 @@ Example test: const audio = require('./audio'); const video = require('./video'); +afterEach(() => { + jest.restoreAllMocks(); +}); + test('plays video', () => { const spy = jest.spyOn(video, 'play', 'get'); // we pass 'get' const isPlaying = video.play; @@ -544,7 +556,6 @@ test('plays video', () => { expect(spy).toHaveBeenCalled(); expect(isPlaying).toBe(true); - spy.mockRestore(); }); test('plays audio', () => { @@ -554,7 +565,6 @@ test('plays audio', () => { expect(spy).toHaveBeenCalled(); expect(audio.volume).toBe(100); - spy.mockRestore(); }); ``` diff --git a/website/versioned_docs/version-26.x/JestObjectAPI.md b/website/versioned_docs/version-26.x/JestObjectAPI.md index c491301cbbd8..94c47fa64411 100644 --- a/website/versioned_docs/version-26.x/JestObjectAPI.md +++ b/website/versioned_docs/version-26.x/JestObjectAPI.md @@ -475,7 +475,13 @@ Determines if the given function is a mocked function. Creates a mock function similar to `jest.fn` but also tracks calls to `object[methodName]`. Returns a Jest [mock function](MockFunctionAPI.md). -_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);`_ +:::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);` +::: + +:::tip +Since `jest.spyOn` is a mock. You could restore the initial state calling [jest.restoreAllMocks](#jestrestoreallmocks) on [afterEach](GlobalAPI.md#aftereachfn-timeout) method. +::: Example: @@ -494,6 +500,10 @@ Example test: ```js const video = require('./video'); +afterEach(() => { + jest.restoreAllMocks(); +}); + test('plays video', () => { const spy = jest.spyOn(video, 'play'); const isPlaying = video.play(); @@ -501,7 +511,6 @@ test('plays video', () => { expect(spy).toHaveBeenCalled(); expect(isPlaying).toBe(true); - spy.mockRestore(); }); ``` @@ -541,6 +550,10 @@ Example test: const audio = require('./audio'); const video = require('./video'); +afterEach(() => { + jest.restoreAllMocks(); +}); + test('plays video', () => { const spy = jest.spyOn(video, 'play', 'get'); // we pass 'get' const isPlaying = video.play; @@ -548,7 +561,6 @@ test('plays video', () => { expect(spy).toHaveBeenCalled(); expect(isPlaying).toBe(true); - spy.mockRestore(); }); test('plays audio', () => { @@ -558,7 +570,6 @@ test('plays audio', () => { expect(spy).toHaveBeenCalled(); expect(audio.volume).toBe(100); - spy.mockRestore(); }); ``` diff --git a/website/versioned_docs/version-27.x/JestObjectAPI.md b/website/versioned_docs/version-27.x/JestObjectAPI.md index 74dfb31bbed8..ef8a731a4f8c 100644 --- a/website/versioned_docs/version-27.x/JestObjectAPI.md +++ b/website/versioned_docs/version-27.x/JestObjectAPI.md @@ -475,7 +475,13 @@ Determines if the given function is a mocked function. Creates a mock function similar to `jest.fn` but also tracks calls to `object[methodName]`. Returns a Jest [mock function](MockFunctionAPI.md). -_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);`_ +:::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);` +::: + +:::tip +Since `jest.spyOn` is a mock. You could restore the initial state calling [jest.restoreAllMocks](#jestrestoreallmocks) on [afterEach](GlobalAPI.md#aftereachfn-timeout) method. +::: Example: @@ -494,6 +500,10 @@ Example test: ```js const video = require('./video'); +afterEach(() => { + jest.restoreAllMocks(); +}); + test('plays video', () => { const spy = jest.spyOn(video, 'play'); const isPlaying = video.play(); @@ -501,7 +511,6 @@ test('plays video', () => { expect(spy).toHaveBeenCalled(); expect(isPlaying).toBe(true); - spy.mockRestore(); }); ``` @@ -541,6 +550,10 @@ Example test: const audio = require('./audio'); const video = require('./video'); +afterEach(() => { + jest.restoreAllMocks(); +}); + test('plays video', () => { const spy = jest.spyOn(video, 'play', 'get'); // we pass 'get' const isPlaying = video.play; @@ -548,7 +561,6 @@ test('plays video', () => { expect(spy).toHaveBeenCalled(); expect(isPlaying).toBe(true); - spy.mockRestore(); }); test('plays audio', () => { @@ -558,7 +570,6 @@ test('plays audio', () => { expect(spy).toHaveBeenCalled(); expect(audio.volume).toBe(100); - spy.mockRestore(); }); ``` diff --git a/website/versioned_docs/version-28.0/JestObjectAPI.md b/website/versioned_docs/version-28.0/JestObjectAPI.md index 9d3629f2a7a5..6d33b6773eaa 100644 --- a/website/versioned_docs/version-28.0/JestObjectAPI.md +++ b/website/versioned_docs/version-28.0/JestObjectAPI.md @@ -481,7 +481,13 @@ Determines if the given function is a mocked function. Creates a mock function similar to `jest.fn` but also tracks calls to `object[methodName]`. Returns a Jest [mock function](MockFunctionAPI.md). -_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);`_ +:::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);` +::: + +:::tip +Since `jest.spyOn` is a mock. You could restore the initial state calling [jest.restoreAllMocks](#jestrestoreallmocks) on [afterEach](GlobalAPI.md#aftereachfn-timeout) method. +::: Example: @@ -500,6 +506,10 @@ Example test: ```js const video = require('./video'); +afterEach(() => { + jest.restoreAllMocks(); +}); + test('plays video', () => { const spy = jest.spyOn(video, 'play'); const isPlaying = video.play(); @@ -507,7 +517,6 @@ test('plays video', () => { expect(spy).toHaveBeenCalled(); expect(isPlaying).toBe(true); - spy.mockRestore(); }); ``` @@ -547,6 +556,10 @@ Example test: const audio = require('./audio'); const video = require('./video'); +afterEach(() => { + jest.restoreAllMocks(); +}); + test('plays video', () => { const spy = jest.spyOn(video, 'play', 'get'); // we pass 'get' const isPlaying = video.play; @@ -554,7 +567,6 @@ test('plays video', () => { expect(spy).toHaveBeenCalled(); expect(isPlaying).toBe(true); - spy.mockRestore(); }); test('plays audio', () => { @@ -564,7 +576,6 @@ test('plays audio', () => { expect(spy).toHaveBeenCalled(); expect(audio.volume).toBe(100); - spy.mockRestore(); }); ``` diff --git a/website/versioned_docs/version-28.1/JestObjectAPI.md b/website/versioned_docs/version-28.1/JestObjectAPI.md index 9d3629f2a7a5..41fd3c525c1b 100644 --- a/website/versioned_docs/version-28.1/JestObjectAPI.md +++ b/website/versioned_docs/version-28.1/JestObjectAPI.md @@ -481,7 +481,13 @@ Determines if the given function is a mocked function. Creates a mock function similar to `jest.fn` but also tracks calls to `object[methodName]`. Returns a Jest [mock function](MockFunctionAPI.md). -_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);`_ +:::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);` +::: + +:::tip +Since `jest.spyOn` is a mock. You could restore the initial state calling [jest.restoreAllMocks](#jestrestoreallmocks) on [afterEach](GlobalAPI.md#aftereachfn-timeout) method. +::: Example: @@ -500,6 +506,11 @@ Example test: ```js const video = require('./video'); +afterEach(() => { + jest.restoreAllMocks(); +}); + + test('plays video', () => { const spy = jest.spyOn(video, 'play'); const isPlaying = video.play(); @@ -507,7 +518,6 @@ test('plays video', () => { expect(spy).toHaveBeenCalled(); expect(isPlaying).toBe(true); - spy.mockRestore(); }); ``` @@ -547,6 +557,11 @@ Example test: const audio = require('./audio'); const video = require('./video'); +afterEach(() => { + jest.restoreAllMocks(); +}); + + test('plays video', () => { const spy = jest.spyOn(video, 'play', 'get'); // we pass 'get' const isPlaying = video.play; @@ -554,7 +569,6 @@ test('plays video', () => { expect(spy).toHaveBeenCalled(); expect(isPlaying).toBe(true); - spy.mockRestore(); }); test('plays audio', () => { @@ -564,7 +578,6 @@ test('plays audio', () => { expect(spy).toHaveBeenCalled(); expect(audio.volume).toBe(100); - spy.mockRestore(); }); ``` From ca7d4ea8f966534ec72e93651b6e213133fd7ad8 Mon Sep 17 00:00:00 2001 From: Miguel Yax Date: Thu, 7 Jul 2022 20:00:33 -0600 Subject: [PATCH 2/6] Update changelog.md file --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index bf8893b6b24f..b1033594576b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ ### Chore & Maintenance - `[*]` Replace internal usage of `pretty-format/ConvertAnsi` with `jest-serializer-ansi-escapes` ([#12935](https://github.com/facebook/jest/pull/12935)) +- `[docs]` Update spyOn docs ([#4828](https://github.com/facebook/jest/pull/4828)) ### Performance From 8247b01459ff301074daacb680ff7d41928a1536 Mon Sep 17 00:00:00 2001 From: Miguel Yax Date: Fri, 8 Jul 2022 05:21:46 -0600 Subject: [PATCH 3/6] Update CHANGELOG.md Co-authored-by: Tom Mrazauskas --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b1033594576b..5100fb040530 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,7 +13,7 @@ ### Chore & Maintenance - `[*]` Replace internal usage of `pretty-format/ConvertAnsi` with `jest-serializer-ansi-escapes` ([#12935](https://github.com/facebook/jest/pull/12935)) -- `[docs]` Update spyOn docs ([#4828](https://github.com/facebook/jest/pull/4828)) +- `[docs]` Update spyOn docs ([#13000](https://github.com/facebook/jest/pull/13000)) ### Performance From 6fa35fe8084565f7d6cf973c8a1c6970c13da702 Mon Sep 17 00:00:00 2001 From: Miguel Yax Date: Fri, 8 Jul 2022 06:00:54 -0600 Subject: [PATCH 4/6] Update spyOn examples adding a comment for jest.restoreAllMocks call --- docs/JestObjectAPI.md | 2 ++ website/versioned_docs/version-25.x/JestObjectAPI.md | 2 ++ website/versioned_docs/version-26.x/JestObjectAPI.md | 2 ++ website/versioned_docs/version-27.x/JestObjectAPI.md | 2 ++ website/versioned_docs/version-28.0/JestObjectAPI.md | 2 ++ website/versioned_docs/version-28.1/JestObjectAPI.md | 2 ++ 6 files changed, 12 insertions(+) diff --git a/docs/JestObjectAPI.md b/docs/JestObjectAPI.md index a55eca97a629..b6941fa0bca2 100644 --- a/docs/JestObjectAPI.md +++ b/docs/JestObjectAPI.md @@ -507,6 +507,7 @@ Example test: const video = require('./video'); afterEach(() => { + // restore the spy created with spyOn jest.restoreAllMocks(); }); @@ -556,6 +557,7 @@ const audio = require('./audio'); const video = require('./video'); afterEach(() => { + // restore the spy created with spyOn jest.restoreAllMocks(); }); diff --git a/website/versioned_docs/version-25.x/JestObjectAPI.md b/website/versioned_docs/version-25.x/JestObjectAPI.md index e4567765fa05..85566aaf8c24 100644 --- a/website/versioned_docs/version-25.x/JestObjectAPI.md +++ b/website/versioned_docs/version-25.x/JestObjectAPI.md @@ -496,6 +496,7 @@ Example test: const video = require('./video'); afterEach(() => { + // restore the spy created with spyOn jest.restoreAllMocks(); }); @@ -546,6 +547,7 @@ const audio = require('./audio'); const video = require('./video'); afterEach(() => { +// restore the spy created with spyOn jest.restoreAllMocks(); }); diff --git a/website/versioned_docs/version-26.x/JestObjectAPI.md b/website/versioned_docs/version-26.x/JestObjectAPI.md index 94c47fa64411..4acffeff2035 100644 --- a/website/versioned_docs/version-26.x/JestObjectAPI.md +++ b/website/versioned_docs/version-26.x/JestObjectAPI.md @@ -501,6 +501,7 @@ Example test: const video = require('./video'); afterEach(() => { + // restore the spy created with spyOn jest.restoreAllMocks(); }); @@ -551,6 +552,7 @@ const audio = require('./audio'); const video = require('./video'); afterEach(() => { + // restore the spy created with spyOn jest.restoreAllMocks(); }); diff --git a/website/versioned_docs/version-27.x/JestObjectAPI.md b/website/versioned_docs/version-27.x/JestObjectAPI.md index ef8a731a4f8c..e78bc1a0029e 100644 --- a/website/versioned_docs/version-27.x/JestObjectAPI.md +++ b/website/versioned_docs/version-27.x/JestObjectAPI.md @@ -501,6 +501,7 @@ Example test: const video = require('./video'); afterEach(() => { + // restore the spy created with spyOn jest.restoreAllMocks(); }); @@ -551,6 +552,7 @@ const audio = require('./audio'); const video = require('./video'); afterEach(() => { + // restore the spy created with spyOn jest.restoreAllMocks(); }); diff --git a/website/versioned_docs/version-28.0/JestObjectAPI.md b/website/versioned_docs/version-28.0/JestObjectAPI.md index 6d33b6773eaa..49c9eead0c4a 100644 --- a/website/versioned_docs/version-28.0/JestObjectAPI.md +++ b/website/versioned_docs/version-28.0/JestObjectAPI.md @@ -507,6 +507,7 @@ Example test: const video = require('./video'); afterEach(() => { + // restore the spy created with spyOn jest.restoreAllMocks(); }); @@ -557,6 +558,7 @@ const audio = require('./audio'); const video = require('./video'); afterEach(() => { + // restore the spy created with spyOn jest.restoreAllMocks(); }); diff --git a/website/versioned_docs/version-28.1/JestObjectAPI.md b/website/versioned_docs/version-28.1/JestObjectAPI.md index 41fd3c525c1b..9a22a58006f2 100644 --- a/website/versioned_docs/version-28.1/JestObjectAPI.md +++ b/website/versioned_docs/version-28.1/JestObjectAPI.md @@ -507,6 +507,7 @@ Example test: const video = require('./video'); afterEach(() => { + // restore the spy created with spyOn jest.restoreAllMocks(); }); @@ -558,6 +559,7 @@ const audio = require('./audio'); const video = require('./video'); afterEach(() => { + // restore the spy created with spyOn jest.restoreAllMocks(); }); From ba2a62f06d3b800ddaaf2607d2482d2e22375bf9 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Wed, 13 Jul 2022 09:51:54 +0200 Subject: [PATCH 5/6] prettier --- CHANGELOG.md | 2 +- docs/JestObjectAPI.md | 14 +++++++++----- .../versioned_docs/version-25.x/JestObjectAPI.md | 14 ++++++++------ .../versioned_docs/version-26.x/JestObjectAPI.md | 9 +++++---- .../versioned_docs/version-27.x/JestObjectAPI.md | 9 +++++---- .../versioned_docs/version-28.0/JestObjectAPI.md | 9 +++++---- .../versioned_docs/version-28.1/JestObjectAPI.md | 11 +++++------ 7 files changed, 38 insertions(+), 30 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b7fabc8b3854..6231663782db 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,7 +14,7 @@ ### Chore & Maintenance - `[*]` 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)) -- `[docs]` Update spyOn docs ([#13000](https://github.com/facebook/jest/pull/13000)) +- `[docs]` Update spyOn docs ([#13000](https://github.com/facebook/jest/pull/13000)) ### Performance diff --git a/docs/JestObjectAPI.md b/docs/JestObjectAPI.md index b6941fa0bca2..c68f4d009133 100644 --- a/docs/JestObjectAPI.md +++ b/docs/JestObjectAPI.md @@ -482,11 +482,15 @@ Determines if the given function is a mocked function. Creates a mock function similar to `jest.fn` but also tracks calls to `object[methodName]`. Returns a Jest [mock function](MockFunctionAPI.md). :::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);` + +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);` + ::: :::tip -Since `jest.spyOn` is a mock. You could restore the initial state calling [jest.restoreAllMocks](#jestrestoreallmocks) on [afterEach](GlobalAPI.md#aftereachfn-timeout) method. + +Since `jest.spyOn` is a mock. You could restore the initial state calling [jest.restoreAllMocks](#jestrestoreallmocks) on [afterEach](GlobalAPI.md#aftereachfn-timeout) method. + ::: Example: @@ -516,7 +520,7 @@ test('plays video', () => { const isPlaying = video.play(); expect(spy).toHaveBeenCalled(); - expect(isPlaying).toBe(true); + expect(isPlaying).toBe(true); }); ``` @@ -566,7 +570,7 @@ test('plays video', () => { const isPlaying = video.play; expect(spy).toHaveBeenCalled(); - expect(isPlaying).toBe(true); + expect(isPlaying).toBe(true); }); test('plays audio', () => { @@ -574,7 +578,7 @@ test('plays audio', () => { audio.volume = 100; expect(spy).toHaveBeenCalled(); - expect(audio.volume).toBe(100); + expect(audio.volume).toBe(100); }); ``` diff --git a/website/versioned_docs/version-25.x/JestObjectAPI.md b/website/versioned_docs/version-25.x/JestObjectAPI.md index 85566aaf8c24..16ec2a85af04 100644 --- a/website/versioned_docs/version-25.x/JestObjectAPI.md +++ b/website/versioned_docs/version-25.x/JestObjectAPI.md @@ -469,13 +469,18 @@ Determines if the given function is a mocked function. ### `jest.spyOn(object, methodName)` -Creates a mock function similar to `jest.fn` but also tracks calls to `object[methodName]`. Returns a Jest [mock function](MockFunctionAPI.md). +Creates a mock function similar to `jest.fn` but also tracks calls to `object[methodName]`. Returns a Jest [mock function](MockFunctionAPI.md). + :::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);` + ::: :::tip -Since `jest.spyOn` is a mock. You could restore the initial state calling [jest.restoreAllMocks](#jestrestoreallmocks) on [afterEach](GlobalAPI.md#aftereachfn-timeout) method. + +Since `jest.spyOn` is a mock. You could restore the initial state calling [jest.restoreAllMocks](#jestrestoreallmocks) on [afterEach](GlobalAPI.md#aftereachfn-timeout) method. + ::: Example: @@ -506,7 +511,6 @@ test('plays video', () => { expect(spy).toHaveBeenCalled(); expect(isPlaying).toBe(true); - }); ``` @@ -547,7 +551,7 @@ const audio = require('./audio'); const video = require('./video'); afterEach(() => { -// restore the spy created with spyOn + // restore the spy created with spyOn jest.restoreAllMocks(); }); @@ -557,7 +561,6 @@ test('plays video', () => { expect(spy).toHaveBeenCalled(); expect(isPlaying).toBe(true); - }); test('plays audio', () => { @@ -566,7 +569,6 @@ test('plays audio', () => { expect(spy).toHaveBeenCalled(); expect(audio.volume).toBe(100); - }); ``` diff --git a/website/versioned_docs/version-26.x/JestObjectAPI.md b/website/versioned_docs/version-26.x/JestObjectAPI.md index 4acffeff2035..e539b60f1849 100644 --- a/website/versioned_docs/version-26.x/JestObjectAPI.md +++ b/website/versioned_docs/version-26.x/JestObjectAPI.md @@ -476,11 +476,15 @@ Determines if the given function is a mocked function. Creates a mock function similar to `jest.fn` but also tracks calls to `object[methodName]`. Returns a Jest [mock function](MockFunctionAPI.md). :::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);` + ::: :::tip -Since `jest.spyOn` is a mock. You could restore the initial state calling [jest.restoreAllMocks](#jestrestoreallmocks) on [afterEach](GlobalAPI.md#aftereachfn-timeout) method. + +Since `jest.spyOn` is a mock. You could restore the initial state calling [jest.restoreAllMocks](#jestrestoreallmocks) on [afterEach](GlobalAPI.md#aftereachfn-timeout) method. + ::: Example: @@ -511,7 +515,6 @@ test('plays video', () => { expect(spy).toHaveBeenCalled(); expect(isPlaying).toBe(true); - }); ``` @@ -562,7 +565,6 @@ test('plays video', () => { expect(spy).toHaveBeenCalled(); expect(isPlaying).toBe(true); - }); test('plays audio', () => { @@ -571,7 +573,6 @@ test('plays audio', () => { expect(spy).toHaveBeenCalled(); expect(audio.volume).toBe(100); - }); ``` diff --git a/website/versioned_docs/version-27.x/JestObjectAPI.md b/website/versioned_docs/version-27.x/JestObjectAPI.md index e78bc1a0029e..6b8dea8ab37b 100644 --- a/website/versioned_docs/version-27.x/JestObjectAPI.md +++ b/website/versioned_docs/version-27.x/JestObjectAPI.md @@ -476,11 +476,15 @@ Determines if the given function is a mocked function. Creates a mock function similar to `jest.fn` but also tracks calls to `object[methodName]`. Returns a Jest [mock function](MockFunctionAPI.md). :::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);` + ::: :::tip -Since `jest.spyOn` is a mock. You could restore the initial state calling [jest.restoreAllMocks](#jestrestoreallmocks) on [afterEach](GlobalAPI.md#aftereachfn-timeout) method. + +Since `jest.spyOn` is a mock. You could restore the initial state calling [jest.restoreAllMocks](#jestrestoreallmocks) on [afterEach](GlobalAPI.md#aftereachfn-timeout) method. + ::: Example: @@ -511,7 +515,6 @@ test('plays video', () => { expect(spy).toHaveBeenCalled(); expect(isPlaying).toBe(true); - }); ``` @@ -562,7 +565,6 @@ test('plays video', () => { expect(spy).toHaveBeenCalled(); expect(isPlaying).toBe(true); - }); test('plays audio', () => { @@ -571,7 +573,6 @@ test('plays audio', () => { expect(spy).toHaveBeenCalled(); expect(audio.volume).toBe(100); - }); ``` diff --git a/website/versioned_docs/version-28.0/JestObjectAPI.md b/website/versioned_docs/version-28.0/JestObjectAPI.md index 49c9eead0c4a..c68f4d009133 100644 --- a/website/versioned_docs/version-28.0/JestObjectAPI.md +++ b/website/versioned_docs/version-28.0/JestObjectAPI.md @@ -482,11 +482,15 @@ Determines if the given function is a mocked function. Creates a mock function similar to `jest.fn` but also tracks calls to `object[methodName]`. Returns a Jest [mock function](MockFunctionAPI.md). :::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);` + ::: :::tip -Since `jest.spyOn` is a mock. You could restore the initial state calling [jest.restoreAllMocks](#jestrestoreallmocks) on [afterEach](GlobalAPI.md#aftereachfn-timeout) method. + +Since `jest.spyOn` is a mock. You could restore the initial state calling [jest.restoreAllMocks](#jestrestoreallmocks) on [afterEach](GlobalAPI.md#aftereachfn-timeout) method. + ::: Example: @@ -517,7 +521,6 @@ test('plays video', () => { expect(spy).toHaveBeenCalled(); expect(isPlaying).toBe(true); - }); ``` @@ -568,7 +571,6 @@ test('plays video', () => { expect(spy).toHaveBeenCalled(); expect(isPlaying).toBe(true); - }); test('plays audio', () => { @@ -577,7 +579,6 @@ test('plays audio', () => { expect(spy).toHaveBeenCalled(); expect(audio.volume).toBe(100); - }); ``` diff --git a/website/versioned_docs/version-28.1/JestObjectAPI.md b/website/versioned_docs/version-28.1/JestObjectAPI.md index 9a22a58006f2..c68f4d009133 100644 --- a/website/versioned_docs/version-28.1/JestObjectAPI.md +++ b/website/versioned_docs/version-28.1/JestObjectAPI.md @@ -482,11 +482,15 @@ Determines if the given function is a mocked function. Creates a mock function similar to `jest.fn` but also tracks calls to `object[methodName]`. Returns a Jest [mock function](MockFunctionAPI.md). :::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);` + ::: :::tip -Since `jest.spyOn` is a mock. You could restore the initial state calling [jest.restoreAllMocks](#jestrestoreallmocks) on [afterEach](GlobalAPI.md#aftereachfn-timeout) method. + +Since `jest.spyOn` is a mock. You could restore the initial state calling [jest.restoreAllMocks](#jestrestoreallmocks) on [afterEach](GlobalAPI.md#aftereachfn-timeout) method. + ::: Example: @@ -511,14 +515,12 @@ afterEach(() => { jest.restoreAllMocks(); }); - test('plays video', () => { const spy = jest.spyOn(video, 'play'); const isPlaying = video.play(); expect(spy).toHaveBeenCalled(); expect(isPlaying).toBe(true); - }); ``` @@ -563,14 +565,12 @@ afterEach(() => { jest.restoreAllMocks(); }); - test('plays video', () => { const spy = jest.spyOn(video, 'play', 'get'); // we pass 'get' const isPlaying = video.play; expect(spy).toHaveBeenCalled(); expect(isPlaying).toBe(true); - }); test('plays audio', () => { @@ -579,7 +579,6 @@ test('plays audio', () => { expect(spy).toHaveBeenCalled(); expect(audio.volume).toBe(100); - }); ``` From cb3e595a4213111b435068a83a02ccf4d860add2 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Wed, 13 Jul 2022 10:00:27 +0200 Subject: [PATCH 6/6] oops --- website/versioned_docs/version-25.x/JestObjectAPI.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/website/versioned_docs/version-25.x/JestObjectAPI.md b/website/versioned_docs/version-25.x/JestObjectAPI.md index 16ec2a85af04..56207071ef37 100644 --- a/website/versioned_docs/version-25.x/JestObjectAPI.md +++ b/website/versioned_docs/version-25.x/JestObjectAPI.md @@ -469,7 +469,7 @@ Determines if the given function is a mocked function. ### `jest.spyOn(object, methodName)` -Creates a mock function similar to `jest.fn` but also tracks calls to `object[methodName]`. Returns a Jest [mock function](MockFunctionAPI.md). +Creates a mock function similar to `jest.fn` but also tracks calls to `object[methodName]`. Returns a Jest [mock function](MockFunctionAPI.md). :::note