From db657c7ba99742928e3f0547110bce2527b6a89c Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Sun, 23 Apr 2017 10:16:06 +0200 Subject: [PATCH] Support setting returnvalue from mock to `undefined` Fixes #3320 --- .../jest-mock/src/__tests__/jest-mock-test.js | 20 +++++++++++++++++++ packages/jest-mock/src/index.js | 2 ++ 2 files changed, 22 insertions(+) diff --git a/packages/jest-mock/src/__tests__/jest-mock-test.js b/packages/jest-mock/src/__tests__/jest-mock-test.js index 9c9897e914d2..5265812cfcb4 100644 --- a/packages/jest-mock/src/__tests__/jest-mock-test.js +++ b/packages/jest-mock/src/__tests__/jest-mock-test.js @@ -330,6 +330,26 @@ describe('moduleMocker', () => { expect(fn2()).not.toEqual('abcd'); }); }); + + it('supports mock value returning undefined', () => { + const obj = { + func: () => 'some text', + }; + + moduleMocker.spyOn(obj, 'func').mockReturnValue(undefined); + + expect(obj.func()).not.toEqual('some text'); + }); + + it('supports mock value once returning undefined', () => { + const obj = { + func: () => 'some text', + }; + + moduleMocker.spyOn(obj, 'func').mockReturnValueOnce(undefined); + + expect(obj.func()).not.toEqual('some text'); + }); }); describe('getMockImplementation', () => { diff --git a/packages/jest-mock/src/index.js b/packages/jest-mock/src/index.js index 9627e65e43c7..ba7001b9b30e 100644 --- a/packages/jest-mock/src/index.js +++ b/packages/jest-mock/src/index.js @@ -274,6 +274,8 @@ class ModuleMockerClass { if (returnValue === undefined) { returnValue = mockConfig.defaultReturnValue; } + + return returnValue; } // If mockImplementationOnce()/mockImplementation() is last set,