From eb9be5aca24951246842a40d45fd1a77e539ca4f Mon Sep 17 00:00:00 2001 From: Aaron Abramov Date: Thu, 29 Jun 2017 00:58:00 -0700 Subject: [PATCH] fix mock return value (#3933) --- packages/jest-mock/src/__tests__/jest_mock.test.js | 8 ++++++++ packages/jest-mock/src/index.js | 1 + 2 files changed, 9 insertions(+) diff --git a/packages/jest-mock/src/__tests__/jest_mock.test.js b/packages/jest-mock/src/__tests__/jest_mock.test.js index 5b07888980ca..4aa0645df5cd 100644 --- a/packages/jest-mock/src/__tests__/jest_mock.test.js +++ b/packages/jest-mock/src/__tests__/jest_mock.test.js @@ -422,6 +422,14 @@ describe('moduleMocker', () => { }); }); + test('mockImplementation resets the mock', () => { + const fn = jest.fn(); + expect(fn()).toBeUndefined(); + fn.mockReturnValue('returnValue'); + fn.mockImplementation(() => 'foo'); + expect(fn()).toBe('foo'); + }); + it('should recognize a mocked function', () => { const mockFn = moduleMocker.fn(); diff --git a/packages/jest-mock/src/index.js b/packages/jest-mock/src/index.js index e41b5c221ff5..ea49d9452fff 100644 --- a/packages/jest-mock/src/index.js +++ b/packages/jest-mock/src/index.js @@ -348,6 +348,7 @@ class ModuleMockerClass { // 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; };