-
-
Notifications
You must be signed in to change notification settings - Fork 6.6k
Add documentation related to auto-mocking #8099
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
Changes from 6 commits
746ea44
91b9427
708cf2c
1acd876
3ce58b7
3b92d72
48855e7
062b28c
1e80856
b24fb10
9df0f20
e050750
df8c6b8
b3c807f
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 |
|---|---|---|
|
|
@@ -119,6 +119,102 @@ test('implementation created by jest.genMockFromModule', () => { | |
| }); | ||
| ``` | ||
|
|
||
| This is how `genMockFromModule` will mock the follwing data types: | ||
pedrottimark marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| #### `Function` | ||
|
|
||
| A new [mock function](https://jestjs.io/docs/en/mock-functions.html) will be created. The new function will have no formal parameters and when called will return `undefined`. This functionality also applies to `async functions`. | ||
|
||
|
|
||
| #### `Class` | ||
|
|
||
| A new class will be created. The interface of the original class is maintained however all of the class member functions and properties will be mocked. | ||
|
|
||
| #### `Object` | ||
|
|
||
| Objects are deeply cloned. Their keys are maintained and their values are mocked. | ||
|
|
||
| #### `Array` | ||
|
|
||
| The original array is ignored and a new empty array is created. | ||
|
|
||
| #### `String` | ||
|
|
||
| A new copy of the original string is mocked. | ||
|
||
|
|
||
| #### `Number` | ||
|
|
||
| A new copy of the original number is mocked. | ||
|
|
||
| Example: | ||
|
|
||
| <!-- prettier-ignore --> | ||
|
||
| ```js | ||
| // example.js | ||
| module.exports = { | ||
| function: function foo(a, b) { | ||
| return a + b; | ||
| }, | ||
| asyncFunction: async function asyncFoo(a, b) { | ||
| const result = await a + b; | ||
| return result; | ||
| }, | ||
| class: new class Bar { | ||
| constructor() { | ||
| this.array = [1, 2, 3]; | ||
| } | ||
| foo() {} | ||
| }, | ||
| object: { | ||
| baz: 'foo', | ||
| bar: { | ||
| fiz: 1, | ||
| buzz: [1, 2, 3], | ||
| }, | ||
| }, | ||
| array: [1, 2, 3], | ||
| number: 123, | ||
| string: 'baz', | ||
| }; | ||
| ``` | ||
|
|
||
| ```js | ||
| // __tests__/example.test.js | ||
| const example = jest.genMockFromModule('./example'); | ||
|
|
||
| test('should run example code', () => { | ||
SimenB marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // a new mocked function with no formal arguments. | ||
| expect(example.function.name).toEqual('foo'); | ||
| expect(example.function.length).toEqual(0); | ||
|
|
||
| // async functions are treated just like standard synchronous functions. | ||
| expect(example.asyncFunction.name).toEqual('asyncFoo'); | ||
| expect(example.asyncFunction.length).toEqual(0); | ||
|
|
||
| // a new mocked class that maintains the original interface and mocks member functions and properties. | ||
| expect(example.class.constructor.name).toEqual('Bar'); | ||
| expect(example.class.foo.name).toEqual('foo'); | ||
| expect(example.class.array.length).toEqual(0); | ||
|
|
||
| // a deeply cloned object that maintains the original interface and mocks it's values. | ||
| expect(example.object).toEqual({ | ||
| baz: 'foo', | ||
| bar: { | ||
| fiz: 1, | ||
| buzz: [], | ||
| }, | ||
| }); | ||
|
|
||
| // the original array is ignored and a new emtpy array is mocked. | ||
| expect(example.array.length).toEqual(0); | ||
|
|
||
| // a new copy of the original number. | ||
| expect(example.number).toEqual(123); | ||
|
|
||
| // a new copy of the original string. | ||
| expect(example.string).toEqual('baz'); | ||
| }); | ||
| ``` | ||
|
|
||
| ### `jest.mock(moduleName, factory, options)` | ||
|
|
||
| Mocks a module with an auto-mocked version when it is being required. `factory` and `options` are optional. For example: | ||
|
|
||


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.
Can you move this up under master?
(Might have to merge in master or rebase first)
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.
Synced with upstream master and moved the changelog line under the master heading. Thanks for the feedback and help with this @SimenB !