From 954bc794f00318581edd9f541239d78ed2d72ea4 Mon Sep 17 00:00:00 2001 From: Czysty Date: Tue, 20 Feb 2018 21:06:41 +0100 Subject: [PATCH 01/11] Update documentation about automocking --- docs/Configuration.md | 42 +++++++++++++++++-- docs/JestObjectAPI.md | 34 +++++++++++++++ examples/automatic_mocks/.babelrc | 3 ++ .../__tests__/automock.test.js | 16 +++++++ .../__tests__/disableAutomocking.test.js | 9 ++++ examples/automatic_mocks/package.json | 12 ++++++ examples/automatic_mocks/utils.js | 9 ++++ 7 files changed, 121 insertions(+), 4 deletions(-) create mode 100644 examples/automatic_mocks/.babelrc create mode 100644 examples/automatic_mocks/__tests__/automock.test.js create mode 100644 examples/automatic_mocks/__tests__/disableAutomocking.test.js create mode 100644 examples/automatic_mocks/package.json create mode 100644 examples/automatic_mocks/utils.js diff --git a/docs/Configuration.md b/docs/Configuration.md index 32a7746929a4..ff693f56c155 100644 --- a/docs/Configuration.md +++ b/docs/Configuration.md @@ -54,10 +54,44 @@ configuration power. Default: `false` -This option is disabled by default. If you are introducing Jest to a large -organization with an existing codebase but few tests, enabling this option can -be helpful to introduce unit tests gradually. Modules can be explicitly -auto-mocked using `jest.mock(moduleName)`. +This option tell to jest that all imported modules in your tests should be me +mocked, it means that the module will have a replacement implementation, but the +shape will be the same as original module. + +Example: + +```js +// utils.js +export default { + authorize: () => { + // implementation + return 'token'; + }, + isAuthorized: secret => secret === 'wizard', +}; +``` + +Then the imported module in test have the same shape as the original, but +implementations are replaced also will return `jest` API for each element. + +```js +import utils from '../utils'; + +test('if utils mocked automatically', () => { + expect(utils.authorize._isMockFunction).toBeTruthy(); + expect(utils.isAuthorized._isMockFunction).toBeTruthy(); + + utils.authorize.mockReturnValue('mocked_token'); + utils.isAuthorized.mockReturnValue(true); + + expect(utils.authorize()).toBe('mocked_token'); + expect(utils.isAuthorized('not_wizard')).toBeTruthy(); +}); +``` + +If you are introducing Jest to a large organization with an existing codebase +but few tests, enabling this option can be helpful to introduce unit tests +gradually. Modules can be explicitly auto-mocked using `jest.mock(moduleName)`. _Note: Core modules, like `fs`, are not mocked by default. They can be mocked explicitly, like `jest.mock('fs')`._ diff --git a/docs/JestObjectAPI.md b/docs/JestObjectAPI.md index 39efd22e65fe..ba4671fcd53e 100644 --- a/docs/JestObjectAPI.md +++ b/docs/JestObjectAPI.md @@ -49,9 +49,41 @@ will be cleared and will never have the opportunity to execute in the future. Disables automatic mocking in the module loader. +> See `automock` section of [configuration](Configuration.md) for more +> information + After this method is called, all `require()`s will return the real versions of each module (rather than a mocked version). +Jest configuration: + +```json +"automocking": true +``` + +Example: + +```js +// utils.js +export default { + authorize: () => { + // implementation + return 'token'; + }, +}; + +// __tests__/disableAutomocking.js +jest.disableAutomock(); + +import utils from '../utils'; + +test('original implementation', () => { + // now we have the original implementation, + // even if we set the automocking in a jest configuration + expect(utils.authorize()).toBe('token'); +}); +``` + This is usually useful when you have a scenario where the number of dependencies you want to mock is far less than the number of dependencies that you don't. For example, if you're writing a test for a module that uses a large number of @@ -75,6 +107,8 @@ Enables automatic mocking in the module loader. Returns the `jest` object for chaining. +> see [`jest.disableAutomock()`](#jestdisableautomock) + _Note: this method was previously called `autoMockOn`. When using `babel-jest`, calls to `enableAutomock` will automatically be hoisted to the top of the code block. Use `autoMockOn` if you want to explicitly avoid this behavior._ diff --git a/examples/automatic_mocks/.babelrc b/examples/automatic_mocks/.babelrc new file mode 100644 index 000000000000..002b4aa0d58e --- /dev/null +++ b/examples/automatic_mocks/.babelrc @@ -0,0 +1,3 @@ +{ + "presets": ["env"] +} diff --git a/examples/automatic_mocks/__tests__/automock.test.js b/examples/automatic_mocks/__tests__/automock.test.js new file mode 100644 index 000000000000..d18adc477c45 --- /dev/null +++ b/examples/automatic_mocks/__tests__/automock.test.js @@ -0,0 +1,16 @@ +// Copyright 2004-present Facebook. All Rights Reserved. + +import utils from '../utils'; + +test('if utils mocked', () => { + expect(utils.authorize._isMockFunction).toBeTruthy(); + expect(utils.isAuthorized._isMockFunction).toBeTruthy(); +}); + +test('replace implementation', () => { + utils.authorize.mockReturnValue('mocked_token'); + utils.isAuthorized.mockReturnValue(true); + + expect(utils.authorize()).toBe('mocked_token'); + expect(utils.isAuthorized('not_wizard')).toBeTruthy(); +}); diff --git a/examples/automatic_mocks/__tests__/disableAutomocking.test.js b/examples/automatic_mocks/__tests__/disableAutomocking.test.js new file mode 100644 index 000000000000..d92333a99638 --- /dev/null +++ b/examples/automatic_mocks/__tests__/disableAutomocking.test.js @@ -0,0 +1,9 @@ +// Copyright 2004-present Facebook. All Rights Reserved. + +jest.disableAutomock(); + +import utils from '../utils'; + +test('replace implementation', () => { + expect(utils.authorize()).toBe('token'); +}); diff --git a/examples/automatic_mocks/package.json b/examples/automatic_mocks/package.json new file mode 100644 index 000000000000..d90b15a8b6b3 --- /dev/null +++ b/examples/automatic_mocks/package.json @@ -0,0 +1,12 @@ +{ + "devDependencies": { + "babel-preset-env": "*", + "jest": "*" + }, + "scripts": { + "test": "jest" + }, + "jest": { + "automock": true + } +} diff --git a/examples/automatic_mocks/utils.js b/examples/automatic_mocks/utils.js new file mode 100644 index 000000000000..7f8f014d1018 --- /dev/null +++ b/examples/automatic_mocks/utils.js @@ -0,0 +1,9 @@ +// Copyright 2004-present Facebook. All Rights Reserved. + +export default { + authorize: () => { + // implementation + return 'token'; + }, + isAuthorized: secret => secret === 'wizard', +}; From 9718940d8689121fc519ad74146430544561992f Mon Sep 17 00:00:00 2001 From: Czysty Date: Tue, 20 Feb 2018 22:10:50 +0100 Subject: [PATCH 02/11] Add tests for genMockFromModule --- .../__tests__/genMockFromModule.test.js | 16 ++++++++ examples/automatic_mocks/utilsMocked.js | 41 +++++++++++++++++++ 2 files changed, 57 insertions(+) create mode 100644 examples/automatic_mocks/__tests__/genMockFromModule.test.js create mode 100644 examples/automatic_mocks/utilsMocked.js diff --git a/examples/automatic_mocks/__tests__/genMockFromModule.test.js b/examples/automatic_mocks/__tests__/genMockFromModule.test.js new file mode 100644 index 000000000000..0a66ee34196e --- /dev/null +++ b/examples/automatic_mocks/__tests__/genMockFromModule.test.js @@ -0,0 +1,16 @@ +// Copyright 2004-present Facebook. All Rights Reserved. + +//we need to disable automocking because is enabled on config in this example +jest.disableAutomock(); + +import utils from '../utilsMocked'; + +test('if exist additional implementation', () => { + console.log(utils.isAuthorized.getMockImplementation.getMockImplementation); + // expect(utils.authorized('wizzard')).toBeTruthy(); +}); + +test('mocked implementation', () => { + expect(utils.authorize._isMockFunction).toBe(true); + expect(utils.isAuthorized._isMockFunction).toBe(true); +}); diff --git a/examples/automatic_mocks/utilsMocked.js b/examples/automatic_mocks/utilsMocked.js new file mode 100644 index 000000000000..02ff323310e3 --- /dev/null +++ b/examples/automatic_mocks/utilsMocked.js @@ -0,0 +1,41 @@ +// Copyright 2004-present Facebook. All Rights Reserved. + +const utils = jest.genMockFromModule('./utils'); +// All implementation from original urils module is now mocked + +utils.isAuthorized = secret => { + console.log(123123123123); + return secret === 'not_wizzard'; +}; + +module.exports = utils; + +// +// const fs = jest.genMockFromModule('fs'); +// +// // This is a custom function that our tests can use during setup to specify +// // what the files on the "mock" filesystem should look like when any of the +// // `fs` APIs are used. +// let mockFiles = Object.create(null); +// function __setMockFiles(newMockFiles) { +// mockFiles = Object.create(null); +// for (const file in newMockFiles) { +// const dir = path.dirname(file); +// +// if (!mockFiles[dir]) { +// mockFiles[dir] = []; +// } +// mockFiles[dir].push(path.basename(file)); +// } +// } +// +// // A custom version of `readdirSync` that reads from the special mocked out +// // file list set via __setMockFiles +// function readdirSync(directoryPath) { +// return mockFiles[directoryPath] || []; +// } +// +// fs.__setMockFiles = __setMockFiles; +// fs.readdirSync = readdirSync; +// +// module.exports = fs; From 186f55936380bfc111e4815ff57576d2bb4bfdb8 Mon Sep 17 00:00:00 2001 From: Czysty Date: Wed, 21 Feb 2018 09:23:41 +0100 Subject: [PATCH 03/11] Add test for enableAutomock --- docs/Configuration.md | 3 +- docs/JestObjectAPI.md | 29 +++++++++++++++-- .../__tests__/automock.test.js | 4 +-- .../__tests__/disableAutomocking.test.js | 2 +- .../__tests__/enabledAutomocking.test.js | 14 +++++++++ examples/automatic_mocks/utilsMocked.js | 31 +------------------ 6 files changed, 47 insertions(+), 36 deletions(-) create mode 100644 examples/automatic_mocks/__tests__/enabledAutomocking.test.js diff --git a/docs/Configuration.md b/docs/Configuration.md index ff693f56c155..c33c8098f4b3 100644 --- a/docs/Configuration.md +++ b/docs/Configuration.md @@ -71,10 +71,11 @@ export default { }; ``` -Then the imported module in test have the same shape as the original, but +Then the imported module have the same shape as the original, but implementations are replaced also will return `jest` API for each element. ```js +//__tests__/automocking.test.js import utils from '../utils'; test('if utils mocked automatically', () => { diff --git a/docs/JestObjectAPI.md b/docs/JestObjectAPI.md index ba4671fcd53e..d34e7771f010 100644 --- a/docs/JestObjectAPI.md +++ b/docs/JestObjectAPI.md @@ -107,7 +107,32 @@ Enables automatic mocking in the module loader. Returns the `jest` object for chaining. -> see [`jest.disableAutomock()`](#jestdisableautomock) +> See `automock` section of [configuration](Configuration.md) for more +> information + +Example: + +````js +// utils.js +export default { + authorize: () => { + // implementation + return 'token'; + }, + isAuthorized: secret => secret === 'wizard', +}; + +// __tests__/disableAutomocking.js +jest.enableAutomock(); + +import utils from '../utils'; + +test('original implementation', () => { + // now we have the mocked implementation, + expect(utils.authorize._isMockFunction).toBeTruthy(); + expect(utils.isAuthorized._isMockFunction).toBeTruthy(); +}); +`` _Note: this method was previously called `autoMockOn`. When using `babel-jest`, calls to `enableAutomock` will automatically be hoisted to the top of the code @@ -126,7 +151,7 @@ expect(mockFn).toHaveBeenCalled(); // With a mock implementation: const returnsTrue = jest.fn(() => true); console.log(returnsTrue()); // true; -``` +```` ### `jest.isMockFunction(fn)` diff --git a/examples/automatic_mocks/__tests__/automock.test.js b/examples/automatic_mocks/__tests__/automock.test.js index d18adc477c45..270f977f2264 100644 --- a/examples/automatic_mocks/__tests__/automock.test.js +++ b/examples/automatic_mocks/__tests__/automock.test.js @@ -2,12 +2,12 @@ import utils from '../utils'; -test('if utils mocked', () => { +test('if utils are mocked', () => { expect(utils.authorize._isMockFunction).toBeTruthy(); expect(utils.isAuthorized._isMockFunction).toBeTruthy(); }); -test('replace implementation', () => { +test('mocked implementation', () => { utils.authorize.mockReturnValue('mocked_token'); utils.isAuthorized.mockReturnValue(true); diff --git a/examples/automatic_mocks/__tests__/disableAutomocking.test.js b/examples/automatic_mocks/__tests__/disableAutomocking.test.js index d92333a99638..2bad7f57f462 100644 --- a/examples/automatic_mocks/__tests__/disableAutomocking.test.js +++ b/examples/automatic_mocks/__tests__/disableAutomocking.test.js @@ -4,6 +4,6 @@ jest.disableAutomock(); import utils from '../utils'; -test('replace implementation', () => { +test('original implementation', () => { expect(utils.authorize()).toBe('token'); }); diff --git a/examples/automatic_mocks/__tests__/enabledAutomocking.test.js b/examples/automatic_mocks/__tests__/enabledAutomocking.test.js new file mode 100644 index 000000000000..fe06f5b683a1 --- /dev/null +++ b/examples/automatic_mocks/__tests__/enabledAutomocking.test.js @@ -0,0 +1,14 @@ +// Copyright 2004-present Facebook. All Rights Reserved. + +// we need to disable automocking becouse of jest configuration in this example +jest.disableAutomock(); + +// Nowe we enabling automocking +jest.enableAutomock(); + +import utils from '../utils'; + +test('mocked implementation', () => { + expect(utils.authorize._isMockFunction).toBeTruthy(); + expect(utils.isAuthorized._isMockFunction).toBeTruthy(); +}); diff --git a/examples/automatic_mocks/utilsMocked.js b/examples/automatic_mocks/utilsMocked.js index 02ff323310e3..519e19b69770 100644 --- a/examples/automatic_mocks/utilsMocked.js +++ b/examples/automatic_mocks/utilsMocked.js @@ -8,34 +8,5 @@ utils.isAuthorized = secret => { return secret === 'not_wizzard'; }; +// ?BUG?: export default not working correctly.. module.exports = utils; - -// -// const fs = jest.genMockFromModule('fs'); -// -// // This is a custom function that our tests can use during setup to specify -// // what the files on the "mock" filesystem should look like when any of the -// // `fs` APIs are used. -// let mockFiles = Object.create(null); -// function __setMockFiles(newMockFiles) { -// mockFiles = Object.create(null); -// for (const file in newMockFiles) { -// const dir = path.dirname(file); -// -// if (!mockFiles[dir]) { -// mockFiles[dir] = []; -// } -// mockFiles[dir].push(path.basename(file)); -// } -// } -// -// // A custom version of `readdirSync` that reads from the special mocked out -// // file list set via __setMockFiles -// function readdirSync(directoryPath) { -// return mockFiles[directoryPath] || []; -// } -// -// fs.__setMockFiles = __setMockFiles; -// fs.readdirSync = readdirSync; -// -// module.exports = fs; From 42c02dcf1189aa4b47a934b624578710a71f9ed1 Mon Sep 17 00:00:00 2001 From: Czysty Date: Wed, 21 Feb 2018 10:07:13 +0100 Subject: [PATCH 04/11] Update genMockFromModule section --- docs/Configuration.md | 16 +++++------ docs/JestObjectAPI.md | 28 ++++++++++++++++++- .../__tests__/genMockFromModule.test.js | 20 ++++++------- examples/automatic_mocks/utilsMocked.js | 12 -------- 4 files changed, 45 insertions(+), 31 deletions(-) delete mode 100644 examples/automatic_mocks/utilsMocked.js diff --git a/docs/Configuration.md b/docs/Configuration.md index c33c8098f4b3..6cf6695c78ce 100644 --- a/docs/Configuration.md +++ b/docs/Configuration.md @@ -54,9 +54,9 @@ configuration power. Default: `false` -This option tell to jest that all imported modules in your tests should be me -mocked, it means that the module will have a replacement implementation, but the -shape will be the same as original module. +This option tells Jest that all imported modules in your tests should be mocked +automatically. All modules used in your tests will have a replacement +implementation, keeping the API surface. Example: @@ -71,17 +71,17 @@ export default { }; ``` -Then the imported module have the same shape as the original, but -implementations are replaced also will return `jest` API for each element. - ```js //__tests__/automocking.test.js import utils from '../utils'; test('if utils mocked automatically', () => { - expect(utils.authorize._isMockFunction).toBeTruthy(); - expect(utils.isAuthorized._isMockFunction).toBeTruthy(); + // Public methods of `utils` are now mock functions + expect(utils.authorize.mock).toBeTruthy(); + expect(utils.isAuthorized.mock).toBeTruthy(); + // You can provide them with your own implementation + // or just pass the expected return value utils.authorize.mockReturnValue('mocked_token'); utils.isAuthorized.mockReturnValue(true); diff --git a/docs/JestObjectAPI.md b/docs/JestObjectAPI.md index d34e7771f010..3c44a1615660 100644 --- a/docs/JestObjectAPI.md +++ b/docs/JestObjectAPI.md @@ -58,7 +58,7 @@ each module (rather than a mocked version). Jest configuration: ```json -"automocking": true +"automock": true ``` Example: @@ -165,6 +165,32 @@ mocked version of the module for you. This is useful when you want to create a [manual mock](ManualMocks.md) that extends the automatic mock's behavior. +Example: + +```js +// utils.js +export default { + authorize: () => { + // implementation + return 'token'; + }, + isAuthorized: secret => secret === 'wizard', +}; +``` + +```js +// __tests__/genMockFromModule.test.js +import utils from '../utils'; + +const utils = jest.genMockFromModule('../utils').default; +utils.isAuthorized = jest.fn(secret => secret === 'not wizard'); + +test('implementation created by jest.genMockFromModule', () => { + expect(utils.authorize.mock).toBeTruthy(); + expect(utils.isAuthorized('not wizard')).toEqual(true); +}); +``` + ### `jest.mock(moduleName, factory, options)` Mocks a module with an auto-mocked version when it is being required. `factory` diff --git a/examples/automatic_mocks/__tests__/genMockFromModule.test.js b/examples/automatic_mocks/__tests__/genMockFromModule.test.js index 0a66ee34196e..274e1b8d5ca8 100644 --- a/examples/automatic_mocks/__tests__/genMockFromModule.test.js +++ b/examples/automatic_mocks/__tests__/genMockFromModule.test.js @@ -1,16 +1,16 @@ // Copyright 2004-present Facebook. All Rights Reserved. -//we need to disable automocking because is enabled on config in this example -jest.disableAutomock(); +import utils from '../utils'; -import utils from '../utilsMocked'; - -test('if exist additional implementation', () => { - console.log(utils.isAuthorized.getMockImplementation.getMockImplementation); - // expect(utils.authorized('wizzard')).toBeTruthy(); +test('implementation created by automock', () => { + expect(utils.authorize('wizzard')).toBeUndefined(); + expect(utils.isAuthorized()).toBeUndefined(); }); -test('mocked implementation', () => { - expect(utils.authorize._isMockFunction).toBe(true); - expect(utils.isAuthorized._isMockFunction).toBe(true); +test('implementation created by jest.genMockFromModule', () => { + const utils = jest.genMockFromModule('../utils').default; + utils.isAuthorized = jest.fn(secret => secret === 'not wizard'); + + expect(utils.authorize.mock).toBeTruthy(); + expect(utils.isAuthorized('not wizard')).toEqual(true); }); diff --git a/examples/automatic_mocks/utilsMocked.js b/examples/automatic_mocks/utilsMocked.js deleted file mode 100644 index 519e19b69770..000000000000 --- a/examples/automatic_mocks/utilsMocked.js +++ /dev/null @@ -1,12 +0,0 @@ -// Copyright 2004-present Facebook. All Rights Reserved. - -const utils = jest.genMockFromModule('./utils'); -// All implementation from original urils module is now mocked - -utils.isAuthorized = secret => { - console.log(123123123123); - return secret === 'not_wizzard'; -}; - -// ?BUG?: export default not working correctly.. -module.exports = utils; From c02db18f84a0d551a240c951ffab1b1cdbe76272 Mon Sep 17 00:00:00 2001 From: Czysty Date: Wed, 21 Feb 2018 10:26:52 +0100 Subject: [PATCH 05/11] Split examples --- docs/JestObjectAPI.md | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/docs/JestObjectAPI.md b/docs/JestObjectAPI.md index 3c44a1615660..d9564cf89f02 100644 --- a/docs/JestObjectAPI.md +++ b/docs/JestObjectAPI.md @@ -71,7 +71,9 @@ export default { return 'token'; }, }; +``` +```js // __tests__/disableAutomocking.js jest.disableAutomock(); @@ -112,7 +114,7 @@ Returns the `jest` object for chaining. Example: -````js +```js // utils.js export default { authorize: () => { @@ -121,7 +123,9 @@ export default { }, isAuthorized: secret => secret === 'wizard', }; +``` +```js // __tests__/disableAutomocking.js jest.enableAutomock(); @@ -132,7 +136,7 @@ test('original implementation', () => { expect(utils.authorize._isMockFunction).toBeTruthy(); expect(utils.isAuthorized._isMockFunction).toBeTruthy(); }); -`` +``` _Note: this method was previously called `autoMockOn`. When using `babel-jest`, calls to `enableAutomock` will automatically be hoisted to the top of the code @@ -151,7 +155,7 @@ expect(mockFn).toHaveBeenCalled(); // With a mock implementation: const returnsTrue = jest.fn(() => true); console.log(returnsTrue()); // true; -```` +``` ### `jest.isMockFunction(fn)` From a9026ca22a8ff207d09f3b272477bc77a9832e6d Mon Sep 17 00:00:00 2001 From: Czysty Date: Wed, 21 Feb 2018 10:30:50 +0100 Subject: [PATCH 06/11] Update tests in examples --- .../automatic_mocks/__tests__/automock.test.js | 4 ++-- .../__tests__/enabledAutomocking.test.js | 14 -------------- 2 files changed, 2 insertions(+), 16 deletions(-) delete mode 100644 examples/automatic_mocks/__tests__/enabledAutomocking.test.js diff --git a/examples/automatic_mocks/__tests__/automock.test.js b/examples/automatic_mocks/__tests__/automock.test.js index 270f977f2264..fe12a881cb10 100644 --- a/examples/automatic_mocks/__tests__/automock.test.js +++ b/examples/automatic_mocks/__tests__/automock.test.js @@ -3,8 +3,8 @@ import utils from '../utils'; test('if utils are mocked', () => { - expect(utils.authorize._isMockFunction).toBeTruthy(); - expect(utils.isAuthorized._isMockFunction).toBeTruthy(); + expect(utils.authorize.mock).toBeTruthy(); + expect(utils.isAuthorized.mock).toBeTruthy(); }); test('mocked implementation', () => { diff --git a/examples/automatic_mocks/__tests__/enabledAutomocking.test.js b/examples/automatic_mocks/__tests__/enabledAutomocking.test.js deleted file mode 100644 index fe06f5b683a1..000000000000 --- a/examples/automatic_mocks/__tests__/enabledAutomocking.test.js +++ /dev/null @@ -1,14 +0,0 @@ -// Copyright 2004-present Facebook. All Rights Reserved. - -// we need to disable automocking becouse of jest configuration in this example -jest.disableAutomock(); - -// Nowe we enabling automocking -jest.enableAutomock(); - -import utils from '../utils'; - -test('mocked implementation', () => { - expect(utils.authorize._isMockFunction).toBeTruthy(); - expect(utils.isAuthorized._isMockFunction).toBeTruthy(); -}); From f9b244234b62a723069c3fd32f3ee65cbabd8ea5 Mon Sep 17 00:00:00 2001 From: Czysty Date: Wed, 21 Feb 2018 12:52:56 +0100 Subject: [PATCH 07/11] Request changes --- docs/JestObjectAPI.md | 14 ++++++-------- .../__tests__/disableAutomocking.test.js | 4 ++-- 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/docs/JestObjectAPI.md b/docs/JestObjectAPI.md index d9564cf89f02..21832a090bf2 100644 --- a/docs/JestObjectAPI.md +++ b/docs/JestObjectAPI.md @@ -49,8 +49,8 @@ will be cleared and will never have the opportunity to execute in the future. Disables automatic mocking in the module loader. -> See `automock` section of [configuration](Configuration.md) for more -> information +> See `automock` section of [configuration](Configuration.md#automock-boolean) +> for more information After this method is called, all `require()`s will return the real versions of each module (rather than a mocked version). @@ -75,10 +75,10 @@ export default { ```js // __tests__/disableAutomocking.js -jest.disableAutomock(); - import utils from '../utils'; +jest.disableAutomock(); + test('original implementation', () => { // now we have the original implementation, // even if we set the automocking in a jest configuration @@ -109,8 +109,8 @@ Enables automatic mocking in the module loader. Returns the `jest` object for chaining. -> See `automock` section of [configuration](Configuration.md) for more -> information +> See `automock` section of [configuration](Configuration.md#automock-boolean) +> for more information Example: @@ -184,8 +184,6 @@ export default { ```js // __tests__/genMockFromModule.test.js -import utils from '../utils'; - const utils = jest.genMockFromModule('../utils').default; utils.isAuthorized = jest.fn(secret => secret === 'not wizard'); diff --git a/examples/automatic_mocks/__tests__/disableAutomocking.test.js b/examples/automatic_mocks/__tests__/disableAutomocking.test.js index 2bad7f57f462..b7e5b77f93ad 100644 --- a/examples/automatic_mocks/__tests__/disableAutomocking.test.js +++ b/examples/automatic_mocks/__tests__/disableAutomocking.test.js @@ -1,9 +1,9 @@ // Copyright 2004-present Facebook. All Rights Reserved. -jest.disableAutomock(); - import utils from '../utils'; +jest.disableAutomock(); + test('original implementation', () => { expect(utils.authorize()).toBe('token'); }); From e81f1b38999c487f73da2f1b4c27c96389fb66ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Pierzcha=C5=82a?= Date: Wed, 21 Feb 2018 13:02:40 +0100 Subject: [PATCH 08/11] Remove automock guidance --- docs/Configuration.md | 4 ---- 1 file changed, 4 deletions(-) diff --git a/docs/Configuration.md b/docs/Configuration.md index 6cf6695c78ce..bcdc685496f5 100644 --- a/docs/Configuration.md +++ b/docs/Configuration.md @@ -90,10 +90,6 @@ test('if utils mocked automatically', () => { }); ``` -If you are introducing Jest to a large organization with an existing codebase -but few tests, enabling this option can be helpful to introduce unit tests -gradually. Modules can be explicitly auto-mocked using `jest.mock(moduleName)`. - _Note: Core modules, like `fs`, are not mocked by default. They can be mocked explicitly, like `jest.mock('fs')`._ From def143e1910a7e761498de2e1cb279ab76942fe5 Mon Sep 17 00:00:00 2001 From: Czysty Date: Wed, 21 Feb 2018 13:39:42 +0100 Subject: [PATCH 09/11] Remove unnecessary comments --- docs/Configuration.md | 1 - docs/JestObjectAPI.md | 3 --- examples/automatic_mocks/utils.js | 1 - 3 files changed, 5 deletions(-) diff --git a/docs/Configuration.md b/docs/Configuration.md index 6cf6695c78ce..42a2a18acff9 100644 --- a/docs/Configuration.md +++ b/docs/Configuration.md @@ -64,7 +64,6 @@ Example: // utils.js export default { authorize: () => { - // implementation return 'token'; }, isAuthorized: secret => secret === 'wizard', diff --git a/docs/JestObjectAPI.md b/docs/JestObjectAPI.md index 21832a090bf2..3169bd70244f 100644 --- a/docs/JestObjectAPI.md +++ b/docs/JestObjectAPI.md @@ -67,7 +67,6 @@ Example: // utils.js export default { authorize: () => { - // implementation return 'token'; }, }; @@ -118,7 +117,6 @@ Example: // utils.js export default { authorize: () => { - // implementation return 'token'; }, isAuthorized: secret => secret === 'wizard', @@ -175,7 +173,6 @@ Example: // utils.js export default { authorize: () => { - // implementation return 'token'; }, isAuthorized: secret => secret === 'wizard', diff --git a/examples/automatic_mocks/utils.js b/examples/automatic_mocks/utils.js index 7f8f014d1018..6756a3fd32e0 100644 --- a/examples/automatic_mocks/utils.js +++ b/examples/automatic_mocks/utils.js @@ -2,7 +2,6 @@ export default { authorize: () => { - // implementation return 'token'; }, isAuthorized: secret => secret === 'wizard', From 904f4495e3ae438fdd8653e0a34bc150360985a0 Mon Sep 17 00:00:00 2001 From: Czysty Date: Wed, 21 Feb 2018 14:18:52 +0100 Subject: [PATCH 10/11] Update changelog --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7507d751ee0a..1248cc466492 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ### Fixes +* `[docs]` Update automatic mocks documentation + ([#5630](https://github.com/facebook/jest/pull/5630)) * `[expect]` Allow matching of Errors against plain objects ([#5611](https://github.com/facebook/jest/pull/5611)) * `[jest-cli]` Don't skip matchers for exact files From 91ea3b182091710a8bb75d7be0918d512b36472f Mon Sep 17 00:00:00 2001 From: Czysty Date: Wed, 21 Feb 2018 15:09:06 +0100 Subject: [PATCH 11/11] Add chore section to changelog --- CHANGELOG.md | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1248cc466492..c6ac3ed91738 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,8 +2,6 @@ ### Fixes -* `[docs]` Update automatic mocks documentation - ([#5630](https://github.com/facebook/jest/pull/5630)) * `[expect]` Allow matching of Errors against plain objects ([#5611](https://github.com/facebook/jest/pull/5611)) * `[jest-cli]` Don't skip matchers for exact files @@ -31,6 +29,11 @@ * `[docs]` Add a documentation note for project `displayName` configuration ([#5600](https://github.com/facebook/jest/pull/5600)) +# Chore & Maintenance + +* `[docs]` Update automatic mocks documentation + ([#5630](https://github.com/facebook/jest/pull/5630)) + ## jest 22.3.0 ### Fixes