diff --git a/docs/MockFunctionAPI.md b/docs/MockFunctionAPI.md index 5ffb11992b84..6bc4bcd24120 100644 --- a/docs/MockFunctionAPI.md +++ b/docs/MockFunctionAPI.md @@ -130,7 +130,7 @@ Beware that `mockFn.mockClear()` will replace `mockFn.mock`, not just reset the Does everything that [`mockFn.mockClear()`](#mockfnmockclear) does, and also removes any mocked return values or implementations. -This is useful when you want to completely reset a _mock_ back to its initial state. +This is useful when you want to completely reset a _mock_ back to its initial state. (Note that resetting a _spy_ will result in a function with no return value). The [`resetMocks`](configuration#resetmocks-boolean) configuration option is available to reset mocks automatically before each test. diff --git a/packages/jest-mock/src/__tests__/index.test.ts b/packages/jest-mock/src/__tests__/index.test.ts index 6ba5685e89f8..a52f5b4921bc 100644 --- a/packages/jest-mock/src/__tests__/index.test.ts +++ b/packages/jest-mock/src/__tests__/index.test.ts @@ -1247,30 +1247,6 @@ describe('moduleMocker', () => { expect(fn.getMockName()).toBe('jest.fn()'); }); - test('after mock reset, the object should return to its original value', () => { - const myObject = {bar: () => 'bar'}; - - const barStub = moduleMocker.spyOn(myObject, 'bar'); - - barStub.mockReturnValue('POTATO!'); - expect(myObject.bar()).toBe('POTATO!'); - barStub.mockReset(); - - expect(myObject.bar()).toBe('bar'); - }); - - test('after resetAllMocks, the object should return to its original value', () => { - const myObject = {bar: () => 'bar'}; - - const barStub = moduleMocker.spyOn(myObject, 'bar'); - - barStub.mockReturnValue('POTATO!'); - expect(myObject.bar()).toBe('POTATO!'); - moduleMocker.resetAllMocks(); - - expect(myObject.bar()).toBe('bar'); - }); - test('mockName gets reset by mockRestore', () => { const fn = jest.fn(); expect(fn.getMockName()).toBe('jest.fn()'); diff --git a/packages/jest-mock/src/index.ts b/packages/jest-mock/src/index.ts index 21556c612c03..296934a66a85 100644 --- a/packages/jest-mock/src/index.ts +++ b/packages/jest-mock/src/index.ts @@ -504,7 +504,6 @@ export class ModuleMocker { private _mockConfigRegistry: WeakMap; private _spyState: Set<() => void>; private _invocationCallCounter: number; - private _originalFn: WeakMap; /** * @see README.md @@ -517,7 +516,6 @@ export class ModuleMocker { this._mockConfigRegistry = new WeakMap(); this._spyState = new Set(); this._invocationCallCounter = 1; - this._originalFn = new WeakMap(); } private _getSlots(object?: Record): Array { @@ -771,12 +769,7 @@ export class ModuleMocker { f.mockReset = () => { f.mockClear(); - const originalFn = this._originalFn.get(f); - const originalMockImpl = { - ...this._defaultMockConfig(), - mockImpl: originalFn, - }; - this._mockConfigRegistry.set(f, originalMockImpl); + this._mockConfigRegistry.delete(f); return f; }; @@ -1225,7 +1218,7 @@ export class ModuleMocker { return original.apply(this, arguments); }); } - this._originalFn.set(object[methodKey] as Mock, original); + return object[methodKey] as Mock; } @@ -1418,7 +1411,6 @@ export class ModuleMocker { } resetAllMocks(): void { - this._spyState.forEach(reset => reset()); this._mockConfigRegistry = new WeakMap(); this._mockState = new WeakMap(); }