Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Changed mockName() API to return the current mock name via a separate…
… getMockName() instead of overloading mockName() function to return it when no arguments are passed
  • Loading branch information
gricard committed Oct 8, 2017
commit a116716c18429d6680446ebc763edec478f7cce1
3 changes: 3 additions & 0 deletions docs/en/MockFunctionAPI.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ Mock functions are also known as "spies", because they let you spy on the behavi

## Reference

### `mockFn.getMockName()`
Returns the mock name string set by calling `mockFn.mockName(value)`

### `mockFn.mock.calls`
An array that represents all calls that have been made into this mock function. Each call is represented by an array of arguments that were passed during the call.

Expand Down
8 changes: 4 additions & 4 deletions packages/expect/src/spy_matchers.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const createToBeCalledMatcher = matcherName => (received, expected) => {

const receivedIsSpy = isSpy(received);
const type = receivedIsSpy ? 'spy' : 'mock function';
const receivedName = receivedIsSpy ? 'spy' : received.mockName();
const receivedName = receivedIsSpy ? 'spy' : received.getMockName();
const count = receivedIsSpy
? received.calls.count()
: received.mock.calls.length;
Expand Down Expand Up @@ -61,7 +61,7 @@ const createToBeCalledWithMatcher = matcherName => (

const receivedIsSpy = isSpy(received);
const type = receivedIsSpy ? 'spy' : 'mock function';
const receivedName = receivedIsSpy ? 'spy' : received.mockName();
const receivedName = receivedIsSpy ? 'spy' : received.getMockName();
const calls = receivedIsSpy
? received.calls.all().map(x => x.args)
: received.mock.calls;
Expand Down Expand Up @@ -94,7 +94,7 @@ const createLastCalledWithMatcher = matcherName => (

const receivedIsSpy = isSpy(received);
const type = receivedIsSpy ? 'spy' : 'mock function';
const receivedName = receivedIsSpy ? 'spy' : received.mockName();
const receivedName = receivedIsSpy ? 'spy' : received.getMockName();
const calls = receivedIsSpy
? received.calls.all().map(x => x.args)
: received.mock.calls;
Expand Down Expand Up @@ -127,7 +127,7 @@ const spyMatchers: MatchersObject = {

const receivedIsSpy = isSpy(received);
const type = receivedIsSpy ? 'spy' : 'mock function';
const receivedName = receivedIsSpy ? 'spy' : received.mockName();
const receivedName = receivedIsSpy ? 'spy' : received.getMockName();
const count = receivedIsSpy
? received.calls.count()
: received.mock.calls.length;
Expand Down
22 changes: 13 additions & 9 deletions packages/jest-mock/src/__tests__/jest_mock.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -458,36 +458,40 @@ describe('moduleMocker', () => {
expect(moduleMocker.isMockFunction(mockFn)).toBe(true);
});

test('default mockName is jest.fn()', () => {
const fn = jest.fn();
expect(fn.getMockName()).toBe('jest.fn()');
});

test('mockName sets the mock name', () => {
const fn = jest.fn();
expect(fn.mockName()).toBe('jest.fn()');
fn.mockName('myMockFn');
expect(fn.mockName()).toBe('myMockFn');
expect(fn.getMockName()).toBe('myMockFn');
});

test('mockName gets reset by mockReset', () => {
const fn = jest.fn();
expect(fn.mockName()).toBe('jest.fn()');
expect(fn.getMockName()).toBe('jest.fn()');
fn.mockName('myMockFn');
expect(fn.mockName()).toBe('myMockFn');
expect(fn.getMockName()).toBe('myMockFn');
fn.mockReset();
expect(fn.mockName()).toBe('jest.fn()');
expect(fn.getMockName()).toBe('jest.fn()');
});

test('mockName is not reset by mockRestore', () => {
const fn = jest.fn(() => false);
fn.mockName('myMockFn');
expect(fn.mockName()).toBe('myMockFn');
expect(fn.getMockName()).toBe('myMockFn');
fn.mockRestore();
expect(fn.mockName()).toBe('myMockFn');
expect(fn.getMockName()).toBe('myMockFn');
});

test('mockName is not reset by mockClear', () => {
const fn = jest.fn(() => false);
fn.mockName('myMockFn');
expect(fn.mockName()).toBe('myMockFn');
expect(fn.getMockName()).toBe('myMockFn');
fn.mockClear();
expect(fn.mockName()).toBe('myMockFn');
expect(fn.getMockName()).toBe('myMockFn');
});

describe('spyOn', () => {
Expand Down
12 changes: 7 additions & 5 deletions packages/jest-mock/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -436,14 +436,16 @@ class ModuleMockerClass {
});

f.mockName = name => {
if (arguments.length > 0 && name) {
if (name) {
const mockConfig = this._ensureMockConfig(f);
mockConfig.mockName = name;
return f;
} else {
const mockConfig = this._ensureMockConfig(f);
return mockConfig.mockName || 'jest.fn()';
}
return f;
};

f.getMockName = () => {
const mockConfig = this._ensureMockConfig(f);
return mockConfig.mockName || 'jest.fn()';
};

if (metadata.mockImpl) {
Expand Down