From c98fc56e37a9904c251cd49efa0b1e33fac5b09a Mon Sep 17 00:00:00 2001 From: Miguel Yax Date: Wed, 13 Jul 2022 02:05:53 -0600 Subject: [PATCH] Update spyOn docs (#13000) --- CHANGELOG.md | 1 + docs/JestObjectAPI.md | 28 ++++++++++++++----- .../version-25.x/JestObjectAPI.md | 28 ++++++++++++++----- .../version-26.x/JestObjectAPI.md | 28 ++++++++++++++----- .../version-27.x/JestObjectAPI.md | 28 ++++++++++++++----- .../version-28.0/JestObjectAPI.md | 28 ++++++++++++++----- .../version-28.1/JestObjectAPI.md | 28 ++++++++++++++----- 7 files changed, 127 insertions(+), 42 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index dea223aba580..6231663782db 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +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)) ### Performance diff --git a/docs/JestObjectAPI.md b/docs/JestObjectAPI.md index 9d3629f2a7a5..c68f4d009133 100644 --- a/docs/JestObjectAPI.md +++ b/docs/JestObjectAPI.md @@ -481,7 +481,17 @@ 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 +510,17 @@ Example test: ```js const video = require('./video'); +afterEach(() => { + // restore the spy created with spyOn + jest.restoreAllMocks(); +}); + test('plays video', () => { const spy = jest.spyOn(video, 'play'); const isPlaying = video.play(); expect(spy).toHaveBeenCalled(); expect(isPlaying).toBe(true); - - spy.mockRestore(); }); ``` @@ -547,14 +560,17 @@ Example test: const audio = require('./audio'); const video = require('./video'); +afterEach(() => { + // restore the spy created with spyOn + 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(); }); test('plays audio', () => { @@ -563,8 +579,6 @@ test('plays audio', () => { expect(spy).toHaveBeenCalled(); expect(audio.volume).toBe(100); - - spy.mockRestore(); }); ``` diff --git a/website/versioned_docs/version-25.x/JestObjectAPI.md b/website/versioned_docs/version-25.x/JestObjectAPI.md index 59e0eaa9068f..56207071ef37 100644 --- a/website/versioned_docs/version-25.x/JestObjectAPI.md +++ b/website/versioned_docs/version-25.x/JestObjectAPI.md @@ -471,7 +471,17 @@ 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: @@ -490,14 +500,17 @@ Example test: ```js const video = require('./video'); +afterEach(() => { + // restore the spy created with spyOn + jest.restoreAllMocks(); +}); + test('plays video', () => { const spy = jest.spyOn(video, 'play'); const isPlaying = video.play(); expect(spy).toHaveBeenCalled(); expect(isPlaying).toBe(true); - - spy.mockRestore(); }); ``` @@ -537,14 +550,17 @@ Example test: const audio = require('./audio'); const video = require('./video'); +afterEach(() => { + // restore the spy created with spyOn + 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(); }); test('plays audio', () => { @@ -553,8 +569,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..e539b60f1849 100644 --- a/website/versioned_docs/version-26.x/JestObjectAPI.md +++ b/website/versioned_docs/version-26.x/JestObjectAPI.md @@ -475,7 +475,17 @@ 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,14 +504,17 @@ Example test: ```js const video = require('./video'); +afterEach(() => { + // restore the spy created with spyOn + jest.restoreAllMocks(); +}); + test('plays video', () => { const spy = jest.spyOn(video, 'play'); const isPlaying = video.play(); expect(spy).toHaveBeenCalled(); expect(isPlaying).toBe(true); - - spy.mockRestore(); }); ``` @@ -541,14 +554,17 @@ Example test: const audio = require('./audio'); const video = require('./video'); +afterEach(() => { + // restore the spy created with spyOn + 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(); }); test('plays audio', () => { @@ -557,8 +573,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..6b8dea8ab37b 100644 --- a/website/versioned_docs/version-27.x/JestObjectAPI.md +++ b/website/versioned_docs/version-27.x/JestObjectAPI.md @@ -475,7 +475,17 @@ 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,14 +504,17 @@ Example test: ```js const video = require('./video'); +afterEach(() => { + // restore the spy created with spyOn + jest.restoreAllMocks(); +}); + test('plays video', () => { const spy = jest.spyOn(video, 'play'); const isPlaying = video.play(); expect(spy).toHaveBeenCalled(); expect(isPlaying).toBe(true); - - spy.mockRestore(); }); ``` @@ -541,14 +554,17 @@ Example test: const audio = require('./audio'); const video = require('./video'); +afterEach(() => { + // restore the spy created with spyOn + 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(); }); test('plays audio', () => { @@ -557,8 +573,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..c68f4d009133 100644 --- a/website/versioned_docs/version-28.0/JestObjectAPI.md +++ b/website/versioned_docs/version-28.0/JestObjectAPI.md @@ -481,7 +481,17 @@ 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 +510,17 @@ Example test: ```js const video = require('./video'); +afterEach(() => { + // restore the spy created with spyOn + jest.restoreAllMocks(); +}); + test('plays video', () => { const spy = jest.spyOn(video, 'play'); const isPlaying = video.play(); expect(spy).toHaveBeenCalled(); expect(isPlaying).toBe(true); - - spy.mockRestore(); }); ``` @@ -547,14 +560,17 @@ Example test: const audio = require('./audio'); const video = require('./video'); +afterEach(() => { + // restore the spy created with spyOn + 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(); }); test('plays audio', () => { @@ -563,8 +579,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..c68f4d009133 100644 --- a/website/versioned_docs/version-28.1/JestObjectAPI.md +++ b/website/versioned_docs/version-28.1/JestObjectAPI.md @@ -481,7 +481,17 @@ 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 +510,17 @@ Example test: ```js const video = require('./video'); +afterEach(() => { + // restore the spy created with spyOn + jest.restoreAllMocks(); +}); + test('plays video', () => { const spy = jest.spyOn(video, 'play'); const isPlaying = video.play(); expect(spy).toHaveBeenCalled(); expect(isPlaying).toBe(true); - - spy.mockRestore(); }); ``` @@ -547,14 +560,17 @@ Example test: const audio = require('./audio'); const video = require('./video'); +afterEach(() => { + // restore the spy created with spyOn + 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(); }); test('plays audio', () => { @@ -563,8 +579,6 @@ test('plays audio', () => { expect(spy).toHaveBeenCalled(); expect(audio.volume).toBe(100); - - spy.mockRestore(); }); ```