Skip to content

Commit 90c6002

Browse files
macknessSimenB
authored andcommitted
Add documentation related to auto-mocking (#8099)
* add tests and documentation related to auto mocking * add mock function language and link to the mock function docs * fix string typo * adjust language for string description * line spacing and array property example * update versioned docs and changelog * fix following typo * update language to consistently use present tense / update versioned docs * update function names in test to match example code * update wording of primitive description * update wording of primitive description in code comment * move auto-mocking changelog line under master heading * move auto-mocking changelog line under master heading
1 parent 76782b8 commit 90c6002

File tree

6 files changed

+420
-0
lines changed

6 files changed

+420
-0
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
## master
22

3+
- `[*]` Add documentation and tests related to auto-mocking ([#8086](https://github.com/facebook/jest/pull/8099))
4+
35
### Features
46

57
### Fixes

docs/JestObjectAPI.md

+93
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,99 @@ test('implementation created by jest.genMockFromModule', () => {
119119
});
120120
```
121121

122+
This is how `genMockFromModule` will mock the following data types:
123+
124+
#### `Function`
125+
126+
Creates a new [mock function](https://jestjs.io/docs/en/mock-functions.html). The new function has no formal parameters and when called will return `undefined`. This functionality also applies to `async` functions.
127+
128+
#### `Class`
129+
130+
Creates new class. The interface of the original class is maintained, all of the class member functions and properties will be mocked.
131+
132+
#### `Object`
133+
134+
Creates a new deeply cloned object. The object keys are maintained and their values are mocked.
135+
136+
#### `Array`
137+
138+
Creates a new empty array, ignoring the original.
139+
140+
#### `Primitives`
141+
142+
Creates a new property with the same primitive value as the original property.
143+
144+
Example:
145+
146+
```
147+
// example.js
148+
module.exports = {
149+
function: function square(a, b) {
150+
return a * b;
151+
},
152+
asyncFunction: async function asyncSquare(a, b) {
153+
const result = await a * b;
154+
return result;
155+
},
156+
class: new class Bar {
157+
constructor() {
158+
this.array = [1, 2, 3];
159+
}
160+
foo() {}
161+
},
162+
object: {
163+
baz: 'foo',
164+
bar: {
165+
fiz: 1,
166+
buzz: [1, 2, 3],
167+
},
168+
},
169+
array: [1, 2, 3],
170+
number: 123,
171+
string: 'baz',
172+
boolean: true,
173+
symbol: Symbol.for('a.b.c'),
174+
};
175+
```
176+
177+
```js
178+
// __tests__/example.test.js
179+
const example = jest.genMockFromModule('./example');
180+
181+
test('should run example code', () => {
182+
// creates a new mocked function with no formal arguments.
183+
expect(example.function.name).toEqual('square');
184+
expect(example.function.length).toEqual(0);
185+
186+
// async functions get the same treatment as standard synchronous functions.
187+
expect(example.asyncFunction.name).toEqual('asyncSquare');
188+
expect(example.asyncFunction.length).toEqual(0);
189+
190+
// creates a new class with the same interface, member functions and properties are mocked.
191+
expect(example.class.constructor.name).toEqual('Bar');
192+
expect(example.class.foo.name).toEqual('foo');
193+
expect(example.class.array.length).toEqual(0);
194+
195+
// creates a deeply cloned version of the original object.
196+
expect(example.object).toEqual({
197+
baz: 'foo',
198+
bar: {
199+
fiz: 1,
200+
buzz: [],
201+
},
202+
});
203+
204+
// creates a new empty array, ignoring the original array.
205+
expect(example.array.length).toEqual(0);
206+
207+
// creates a new property with the same primitive value as the original property.
208+
expect(example.number).toEqual(123);
209+
expect(example.string).toEqual('baz');
210+
expect(example.boolean).toEqual(true);
211+
expect(example.symbol).toEqual(Symbol.for('a.b.c'));
212+
});
213+
```
214+
122215
### `jest.mock(moduleName, factory, options)`
123216

124217
Mocks a module with an auto-mocked version when it is being required. `factory` and `options` are optional. For example:

packages/jest-mock/src/__tests__/index.test.ts

+46
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ describe('moduleMocker', () => {
3434
expect(moduleMocker.getMetadata('banana').value).toEqual('banana');
3535
expect(moduleMocker.getMetadata(27).value).toEqual(27);
3636
expect(moduleMocker.getMetadata(false).value).toEqual(false);
37+
expect(moduleMocker.getMetadata(Infinity).value).toEqual(Infinity);
3738
});
3839

3940
it('does not retrieve metadata for arrays', () => {
@@ -57,6 +58,51 @@ describe('moduleMocker', () => {
5758
expect(metadata.members).toBeUndefined();
5859
expect(metadata.type).toEqual('null');
5960
});
61+
62+
it('retrieves metadata for ES6 classes', () => {
63+
class ClassFooMock {
64+
bar() {}
65+
}
66+
const fooInstance = new ClassFooMock();
67+
const metadata = moduleMocker.getMetadata(fooInstance);
68+
expect(metadata.type).toEqual('object');
69+
expect(metadata.members.constructor.name).toEqual('ClassFooMock');
70+
});
71+
72+
it('retrieves synchronous function metadata', () => {
73+
function functionFooMock() {}
74+
const metadata = moduleMocker.getMetadata(functionFooMock);
75+
expect(metadata.type).toEqual('function');
76+
expect(metadata.name).toEqual('functionFooMock');
77+
});
78+
79+
it('retrieves asynchronous function metadata', () => {
80+
async function asyncFunctionFooMock() {}
81+
const metadata = moduleMocker.getMetadata(asyncFunctionFooMock);
82+
expect(metadata.type).toEqual('function');
83+
expect(metadata.name).toEqual('asyncFunctionFooMock');
84+
});
85+
86+
it("retrieves metadata for object literals and it's members", () => {
87+
const metadata = moduleMocker.getMetadata({
88+
bar: 'two',
89+
foo: 1,
90+
});
91+
expect(metadata.type).toEqual('object');
92+
expect(metadata.members.bar.value).toEqual('two');
93+
expect(metadata.members.bar.type).toEqual('constant');
94+
expect(metadata.members.foo.value).toEqual(1);
95+
expect(metadata.members.foo.type).toEqual('constant');
96+
});
97+
98+
it('retrieves Date object metadata', () => {
99+
const metadata = moduleMocker.getMetadata(Date);
100+
expect(metadata.type).toEqual('function');
101+
expect(metadata.name).toEqual('Date');
102+
expect(metadata.members.now.name).toEqual('now');
103+
expect(metadata.members.parse.name).toEqual('parse');
104+
expect(metadata.members.UTC.name).toEqual('UTC');
105+
});
60106
});
61107

62108
describe('generateFromMetadata', () => {

website/versioned_docs/version-22.x/JestObjectAPI.md

+93
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,99 @@ test('implementation created by jest.genMockFromModule', () => {
173173
});
174174
```
175175

176+
This is how `genMockFromModule` will mock the following data types:
177+
178+
#### `Function`
179+
180+
Creates a new [mock function](https://jestjs.io/docs/en/mock-functions.html). The new function has no formal parameters and when called will return `undefined`. This functionality also applies to `async` functions.
181+
182+
#### `Class`
183+
184+
Creates new class. The interface of the original class is maintained, all of the class member functions and properties will be mocked.
185+
186+
#### `Object`
187+
188+
Creates a new deeply cloned object. The object keys are maintained and their values are mocked.
189+
190+
#### `Array`
191+
192+
Creates a new empty array, ignoring the original.
193+
194+
#### `Primitives`
195+
196+
Creates a new property with the same primitive value as the original property.
197+
198+
Example:
199+
200+
```
201+
// example.js
202+
module.exports = {
203+
function: function square(a, b) {
204+
return a * b;
205+
},
206+
asyncFunction: async function asyncSquare(a, b) {
207+
const result = await a * b;
208+
return result;
209+
},
210+
class: new class Bar {
211+
constructor() {
212+
this.array = [1, 2, 3];
213+
}
214+
foo() {}
215+
},
216+
object: {
217+
baz: 'foo',
218+
bar: {
219+
fiz: 1,
220+
buzz: [1, 2, 3],
221+
},
222+
},
223+
array: [1, 2, 3],
224+
number: 123,
225+
string: 'baz',
226+
boolean: true,
227+
symbol: Symbol.for('a.b.c'),
228+
};
229+
```
230+
231+
```js
232+
// __tests__/example.test.js
233+
const example = jest.genMockFromModule('./example');
234+
235+
test('should run example code', () => {
236+
// creates a new mocked function with no formal arguments.
237+
expect(example.function.name).toEqual('square');
238+
expect(example.function.length).toEqual(0);
239+
240+
// async functions get the same treatment as standard synchronous functions.
241+
expect(example.asyncFunction.name).toEqual('asyncSquare');
242+
expect(example.asyncFunction.length).toEqual(0);
243+
244+
// creates a new class with the same interface, member functions and properties are mocked.
245+
expect(example.class.constructor.name).toEqual('Bar');
246+
expect(example.class.foo.name).toEqual('foo');
247+
expect(example.class.array.length).toEqual(0);
248+
249+
// creates a deeply cloned version of the original object.
250+
expect(example.object).toEqual({
251+
baz: 'foo',
252+
bar: {
253+
fiz: 1,
254+
buzz: [],
255+
},
256+
});
257+
258+
// creates a new empty array, ignoring the original array.
259+
expect(example.array.length).toEqual(0);
260+
261+
// creates a new property with the same primitive value as the original property.
262+
expect(example.number).toEqual(123);
263+
expect(example.string).toEqual('baz');
264+
expect(example.boolean).toEqual(true);
265+
expect(example.symbol).toEqual(Symbol.for('a.b.c'));
266+
});
267+
```
268+
176269
### `jest.mock(moduleName, factory, options)`
177270

178271
Mocks a module with an auto-mocked version when it is being required. `factory` and `options` are optional. For example:

website/versioned_docs/version-23.x/JestObjectAPI.md

+93
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,99 @@ test('implementation created by jest.genMockFromModule', () => {
174174
});
175175
```
176176

177+
This is how `genMockFromModule` will mock the following data types:
178+
179+
#### `Function`
180+
181+
Creates a new [mock function](https://jestjs.io/docs/en/mock-functions.html). The new function has no formal parameters and when called will return `undefined`. This functionality also applies to `async` functions.
182+
183+
#### `Class`
184+
185+
Creates new class. The interface of the original class is maintained, all of the class member functions and properties will be mocked.
186+
187+
#### `Object`
188+
189+
Creates a new deeply cloned object. The object keys are maintained and their values are mocked.
190+
191+
#### `Array`
192+
193+
Creates a new empty array, ignoring the original.
194+
195+
#### `Primitives`
196+
197+
Creates a new property with the same primitive value as the original property.
198+
199+
Example:
200+
201+
```
202+
// example.js
203+
module.exports = {
204+
function: function square(a, b) {
205+
return a * b;
206+
},
207+
asyncFunction: async function asyncSquare(a, b) {
208+
const result = await a * b;
209+
return result;
210+
},
211+
class: new class Bar {
212+
constructor() {
213+
this.array = [1, 2, 3];
214+
}
215+
foo() {}
216+
},
217+
object: {
218+
baz: 'foo',
219+
bar: {
220+
fiz: 1,
221+
buzz: [1, 2, 3],
222+
},
223+
},
224+
array: [1, 2, 3],
225+
number: 123,
226+
string: 'baz',
227+
boolean: true,
228+
symbol: Symbol.for('a.b.c'),
229+
};
230+
```
231+
232+
```js
233+
// __tests__/example.test.js
234+
const example = jest.genMockFromModule('./example');
235+
236+
test('should run example code', () => {
237+
// creates a new mocked function with no formal arguments.
238+
expect(example.function.name).toEqual('square');
239+
expect(example.function.length).toEqual(0);
240+
241+
// async functions get the same treatment as standard synchronous functions.
242+
expect(example.asyncFunction.name).toEqual('asyncSquare');
243+
expect(example.asyncFunction.length).toEqual(0);
244+
245+
// creates a new class with the same interface, member functions and properties are mocked.
246+
expect(example.class.constructor.name).toEqual('Bar');
247+
expect(example.class.foo.name).toEqual('foo');
248+
expect(example.class.array.length).toEqual(0);
249+
250+
// creates a deeply cloned version of the original object.
251+
expect(example.object).toEqual({
252+
baz: 'foo',
253+
bar: {
254+
fiz: 1,
255+
buzz: [],
256+
},
257+
});
258+
259+
// creates a new empty array, ignoring the original array.
260+
expect(example.array.length).toEqual(0);
261+
262+
// creates a new property with the same primitive value as the original property.
263+
expect(example.number).toEqual(123);
264+
expect(example.string).toEqual('baz');
265+
expect(example.boolean).toEqual(true);
266+
expect(example.symbol).toEqual(Symbol.for('a.b.c'));
267+
});
268+
```
269+
177270
### `jest.mock(moduleName, factory, options)`
178271

179272
Mocks a module with an auto-mocked version when it is being required. `factory` and `options` are optional. For example:

0 commit comments

Comments
 (0)