diff --git a/CHANGELOG.md b/CHANGELOG.md index 0f377ac43c15..bf3f890aea59 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,6 +19,7 @@ - `[jest-snapshot]` Prevent inline snapshots from drifting when inline snapshots are updated ([#8492](https://github.com/facebook/jest/pull/8492)) - `[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-mock]` Fix incorrect assignments when restoring mocks in instances where they originally didn't exist ([#8625](https://github.com/facebook/jest/pull/8625)) ### Chore & Maintenance diff --git a/packages/jest-mock/src/__tests__/index.test.ts b/packages/jest-mock/src/__tests__/index.test.ts index 64c47464be83..5c84fd7c5d03 100644 --- a/packages/jest-mock/src/__tests__/index.test.ts +++ b/packages/jest-mock/src/__tests__/index.test.ts @@ -542,6 +542,29 @@ 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(); + 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 ba06769d893b..71d20e67ac5b 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