Skip to content

Commit

Permalink
Revert "fix(jest-mock): clear mock when jest.restoreAllMocks() is c…
Browse files Browse the repository at this point in the history
…alled (#13867)"

This reverts commit 66fb417.
  • Loading branch information
robhogan committed Aug 21, 2023
1 parent 6c0b155 commit a5e2404
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 112 deletions.
130 changes: 19 additions & 111 deletions packages/jest-mock/src/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1505,36 +1505,6 @@ describe('moduleMocker', () => {
expect(spy).toHaveBeenCalled();
});

it('supports restoring a spy', () => {
let methodOneCalls = 0;
const obj = {
methodOne() {
methodOneCalls++;
},
};

const spy1 = moduleMocker.spyOn(obj, 'methodOne');

obj.methodOne();

// The spy and the original function got called.
expect(methodOneCalls).toBe(1);
expect(spy1.mock.calls).toHaveLength(1);

expect(moduleMocker.isMockFunction(obj.methodOne)).toBe(true);

spy1.mockRestore();

// After restoring the spy, the method is not mock function.
expect(moduleMocker.isMockFunction(obj.methodOne)).toBe(false);

obj.methodOne();

// After restoring the spy only the real method bumps its call count, not the spy.
expect(methodOneCalls).toBe(2);
expect(spy1.mock.calls).toHaveLength(0);
});

it('supports restoring all spies', () => {
let methodOneCalls = 0;
let methodTwoCalls = 0;
Expand All @@ -1550,32 +1520,25 @@ describe('moduleMocker', () => {
const spy1 = moduleMocker.spyOn(obj, 'methodOne');
const spy2 = moduleMocker.spyOn(obj, 'methodTwo');

// First, we call with the spies: both spies and both original functions
// should be called.
obj.methodOne();
obj.methodTwo();

// Both spies and both original functions got called.
expect(methodOneCalls).toBe(1);
expect(methodTwoCalls).toBe(1);
expect(spy1.mock.calls).toHaveLength(1);
expect(spy2.mock.calls).toHaveLength(1);

expect(moduleMocker.isMockFunction(obj.methodOne)).toBe(true);
expect(moduleMocker.isMockFunction(obj.methodTwo)).toBe(true);

moduleMocker.restoreAllMocks();

// After restoring all mocks, the methods are not mock functions.
expect(moduleMocker.isMockFunction(obj.methodOne)).toBe(false);
expect(moduleMocker.isMockFunction(obj.methodTwo)).toBe(false);

// Then, after resetting all mocks, we call methods again. Only the real
// methods should bump their count, not the spies.
obj.methodOne();
obj.methodTwo();

// After restoring all mocks only the real methods bump their count, not the spies.
expect(methodOneCalls).toBe(2);
expect(methodTwoCalls).toBe(2);
expect(spy1.mock.calls).toHaveLength(0);
expect(spy2.mock.calls).toHaveLength(0);
expect(spy1.mock.calls).toHaveLength(1);
expect(spy2.mock.calls).toHaveLength(1);
});

it('should work with getters', () => {
Expand Down Expand Up @@ -1710,33 +1673,6 @@ describe('moduleMocker', () => {
);
});

it('supports restoring a spy', () => {
let methodOneCalls = 0;
const obj = {
get methodOne() {
return function () {
methodOneCalls++;
};
},
};

const spy1 = moduleMocker.spyOn(obj, 'methodOne', 'get');

obj.methodOne();

// The spy and the original function are called.
expect(methodOneCalls).toBe(1);
expect(spy1.mock.calls).toHaveLength(1);

spy1.mockRestore();

obj.methodOne();

// After restoring the spy only the real method bumps its call count, not the spy.
expect(methodOneCalls).toBe(2);
expect(spy1.mock.calls).toHaveLength(0);
});

it('supports restoring all spies', () => {
let methodOneCalls = 0;
let methodTwoCalls = 0;
Expand All @@ -1756,25 +1692,25 @@ describe('moduleMocker', () => {
const spy1 = moduleMocker.spyOn(obj, 'methodOne', 'get');
const spy2 = moduleMocker.spyOn(obj, 'methodTwo', 'get');

// First, we call with the spies: both spies and both original functions
// should be called.
obj.methodOne();
obj.methodTwo();

// Both spies and both original functions got called.
expect(methodOneCalls).toBe(1);
expect(methodTwoCalls).toBe(1);
expect(spy1.mock.calls).toHaveLength(1);
expect(spy2.mock.calls).toHaveLength(1);

moduleMocker.restoreAllMocks();

// Then, after resetting all mocks, we call methods again. Only the real
// methods should bump their count, not the spies.
obj.methodOne();
obj.methodTwo();

// After restoring all mocks only the real methods bump their count, not the spies.
expect(methodOneCalls).toBe(2);
expect(methodTwoCalls).toBe(2);
expect(spy1.mock.calls).toHaveLength(0);
expect(spy2.mock.calls).toHaveLength(0);
expect(spy1.mock.calls).toHaveLength(1);
expect(spy2.mock.calls).toHaveLength(1);
});

it('should work with getters on the prototype chain', () => {
Expand Down Expand Up @@ -1842,34 +1778,6 @@ describe('moduleMocker', () => {
expect(obj.property).toBe(true);
});

it('supports restoring a spy on the prototype chain', () => {
let methodOneCalls = 0;
const prototype = {
get methodOne() {
return function () {
methodOneCalls++;
};
},
};
const obj = Object.create(prototype, {});

const spy1 = moduleMocker.spyOn(obj, 'methodOne', 'get');

obj.methodOne();

// The spy and the original function are called.
expect(methodOneCalls).toBe(1);
expect(spy1.mock.calls).toHaveLength(1);

spy1.mockRestore();

obj.methodOne();

// After restoring the spy only the real method bumps its call count, not the spy.
expect(methodOneCalls).toBe(2);
expect(spy1.mock.calls).toHaveLength(0);
});

it('supports restoring all spies on the prototype chain', () => {
let methodOneCalls = 0;
let methodTwoCalls = 0;
Expand All @@ -1890,25 +1798,25 @@ describe('moduleMocker', () => {
const spy1 = moduleMocker.spyOn(obj, 'methodOne', 'get');
const spy2 = moduleMocker.spyOn(obj, 'methodTwo', 'get');

// First, we call with the spies: both spies and both original functions
// should be called.
obj.methodOne();
obj.methodTwo();

// Both spies and both original functions got called.
expect(methodOneCalls).toBe(1);
expect(methodTwoCalls).toBe(1);
expect(spy1.mock.calls).toHaveLength(1);
expect(spy2.mock.calls).toHaveLength(1);

moduleMocker.restoreAllMocks();

// Then, after resetting all mocks, we call methods again. Only the real
// methods should bump their count, not the spies.
obj.methodOne();
obj.methodTwo();

// After restoring all mocks only the real methods bump their count, not the spies.
expect(methodOneCalls).toBe(2);
expect(methodTwoCalls).toBe(2);
expect(spy1.mock.calls).toHaveLength(0);
expect(spy2.mock.calls).toHaveLength(0);
expect(spy1.mock.calls).toHaveLength(1);
expect(spy2.mock.calls).toHaveLength(1);
});
});

Expand Down Expand Up @@ -1947,7 +1855,7 @@ describe('moduleMocker', () => {
expect(obj.property).toBe(1);
});

it('should allow mocking with value of different type', () => {
it('should allow mocking with value of different value', () => {
const obj = {
property: 1,
};
Expand Down
1 change: 0 additions & 1 deletion packages/jest-mock/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1424,7 +1424,6 @@ export class ModuleMocker {
}

restoreAllMocks(): void {
this._mockState = new WeakMap();
this._spyState.forEach(restore => restore());
this._spyState = new Set();
}
Expand Down

0 comments on commit a5e2404

Please sign in to comment.