From b76f01bc1c93e2bc4f4f0ed9834227753d7d65cc Mon Sep 17 00:00:00 2001 From: Lucas Fernandes da Costa Date: Thu, 11 Jul 2019 09:12:37 +0100 Subject: [PATCH] Fix mock reassigning to previously inexistent method (#8631) --- CHANGELOG.md | 1 + .../jest-mock/src/__tests__/index.test.ts | 26 +++++++++++++++++++ packages/jest-mock/src/index.ts | 8 +++++- 3 files changed, 34 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d0ca48a0a8dc..2f9aa69ebc30 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -23,6 +23,7 @@ - `[jest-haste-map]` Don't throw on missing mapper in Node crawler ([#8558](https://github.com/facebook/jest/pull/8558)) - `[jest-core]` Fix incorrect `passWithNoTests` warning ([#8595](https://github.com/facebook/jest/pull/8595)) - `[jest-snapshots]` Fix test retries that contain snapshots ([#8629](https://github.com/facebook/jest/pull/8629)) +- `[jest-mock]` Fix incorrect assignments when restoring mocks in instances where they originally didn't exist ([#8631](https://github.com/facebook/jest/pull/8631)) ### Chore & Maintenance diff --git a/packages/jest-mock/src/__tests__/index.test.ts b/packages/jest-mock/src/__tests__/index.test.ts index 64c47464be83..f665def33ea6 100644 --- a/packages/jest-mock/src/__tests__/index.test.ts +++ b/packages/jest-mock/src/__tests__/index.test.ts @@ -542,6 +542,32 @@ describe('moduleMocker', () => { }); }); + it('mocks the method in the passed object itself', () => { + const parent = {func: () => 'abcd'}; + const child = Object.create(parent); + + moduleMocker.spyOn(child, 'func').mockReturnValue('efgh'); + + expect(child.hasOwnProperty('func')).toBe(true); + expect(child.func()).toEqual('efgh'); + expect(parent.func()).toEqual('abcd'); + }); + + it('should delete previously inexistent methods when restoring', () => { + const parent = {func: () => 'abcd'}; + const child = Object.create(parent); + + moduleMocker.spyOn(child, 'func').mockReturnValue('efgh'); + + moduleMocker.restoreAllMocks(); + expect(child.func()).toEqual('abcd'); + + moduleMocker.spyOn(parent, 'func').mockReturnValue('jklm'); + + expect(child.hasOwnProperty('func')).toBe(false); + expect(child.func()).toEqual('jklm'); + }); + it('supports mock value returning undefined', () => { const obj = { func: () => 'some text', diff --git a/packages/jest-mock/src/index.ts b/packages/jest-mock/src/index.ts index 7647b81c379a..be19de0df52f 100644 --- a/packages/jest-mock/src/index.ts +++ b/packages/jest-mock/src/index.ts @@ -1009,9 +1009,15 @@ class ModuleMockerClass { ); } + const isMethodOwner = object.hasOwnProperty(methodName); + // @ts-ignore overriding original method with a Mock object[methodName] = this._makeComponent({type: 'function'}, () => { - object[methodName] = original; + if (isMethodOwner) { + object[methodName] = original; + } else { + delete object[methodName]; + } }); // @ts-ignore original method is now a Mock