From 3703bdece381f3929c21cdac8df6347bf6d7c8dc Mon Sep 17 00:00:00 2001 From: Jeff See Date: Thu, 21 Mar 2019 15:28:28 +1100 Subject: [PATCH 1/7] Emphasize 'mock' variable name exception in docs The docs don't explicitly say that in the `mockImplementation` example `const mockPlaySoundFile = jest.fn();` is relying on some behind-the-scenes magic. Not sure why but the explanation didn't click for me and it seems like I'm not the only one. This is an attempt to highlight this point --- docs/Es6ClassMocks.md | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/docs/Es6ClassMocks.md b/docs/Es6ClassMocks.md index a0b744eb5cdc..ffdcfcf00495 100644 --- a/docs/Es6ClassMocks.md +++ b/docs/Es6ClassMocks.md @@ -145,7 +145,17 @@ jest.mock('./sound-player', () => { }); ``` -A limitation with the factory parameter is that, since calls to `jest.mock()` are hoisted to the top of the file, it's not possible to first define a variable and then use it in the factory. An exception is made for variables that start with the word 'mock'. It's up to you to guarantee that they will be initialized on time! +A limitation with the factory parameter is that, since calls to `jest.mock()` are hoisted to the top of the file, it's not possible to first define a variable and then use it in the factory. An exception is made for variables that start with the word 'mock'. It's up to you to guarantee that they will be initialized on time! For example, the following will throw an out-of-scope error due to the use of 'fake' instead of 'mock' in the variable declaration: + +```javascript +import SoundPlayer from './sound-player'; +const fakePlaySoundFile = jest.fn(); +jest.mock('./sound-player', () => { + return jest.fn().mockImplementation(() => { + return {playSoundFile: fakePlaySoundFile}; + }); +}); +``` ### Replacing the mock using [`mockImplementation()`](MockFunctionAPI.md#mockfnmockimplementationfn) or [`mockImplementationOnce()`](MockFunctionAPI.md#mockfnmockimplementationoncefn) From d18048aff658b8236db90a93ae2aa168748b1004 Mon Sep 17 00:00:00 2001 From: Jeff See Date: Thu, 21 Mar 2019 15:34:09 +1100 Subject: [PATCH 2/7] Update Es6ClassMocks.md --- docs/Es6ClassMocks.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/docs/Es6ClassMocks.md b/docs/Es6ClassMocks.md index ffdcfcf00495..0ae096f4adf7 100644 --- a/docs/Es6ClassMocks.md +++ b/docs/Es6ClassMocks.md @@ -148,6 +148,8 @@ jest.mock('./sound-player', () => { A limitation with the factory parameter is that, since calls to `jest.mock()` are hoisted to the top of the file, it's not possible to first define a variable and then use it in the factory. An exception is made for variables that start with the word 'mock'. It's up to you to guarantee that they will be initialized on time! For example, the following will throw an out-of-scope error due to the use of 'fake' instead of 'mock' in the variable declaration: ```javascript + +// Note: this will fail import SoundPlayer from './sound-player'; const fakePlaySoundFile = jest.fn(); jest.mock('./sound-player', () => { From b78091d81700ddb02efea8768d152350a49927a3 Mon Sep 17 00:00:00 2001 From: Jeff See Date: Thu, 21 Mar 2019 15:34:55 +1100 Subject: [PATCH 3/7] Update Es6ClassMocks.md --- docs/Es6ClassMocks.md | 1 - 1 file changed, 1 deletion(-) diff --git a/docs/Es6ClassMocks.md b/docs/Es6ClassMocks.md index 0ae096f4adf7..01556f330fed 100644 --- a/docs/Es6ClassMocks.md +++ b/docs/Es6ClassMocks.md @@ -148,7 +148,6 @@ jest.mock('./sound-player', () => { A limitation with the factory parameter is that, since calls to `jest.mock()` are hoisted to the top of the file, it's not possible to first define a variable and then use it in the factory. An exception is made for variables that start with the word 'mock'. It's up to you to guarantee that they will be initialized on time! For example, the following will throw an out-of-scope error due to the use of 'fake' instead of 'mock' in the variable declaration: ```javascript - // Note: this will fail import SoundPlayer from './sound-player'; const fakePlaySoundFile = jest.fn(); From f3e39889b9ae0fcd6cacab99900aa166deaead25 Mon Sep 17 00:00:00 2001 From: Jeff See Date: Wed, 3 Apr 2019 08:01:14 +1100 Subject: [PATCH 4/7] Add mock example to version 23 docs --- .../versioned_docs/version-23.x/Es6ClassMocks.md | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/website/versioned_docs/version-23.x/Es6ClassMocks.md b/website/versioned_docs/version-23.x/Es6ClassMocks.md index 32a864d69195..e7cbaf8079ff 100644 --- a/website/versioned_docs/version-23.x/Es6ClassMocks.md +++ b/website/versioned_docs/version-23.x/Es6ClassMocks.md @@ -146,7 +146,18 @@ jest.mock('./sound-player', () => { }); ``` -A limitation with the factory parameter is that, since calls to `jest.mock()` are hoisted to the top of the file, it's not possible to first define a variable and then use it in the factory. An exception is made for variables that start with the word 'mock'. It's up to you to guarantee that they will be initialized on time! +A limitation with the factory parameter is that, since calls to `jest.mock()` are hoisted to the top of the file, it's not possible to first define a variable and then use it in the factory. An exception is made for variables that start with the word 'mock'. It's up to you to guarantee that they will be initialized on time! For example, the following will throw an out-of-scope error due to the use of 'fake' instead of 'mock' in the variable declaration: + + ```javascript +// Note: this will fail +import SoundPlayer from './sound-player'; +const fakePlaySoundFile = jest.fn(); +jest.mock('./sound-player', () => { + return jest.fn().mockImplementation(() => { + return {playSoundFile: fakePlaySoundFile}; + }); +}); +``` ### Replacing the mock using [`mockImplementation()`](MockFunctionAPI.md#mockfnmockimplementationfn) or [`mockImplementationOnce()`](MockFunctionAPI.md#mockfnmockimplementationoncefn) From aa9753d9452bd733104004aac11d2ce0b08ecc23 Mon Sep 17 00:00:00 2001 From: Jeff See Date: Wed, 3 Apr 2019 08:01:43 +1100 Subject: [PATCH 5/7] Add mock example to version 24 docs --- .../versioned_docs/version-24.0/Es6ClassMocks.md | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/website/versioned_docs/version-24.0/Es6ClassMocks.md b/website/versioned_docs/version-24.0/Es6ClassMocks.md index 97e43754114f..0579c0928931 100644 --- a/website/versioned_docs/version-24.0/Es6ClassMocks.md +++ b/website/versioned_docs/version-24.0/Es6ClassMocks.md @@ -146,7 +146,18 @@ jest.mock('./sound-player', () => { }); ``` -A limitation with the factory parameter is that, since calls to `jest.mock()` are hoisted to the top of the file, it's not possible to first define a variable and then use it in the factory. An exception is made for variables that start with the word 'mock'. It's up to you to guarantee that they will be initialized on time! +A limitation with the factory parameter is that, since calls to `jest.mock()` are hoisted to the top of the file, it's not possible to first define a variable and then use it in the factory. An exception is made for variables that start with the word 'mock'. It's up to you to guarantee that they will be initialized on time! For example, the following will throw an out-of-scope error due to the use of 'fake' instead of 'mock' in the variable declaration: + + ```javascript +// Note: this will fail +import SoundPlayer from './sound-player'; +const fakePlaySoundFile = jest.fn(); +jest.mock('./sound-player', () => { + return jest.fn().mockImplementation(() => { + return {playSoundFile: fakePlaySoundFile}; + }); +}); +``` ### Replacing the mock using [`mockImplementation()`](MockFunctionAPI.md#mockfnmockimplementationfn) or [`mockImplementationOnce()`](MockFunctionAPI.md#mockfnmockimplementationoncefn) From 49e60d5e800b4a5c58efebf6b9ed6736ea54d7be Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Wed, 3 Apr 2019 00:58:01 +0200 Subject: [PATCH 6/7] Update Es6ClassMocks.md --- website/versioned_docs/version-23.x/Es6ClassMocks.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/website/versioned_docs/version-23.x/Es6ClassMocks.md b/website/versioned_docs/version-23.x/Es6ClassMocks.md index e7cbaf8079ff..94b0fc1550c7 100644 --- a/website/versioned_docs/version-23.x/Es6ClassMocks.md +++ b/website/versioned_docs/version-23.x/Es6ClassMocks.md @@ -148,7 +148,7 @@ jest.mock('./sound-player', () => { A limitation with the factory parameter is that, since calls to `jest.mock()` are hoisted to the top of the file, it's not possible to first define a variable and then use it in the factory. An exception is made for variables that start with the word 'mock'. It's up to you to guarantee that they will be initialized on time! For example, the following will throw an out-of-scope error due to the use of 'fake' instead of 'mock' in the variable declaration: - ```javascript +```javascript // Note: this will fail import SoundPlayer from './sound-player'; const fakePlaySoundFile = jest.fn(); From 5bed9f406021754f0be36e13840f6ec6bc8c6f77 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Wed, 3 Apr 2019 00:58:20 +0200 Subject: [PATCH 7/7] Update Es6ClassMocks.md --- website/versioned_docs/version-24.0/Es6ClassMocks.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/website/versioned_docs/version-24.0/Es6ClassMocks.md b/website/versioned_docs/version-24.0/Es6ClassMocks.md index 0579c0928931..0fb7dcbb9095 100644 --- a/website/versioned_docs/version-24.0/Es6ClassMocks.md +++ b/website/versioned_docs/version-24.0/Es6ClassMocks.md @@ -148,7 +148,7 @@ jest.mock('./sound-player', () => { A limitation with the factory parameter is that, since calls to `jest.mock()` are hoisted to the top of the file, it's not possible to first define a variable and then use it in the factory. An exception is made for variables that start with the word 'mock'. It's up to you to guarantee that they will be initialized on time! For example, the following will throw an out-of-scope error due to the use of 'fake' instead of 'mock' in the variable declaration: - ```javascript +```javascript // Note: this will fail import SoundPlayer from './sound-player'; const fakePlaySoundFile = jest.fn();