Skip to content
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

Merged
merged 12 commits into from
Feb 21, 2018
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,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
Expand Down
38 changes: 34 additions & 4 deletions docs/Configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,40 @@ 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 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:

```js
// utils.js
export default {
authorize: () => {
return 'token';
},
isAuthorized: secret => secret === 'wizard',
};
```

```js
//__tests__/automocking.test.js
import utils from '../utils';

test('if utils mocked automatically', () => {
// 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);

expect(utils.authorize()).toBe('mocked_token');
expect(utils.isAuthorized('not_wizard')).toBeTruthy();
});
```

_Note: Core modules, like `fs`, are not mocked by default. They can be mocked
explicitly, like `jest.mock('fs')`._
Expand Down
84 changes: 84 additions & 0 deletions docs/JestObjectAPI.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,42 @@ 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#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).

Jest configuration:

```json
"automock": true
```

Example:

```js
// utils.js
export default {
authorize: () => {
return 'token';
},
};
```

```js
// __tests__/disableAutomocking.js
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
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
Expand All @@ -75,6 +108,34 @@ Enables automatic mocking in the module loader.

Returns the `jest` object for chaining.

> See `automock` section of [configuration](Configuration.md#automock-boolean)
> for more information

Example:

```js
// utils.js
export default {
authorize: () => {
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._
Expand Down Expand Up @@ -106,6 +167,29 @@ 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: () => {
return 'token';
},
isAuthorized: secret => secret === 'wizard',
};
```

```js
// __tests__/genMockFromModule.test.js
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`
Expand Down
3 changes: 3 additions & 0 deletions examples/automatic_mocks/.babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"presets": ["env"]
}
16 changes: 16 additions & 0 deletions examples/automatic_mocks/__tests__/automock.test.js
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();
});
9 changes: 9 additions & 0 deletions examples/automatic_mocks/__tests__/disableAutomocking.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Copyright 2004-present Facebook. All Rights Reserved.

import utils from '../utils';

jest.disableAutomock();

test('original implementation', () => {
expect(utils.authorize()).toBe('token');
});
16 changes: 16 additions & 0 deletions examples/automatic_mocks/__tests__/genMockFromModule.test.js
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);
});
12 changes: 12 additions & 0 deletions examples/automatic_mocks/package.json
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
}
}
8 changes: 8 additions & 0 deletions examples/automatic_mocks/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Copyright 2004-present Facebook. All Rights Reserved.

export default {
authorize: () => {
return 'token';
},
isAuthorized: secret => secret === 'wizard',
};