Skip to content

Commit 091ed55

Browse files
committed
Revert "fix: stop changing the behaviour of spies on mockFn.mockReset (#13692)"
This reverts commit eace3a1.
1 parent a5e2404 commit 091ed55

File tree

3 files changed

+3
-35
lines changed

3 files changed

+3
-35
lines changed

docs/MockFunctionAPI.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ Beware that `mockFn.mockClear()` will replace `mockFn.mock`, not just reset the
130130

131131
Does everything that [`mockFn.mockClear()`](#mockfnmockclear) does, and also removes any mocked return values or implementations.
132132

133-
This is useful when you want to completely reset a _mock_ back to its initial state.
133+
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).
134134

135135
The [`resetMocks`](configuration#resetmocks-boolean) configuration option is available to reset mocks automatically before each test.
136136

packages/jest-mock/src/__tests__/index.test.ts

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1247,30 +1247,6 @@ describe('moduleMocker', () => {
12471247
expect(fn.getMockName()).toBe('jest.fn()');
12481248
});
12491249

1250-
test('after mock reset, the object should return to its original value', () => {
1251-
const myObject = {bar: () => 'bar'};
1252-
1253-
const barStub = moduleMocker.spyOn(myObject, 'bar');
1254-
1255-
barStub.mockReturnValue('POTATO!');
1256-
expect(myObject.bar()).toBe('POTATO!');
1257-
barStub.mockReset();
1258-
1259-
expect(myObject.bar()).toBe('bar');
1260-
});
1261-
1262-
test('after resetAllMocks, the object should return to its original value', () => {
1263-
const myObject = {bar: () => 'bar'};
1264-
1265-
const barStub = moduleMocker.spyOn(myObject, 'bar');
1266-
1267-
barStub.mockReturnValue('POTATO!');
1268-
expect(myObject.bar()).toBe('POTATO!');
1269-
moduleMocker.resetAllMocks();
1270-
1271-
expect(myObject.bar()).toBe('bar');
1272-
});
1273-
12741250
test('mockName gets reset by mockRestore', () => {
12751251
const fn = jest.fn();
12761252
expect(fn.getMockName()).toBe('jest.fn()');

packages/jest-mock/src/index.ts

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -504,7 +504,6 @@ export class ModuleMocker {
504504
private _mockConfigRegistry: WeakMap<Function, MockFunctionConfig>;
505505
private _spyState: Set<() => void>;
506506
private _invocationCallCounter: number;
507-
private _originalFn: WeakMap<Mock, Function>;
508507

509508
/**
510509
* @see README.md
@@ -517,7 +516,6 @@ export class ModuleMocker {
517516
this._mockConfigRegistry = new WeakMap();
518517
this._spyState = new Set();
519518
this._invocationCallCounter = 1;
520-
this._originalFn = new WeakMap();
521519
}
522520

523521
private _getSlots(object?: Record<string, any>): Array<string> {
@@ -771,12 +769,7 @@ export class ModuleMocker {
771769

772770
f.mockReset = () => {
773771
f.mockClear();
774-
const originalFn = this._originalFn.get(f);
775-
const originalMockImpl = {
776-
...this._defaultMockConfig(),
777-
mockImpl: originalFn,
778-
};
779-
this._mockConfigRegistry.set(f, originalMockImpl);
772+
this._mockConfigRegistry.delete(f);
780773
return f;
781774
};
782775

@@ -1225,7 +1218,7 @@ export class ModuleMocker {
12251218
return original.apply(this, arguments);
12261219
});
12271220
}
1228-
this._originalFn.set(object[methodKey] as Mock, original);
1221+
12291222
return object[methodKey] as Mock;
12301223
}
12311224

@@ -1418,7 +1411,6 @@ export class ModuleMocker {
14181411
}
14191412

14201413
resetAllMocks(): void {
1421-
this._spyState.forEach(reset => reset());
14221414
this._mockConfigRegistry = new WeakMap();
14231415
this._mockState = new WeakMap();
14241416
}

0 commit comments

Comments
 (0)