You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* 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
Copy file name to clipboardexpand all lines: docs/JestObjectAPI.md
+93
Original file line number
Diff line number
Diff line change
@@ -119,6 +119,99 @@ test('implementation created by jest.genMockFromModule', () => {
119
119
});
120
120
```
121
121
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
+
constexample=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.
Copy file name to clipboardexpand all lines: website/versioned_docs/version-22.x/JestObjectAPI.md
+93
Original file line number
Diff line number
Diff line change
@@ -173,6 +173,99 @@ test('implementation created by jest.genMockFromModule', () => {
173
173
});
174
174
```
175
175
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
+
constexample=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.
Copy file name to clipboardexpand all lines: website/versioned_docs/version-23.x/JestObjectAPI.md
+93
Original file line number
Diff line number
Diff line change
@@ -174,6 +174,99 @@ test('implementation created by jest.genMockFromModule', () => {
174
174
});
175
175
```
176
176
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
+
constexample=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.
0 commit comments