Skip to content

Commit 3fedcfa

Browse files
committed
Rename getMockFn to fn in jest-mock.
1 parent fc96580 commit 3fedcfa

File tree

5 files changed

+27
-37
lines changed

5 files changed

+27
-37
lines changed

packages/jest-mock/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ environment with the given global object.
1212
Generates a mock based on the given metadata (Metadata for the mock in
1313
the schema returned by the getMetadata method of this module). Mocks treat
1414
functions specially, and all mock functions have additional members, described
15-
in the documentation for getMockFunction in this module.
15+
in the documentation for `fn` in this module.
1616

1717

1818
One important note: function prototypes are handled specially by this
@@ -72,7 +72,7 @@ For instance, this metadata blob:
7272

7373
defines an object with a slot named `self` that refers back to the object.
7474

75-
### `getMockFunction`
75+
### `fn`
7676

7777
Generates a stand-alone function with members that help drive unit tests or
7878
confirm expectations. Specifically, functions returned by this method have

packages/jest-mock/src/__tests__/jest-mock-test.js

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ describe('moduleMocker', () => {
171171

172172
describe('mocked functions', () => {
173173
it('tracks calls to mocks', () => {
174-
const fn = moduleMocker.getMockFunction();
174+
const fn = moduleMocker.fn();
175175
expect(fn.mock.calls).toEqual([]);
176176

177177
fn(1, 2, 3);
@@ -182,7 +182,7 @@ describe('moduleMocker', () => {
182182
});
183183

184184
it('tracks instances made by mocks', () => {
185-
const fn = moduleMocker.getMockFunction();
185+
const fn = moduleMocker.fn();
186186
expect(fn.mock.instances).toEqual([]);
187187

188188
const instance1 = new fn();
@@ -193,7 +193,7 @@ describe('moduleMocker', () => {
193193
});
194194

195195
it('supports clearing mock calls', () => {
196-
const fn = moduleMocker.getMockFunction();
196+
const fn = moduleMocker.fn();
197197
expect(fn.mock.calls).toEqual([]);
198198

199199
fn(1, 2, 3);
@@ -211,7 +211,7 @@ describe('moduleMocker', () => {
211211
});
212212

213213
it('supports clearing mocks', () => {
214-
const fn = moduleMocker.getMockFunction();
214+
const fn = moduleMocker.fn();
215215
expect(fn.mock.calls).toEqual([]);
216216

217217
fn(1, 2, 3);
@@ -225,7 +225,7 @@ describe('moduleMocker', () => {
225225
});
226226

227227
it('supports resetting mock return values', () => {
228-
const fn = moduleMocker.getMockFunction();
228+
const fn = moduleMocker.fn();
229229
fn.mockReturnValue('abcd');
230230

231231
const before = fn();
@@ -238,7 +238,7 @@ describe('moduleMocker', () => {
238238
});
239239

240240
it('supports resetting single use mock return values', () => {
241-
const fn = moduleMocker.getMockFunction();
241+
const fn = moduleMocker.fn();
242242
fn.mockReturnValueOnce('abcd');
243243

244244
fn.mockReset();
@@ -248,7 +248,7 @@ describe('moduleMocker', () => {
248248
});
249249

250250
it('supports resetting mock implementations', () => {
251-
const fn = moduleMocker.getMockFunction();
251+
const fn = moduleMocker.fn();
252252
fn.mockImplementation(() => 'abcd');
253253

254254
const before = fn();
@@ -261,7 +261,7 @@ describe('moduleMocker', () => {
261261
});
262262

263263
it('supports resetting single use mock implementations', () => {
264-
const fn = moduleMocker.getMockFunction();
264+
const fn = moduleMocker.fn();
265265
fn.mockImplementationOnce(() => 'abcd');
266266

267267
fn.mockReset();
@@ -271,12 +271,12 @@ describe('moduleMocker', () => {
271271
});
272272

273273
it('supports resetting all mocks', () => {
274-
const fn1 = moduleMocker.getMockFunction();
274+
const fn1 = moduleMocker.fn();
275275
fn1.mockImplementation(() => 'abcd');
276276
fn1(1, 2, 3);
277277
expect(fn1.mock.calls).toEqual([[1, 2, 3]]);
278278

279-
const fn2 = moduleMocker.getMockFunction();
279+
const fn2 = moduleMocker.fn();
280280
fn2.mockReturnValue('abcd');
281281
fn2('a', 'b', 'c');
282282
expect(fn2.mock.calls).toEqual([['a', 'b', 'c']]);
@@ -292,7 +292,7 @@ describe('moduleMocker', () => {
292292

293293
describe('getMockImplementation', () => {
294294
it('should mock calls to a mock function', () => {
295-
const mockFn = moduleMocker.getMockFunction();
295+
const mockFn = moduleMocker.fn();
296296

297297
mockFn.mockImplementation(() => {
298298
return 'Foo';
@@ -305,7 +305,7 @@ describe('moduleMocker', () => {
305305

306306
describe('mockImplementationOnce', () => {
307307
it('should mock single call to a mock function', () => {
308-
const mockFn = moduleMocker.getMockFunction();
308+
const mockFn = moduleMocker.fn();
309309

310310
mockFn.mockImplementationOnce(() => {
311311
return 'Foo';
@@ -319,7 +319,7 @@ describe('moduleMocker', () => {
319319
});
320320

321321
it('should fallback to default mock function when no specific mock is available', () => {
322-
const mockFn = moduleMocker.getMockFunction();
322+
const mockFn = moduleMocker.fn();
323323

324324
mockFn.mockImplementationOnce(() => {
325325
return 'Foo';
@@ -337,7 +337,7 @@ describe('moduleMocker', () => {
337337
});
338338

339339
it('should recognize a mocked function', () => {
340-
const mockFn = moduleMocker.getMockFunction();
340+
const mockFn = moduleMocker.fn();
341341

342342
expect(moduleMocker.isMockFunction(() => {})).toBe(false);
343343
expect(moduleMocker.isMockFunction(mockFn)).toBe(true);

packages/jest-mock/src/index.js

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -549,16 +549,12 @@ class ModuleMockerClass {
549549
return !!fn._isMockFunction;
550550
}
551551

552-
/**
553-
* @see README.md
554-
*/
555-
getMockFunction(): any {
556-
return this._makeComponent({type: 'function'});
557-
}
558-
559-
// Just a short-hand alias
560-
getMockFn(): any {
561-
return this.getMockFunction();
552+
fn(implementation?: any): any {
553+
const fn = this._makeComponent({type: 'function'});
554+
if (implementation) {
555+
fn.mockImplementation(implementation);
556+
}
557+
return fn;
562558
}
563559

564560
resetAllMocks() {

packages/jest-runtime/src/index.js

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -692,6 +692,7 @@ class Runtime {
692692
this.resetModules();
693693
return runtime;
694694
};
695+
const fn = this._moduleMocker.fn.bind(this._moduleMocker);
695696

696697
const runtime = {
697698
addMatchers:
@@ -706,18 +707,11 @@ class Runtime {
706707
doMock: mock,
707708
dontMock: unmock,
708709
enableAutomock,
709-
fn: (impl: ?Function) => {
710-
const fn = this._moduleMocker.getMockFunction();
711-
if (impl) {
712-
return fn.mockImplementation(impl);
713-
}
714-
return fn;
715-
},
716-
genMockFn: this._moduleMocker.getMockFunction.bind(this._moduleMocker),
710+
fn,
711+
genMockFn: fn,
717712
genMockFromModule:
718713
(moduleName: string) => this._generateMock(from, moduleName),
719-
genMockFunction:
720-
this._moduleMocker.getMockFunction.bind(this._moduleMocker),
714+
genMockFunction: fn,
721715
isMockFunction: this._moduleMocker.isMockFunction,
722716

723717
mock,

packages/jest-util/src/FakeTimers.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ class FakeTimers {
328328
}
329329

330330
_createMocks() {
331-
const fn = impl => this._moduleMocker.getMockFn().mockImplementation(impl);
331+
const fn = impl => this._moduleMocker.fn().mockImplementation(impl);
332332

333333
this._fakeTimerAPIs = {
334334
clearImmediate: fn(this._fakeClearImmediate.bind(this)),

0 commit comments

Comments
 (0)