-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update documentation on automocking #5630
Changes from 6 commits
954bc79
9718940
186f559
42c02dc
c02db18
a9026ca
f9b2442
e81f1b3
def143e
ddbc781
904f449
91ea3b1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -49,9 +49,43 @@ 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you link to the exact place, e.g. |
||
> 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 | ||
"automock": true | ||
``` | ||
|
||
Example: | ||
|
||
```js | ||
// utils.js | ||
export default { | ||
authorize: () => { | ||
// implementation | ||
return 'token'; | ||
}, | ||
}; | ||
``` | ||
|
||
```js | ||
// __tests__/disableAutomocking.js | ||
jest.disableAutomock(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should be good to move this below import, |
||
|
||
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 +109,35 @@ Enables automatic mocking in the module loader. | |
|
||
Returns the `jest` object for chaining. | ||
|
||
> See `automock` section of [configuration](Configuration.md) for more | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. direct link |
||
> information | ||
|
||
Example: | ||
|
||
```js | ||
// utils.js | ||
export default { | ||
authorize: () => { | ||
// implementation | ||
return 'token'; | ||
}, | ||
isAuthorized: secret => secret === 'wizard', | ||
}; | ||
``` | ||
|
||
```js | ||
// __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 | ||
block. Use `autoMockOn` if you want to explicitly avoid this behavior._ | ||
|
@@ -106,6 +169,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'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please remove this, as it's not necessary and redefining it below will throw error. |
||
|
||
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` | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"presets": ["env"] | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// Copyright 2004-present Facebook. All Rights Reserved. | ||
|
||
import utils from '../utils'; | ||
|
||
test('if utils are mocked', () => { | ||
expect(utils.authorize.mock).toBeTruthy(); | ||
expect(utils.isAuthorized.mock).toBeTruthy(); | ||
}); | ||
|
||
test('mocked implementation', () => { | ||
utils.authorize.mockReturnValue('mocked_token'); | ||
utils.isAuthorized.mockReturnValue(true); | ||
|
||
expect(utils.authorize()).toBe('mocked_token'); | ||
expect(utils.isAuthorized('not_wizard')).toBeTruthy(); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// Copyright 2004-present Facebook. All Rights Reserved. | ||
|
||
jest.disableAutomock(); | ||
|
||
import utils from '../utils'; | ||
|
||
test('original implementation', () => { | ||
expect(utils.authorize()).toBe('token'); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// Copyright 2004-present Facebook. All Rights Reserved. | ||
|
||
import utils from '../utils'; | ||
|
||
test('implementation created by automock', () => { | ||
expect(utils.authorize('wizzard')).toBeUndefined(); | ||
expect(utils.isAuthorized()).toBeUndefined(); | ||
}); | ||
|
||
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); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"devDependencies": { | ||
"babel-preset-env": "*", | ||
"jest": "*" | ||
}, | ||
"scripts": { | ||
"test": "jest" | ||
}, | ||
"jest": { | ||
"automock": true | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// Copyright 2004-present Facebook. All Rights Reserved. | ||
|
||
export default { | ||
authorize: () => { | ||
// implementation | ||
return 'token'; | ||
}, | ||
isAuthorized: secret => secret === 'wizard', | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think that's a good recommendation now, as then switching to
"automock": false
will be painful. We should kill that paragraph imo.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cc @cpojer @SimenB
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am fine with rolling back this guidance. You are right.