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

Fixed mockReturnValue overriding mockImplementationOnce #8398

Merged
merged 7 commits into from Aug 22, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -25,6 +25,7 @@
- `[expect]` Extract names of async and generator functions ([#8362](https://github.com/facebook/jest/pull/8362))
- `[jest-runtime]` Fix virtual mocks not being unmockable after previously being mocked ([#8396](https://github.com/facebook/jest/pull/8396))
- `[jest-transform]` Replace special characters in transform cache filenames to support Windows ([#8353](https://github.com/facebook/jest/pull/8353))
- `[jest-mock]` Fix for mockReturnValue overriding mockImplementationOnce ([#8398](https://github.com/facebook/jest/pull/8398))

### Chore & Maintenance

Expand Down
9 changes: 9 additions & 0 deletions packages/jest-mock/src/__tests__/index.test.ts
Expand Up @@ -994,6 +994,15 @@ describe('moduleMocker', () => {
});
});

test('mockReturnValue does not override mockImplementationOnce', () => {
const mockFn = jest
.fn()
.mockReturnValue(1)
.mockImplementationOnce(() => 2);
expect(mockFn()).toBe(2);
expect(mockFn()).toBe(1);
});

test('mockImplementation resets the mock', () => {
const fn = jest.fn();
expect(fn()).toBeUndefined();
Expand Down
56 changes: 12 additions & 44 deletions packages/jest-mock/src/index.ts
Expand Up @@ -71,8 +71,6 @@ type MockFunctionState<T, Y extends Array<unknown>> = {
};

type MockFunctionConfig = {
isReturnValueLastSet: boolean;
defaultReturnValue: unknown;
mockImpl: Function | undefined;
mockName: string;
specificReturnValues: Array<unknown>;
Expand Down Expand Up @@ -456,8 +454,6 @@ class ModuleMockerClass {

private _defaultMockConfig(): MockFunctionConfig {
return {
defaultReturnValue: undefined,
isReturnValueLastSet: false,
mockImpl: undefined,
mockName: 'jest.fn()',
specificMockImpls: [],
Expand Down Expand Up @@ -586,40 +582,22 @@ class ModuleMockerClass {
return mockImpl && mockImpl.apply(this, arguments);
}

const returnValue = mockConfig.defaultReturnValue;
// If return value is last set, either specific or default, i.e.
// mockReturnValueOnce()/mockReturnValue() is called and no
// mockImplementationOnce()/mockImplementation() is called after
// that.
// use the set return value.
if (mockConfig.specificReturnValues.length) {
return mockConfig.specificReturnValues.shift();
}

if (mockConfig.isReturnValueLastSet) {
return mockConfig.defaultReturnValue;
}

// If mockImplementationOnce()/mockImplementation() is last set,
// or specific return values are used up, use the mock
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'or specific return values are used up' no longer seems accurate

// implementation.
let specificMockImpl;
if (returnValue === undefined) {
specificMockImpl = mockConfig.specificMockImpls.shift();
if (specificMockImpl === undefined) {
specificMockImpl = mockConfig.mockImpl;
}
if (specificMockImpl) {
return specificMockImpl.apply(this, arguments);
}
let specificMockImpl = mockConfig.specificMockImpls.shift();
if (specificMockImpl === undefined) {
specificMockImpl = mockConfig.mockImpl;
}
if (specificMockImpl) {
return specificMockImpl.apply(this, arguments);
}

// Otherwise use prototype implementation
if (returnValue === undefined && f._protoImpl) {
if (f._protoImpl) {
return f._protoImpl.apply(this, arguments);
}

return returnValue;
return undefined;
})();
} catch (error) {
// Store the thrown error so we can record it, then re-throw it.
Expand Down Expand Up @@ -675,26 +653,19 @@ class ModuleMockerClass {
return restore ? restore() : undefined;
};

f.mockReturnValueOnce = (value: T) => {
f.mockReturnValueOnce = (value: T) =>
// next function call will return this value or default return value
const mockConfig = this._ensureMockConfig(f);
mockConfig.specificReturnValues.push(value);
return f;
};
f.mockImplementationOnce(() => value);

f.mockResolvedValueOnce = (value: T) =>
f.mockImplementationOnce(() => Promise.resolve(value));

f.mockRejectedValueOnce = (value: T) =>
f.mockImplementationOnce(() => Promise.reject(value));

f.mockReturnValue = (value: T) => {
f.mockReturnValue = (value: T) =>
// next function call will return specified return value or this one
const mockConfig = this._ensureMockConfig(f);
mockConfig.isReturnValueLastSet = true;
mockConfig.defaultReturnValue = value;
return f;
};
f.mockImplementation(() => value);

f.mockResolvedValue = (value: T) =>
f.mockImplementation(() => Promise.resolve(value));
Expand All @@ -708,7 +679,6 @@ class ModuleMockerClass {
// next function call will use this mock implementation return value
// or default mock implementation return value
const mockConfig = this._ensureMockConfig(f);
mockConfig.isReturnValueLastSet = false;
mockConfig.specificMockImpls.push(fn);
return f;
};
Expand All @@ -718,8 +688,6 @@ class ModuleMockerClass {
): Mock<T, Y> => {
// next function call will use mock implementation return value
const mockConfig = this._ensureMockConfig(f);
mockConfig.isReturnValueLastSet = false;
mockConfig.defaultReturnValue = undefined;
mockConfig.mockImpl = fn;
return f;
};
Expand Down