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

Rename getMockFn to fn in jest-mock. #2533

Merged
merged 1 commit into from
Jan 8, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/jest-mock/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ environment with the given global object.
Generates a mock based on the given metadata (Metadata for the mock in
the schema returned by the getMetadata method of this module). Mocks treat
functions specially, and all mock functions have additional members, described
in the documentation for getMockFunction in this module.
in the documentation for `fn` in this module.


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

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

### `getMockFunction`
### `fn`

Generates a stand-alone function with members that help drive unit tests or
confirm expectations. Specifically, functions returned by this method have
Expand Down
28 changes: 14 additions & 14 deletions packages/jest-mock/src/__tests__/jest-mock-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ describe('moduleMocker', () => {

describe('mocked functions', () => {
it('tracks calls to mocks', () => {
const fn = moduleMocker.getMockFunction();
const fn = moduleMocker.fn();
expect(fn.mock.calls).toEqual([]);

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

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

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

it('supports clearing mock calls', () => {
const fn = moduleMocker.getMockFunction();
const fn = moduleMocker.fn();
expect(fn.mock.calls).toEqual([]);

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

it('supports clearing mocks', () => {
const fn = moduleMocker.getMockFunction();
const fn = moduleMocker.fn();
expect(fn.mock.calls).toEqual([]);

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

it('supports resetting mock return values', () => {
const fn = moduleMocker.getMockFunction();
const fn = moduleMocker.fn();
fn.mockReturnValue('abcd');

const before = fn();
Expand All @@ -238,7 +238,7 @@ describe('moduleMocker', () => {
});

it('supports resetting single use mock return values', () => {
const fn = moduleMocker.getMockFunction();
const fn = moduleMocker.fn();
fn.mockReturnValueOnce('abcd');

fn.mockReset();
Expand All @@ -248,7 +248,7 @@ describe('moduleMocker', () => {
});

it('supports resetting mock implementations', () => {
const fn = moduleMocker.getMockFunction();
const fn = moduleMocker.fn();
fn.mockImplementation(() => 'abcd');

const before = fn();
Expand All @@ -261,7 +261,7 @@ describe('moduleMocker', () => {
});

it('supports resetting single use mock implementations', () => {
const fn = moduleMocker.getMockFunction();
const fn = moduleMocker.fn();
fn.mockImplementationOnce(() => 'abcd');

fn.mockReset();
Expand All @@ -271,12 +271,12 @@ describe('moduleMocker', () => {
});

it('supports resetting all mocks', () => {
const fn1 = moduleMocker.getMockFunction();
const fn1 = moduleMocker.fn();
fn1.mockImplementation(() => 'abcd');
fn1(1, 2, 3);
expect(fn1.mock.calls).toEqual([[1, 2, 3]]);

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

describe('getMockImplementation', () => {
it('should mock calls to a mock function', () => {
const mockFn = moduleMocker.getMockFunction();
const mockFn = moduleMocker.fn();

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

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

mockFn.mockImplementationOnce(() => {
return 'Foo';
Expand All @@ -319,7 +319,7 @@ describe('moduleMocker', () => {
});

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

mockFn.mockImplementationOnce(() => {
return 'Foo';
Expand All @@ -337,7 +337,7 @@ describe('moduleMocker', () => {
});

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

expect(moduleMocker.isMockFunction(() => {})).toBe(false);
expect(moduleMocker.isMockFunction(mockFn)).toBe(true);
Expand Down
20 changes: 8 additions & 12 deletions packages/jest-mock/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,14 @@ class ModuleMockerClass {
return !!fn._isMockFunction;
}

fn(implementation?: any): any {
const fn = this._makeComponent({type: 'function'});
if (implementation) {
fn.mockImplementation(implementation);
}
return fn;
}

spyOn(object: any, methodName: any): any {
const original = object[methodName];

Expand All @@ -569,18 +577,6 @@ class ModuleMockerClass {
return object[methodName];
}

/**
* @see README.md
*/
getMockFunction(): any {
return this._makeComponent({type: 'function'});
}

// Just a short-hand alias
getMockFn(): any {
return this.getMockFunction();
}

resetAllMocks() {
this._mockRegistry = new WeakMap();
}
Expand Down
14 changes: 4 additions & 10 deletions packages/jest-runtime/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -692,6 +692,7 @@ class Runtime {
this.resetModules();
return runtime;
};
const fn = this._moduleMocker.fn.bind(this._moduleMocker);

const runtime = {
addMatchers:
Expand All @@ -706,18 +707,11 @@ class Runtime {
doMock: mock,
dontMock: unmock,
enableAutomock,
fn: (impl: ?Function) => {
const fn = this._moduleMocker.getMockFunction();
if (impl) {
return fn.mockImplementation(impl);
}
return fn;
},
genMockFn: this._moduleMocker.getMockFunction.bind(this._moduleMocker),
fn,
genMockFn: fn,
genMockFromModule:
(moduleName: string) => this._generateMock(from, moduleName),
genMockFunction:
this._moduleMocker.getMockFunction.bind(this._moduleMocker),
genMockFunction: fn,
isMockFunction: this._moduleMocker.isMockFunction,

mock,
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-util/src/FakeTimers.js
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ class FakeTimers {
}

_createMocks() {
const fn = impl => this._moduleMocker.getMockFn().mockImplementation(impl);
const fn = impl => this._moduleMocker.fn().mockImplementation(impl);

this._fakeTimerAPIs = {
clearImmediate: fn(this._fakeClearImmediate.bind(this)),
Expand Down