From f465fb6c3c785543ac24ea0429207fefd5203acd Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Tue, 25 Apr 2017 12:24:47 +0200 Subject: [PATCH] Support setting returnvalue from mock to `undefined` (#3354) 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,