Skip to content

Commit 6c0b155

Browse files
committed
Revert "fix(jest-mock): do not restore mocks when jest.resetAllMocks() is called (#13866)"
This reverts commit 9432fc3.
1 parent eb61702 commit 6c0b155

File tree

2 files changed

+82
-329
lines changed

2 files changed

+82
-329
lines changed

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

Lines changed: 24 additions & 236 deletions
Original file line numberDiff line numberDiff line change
@@ -1247,6 +1247,30 @@ 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+
12501274
test('mockName gets reset by mockRestore', () => {
12511275
const fn = jest.fn();
12521276
expect(fn.getMockName()).toBe('jest.fn()');
@@ -1481,134 +1505,6 @@ describe('moduleMocker', () => {
14811505
expect(spy).toHaveBeenCalled();
14821506
});
14831507

1484-
it('supports clearing a spy', () => {
1485-
let methodOneCalls = 0;
1486-
const obj = {
1487-
methodOne() {
1488-
methodOneCalls++;
1489-
},
1490-
};
1491-
1492-
const spy1 = moduleMocker.spyOn(obj, 'methodOne');
1493-
1494-
obj.methodOne();
1495-
1496-
// The spy and the original function are called.
1497-
expect(methodOneCalls).toBe(1);
1498-
expect(spy1.mock.calls).toHaveLength(1);
1499-
1500-
expect(moduleMocker.isMockFunction(obj.methodOne)).toBe(true);
1501-
1502-
spy1.mockClear();
1503-
1504-
// After clearing the spy, the method is still mock function.
1505-
expect(moduleMocker.isMockFunction(obj.methodOne)).toBe(true);
1506-
1507-
// After clearing the spy, call count is reset.
1508-
expect(spy1.mock.calls).toHaveLength(0);
1509-
});
1510-
1511-
it('supports clearing all spies', () => {
1512-
let methodOneCalls = 0;
1513-
let methodTwoCalls = 0;
1514-
const obj = {
1515-
methodOne() {
1516-
methodOneCalls++;
1517-
},
1518-
methodTwo() {
1519-
methodTwoCalls++;
1520-
},
1521-
};
1522-
1523-
const spy1 = moduleMocker.spyOn(obj, 'methodOne');
1524-
const spy2 = moduleMocker.spyOn(obj, 'methodTwo');
1525-
1526-
obj.methodOne();
1527-
obj.methodTwo();
1528-
1529-
// Both spies and both original functions are called.
1530-
expect(methodOneCalls).toBe(1);
1531-
expect(methodTwoCalls).toBe(1);
1532-
expect(spy1.mock.calls).toHaveLength(1);
1533-
expect(spy2.mock.calls).toHaveLength(1);
1534-
1535-
expect(moduleMocker.isMockFunction(obj.methodOne)).toBe(true);
1536-
expect(moduleMocker.isMockFunction(obj.methodTwo)).toBe(true);
1537-
1538-
moduleMocker.clearAllMocks();
1539-
1540-
// After clearing all mocks, the methods are still mock functions.
1541-
expect(moduleMocker.isMockFunction(obj.methodOne)).toBe(true);
1542-
expect(moduleMocker.isMockFunction(obj.methodTwo)).toBe(true);
1543-
1544-
// After clearing all mocks, call counts are reset.
1545-
expect(spy1.mock.calls).toHaveLength(0);
1546-
expect(spy2.mock.calls).toHaveLength(0);
1547-
});
1548-
1549-
it('supports resetting a spy', () => {
1550-
const methodOneReturn = 0;
1551-
const obj = {
1552-
methodOne() {
1553-
return methodOneReturn;
1554-
},
1555-
};
1556-
1557-
const spy1 = moduleMocker.spyOn(obj, 'methodOne').mockReturnValue(10);
1558-
1559-
// Return value is mocked.
1560-
expect(methodOneReturn).toBe(0);
1561-
expect(obj.methodOne()).toBe(10);
1562-
1563-
expect(moduleMocker.isMockFunction(obj.methodOne)).toBe(true);
1564-
1565-
spy1.mockReset();
1566-
1567-
// After resetting the spy, the method is still mock functions.
1568-
expect(moduleMocker.isMockFunction(obj.methodOne)).toBe(true);
1569-
1570-
// After resetting the spy, the method returns the original return value.
1571-
expect(methodOneReturn).toBe(0);
1572-
expect(obj.methodOne()).toBe(0);
1573-
});
1574-
1575-
it('supports resetting all spies', () => {
1576-
const methodOneReturn = 10;
1577-
const methodTwoReturn = 20;
1578-
const obj = {
1579-
methodOne() {
1580-
return methodOneReturn;
1581-
},
1582-
methodTwo() {
1583-
return methodTwoReturn;
1584-
},
1585-
};
1586-
1587-
moduleMocker.spyOn(obj, 'methodOne').mockReturnValue(100);
1588-
moduleMocker.spyOn(obj, 'methodTwo').mockReturnValue(200);
1589-
1590-
// Return values are mocked.
1591-
expect(methodOneReturn).toBe(10);
1592-
expect(methodTwoReturn).toBe(20);
1593-
expect(obj.methodOne()).toBe(100);
1594-
expect(obj.methodTwo()).toBe(200);
1595-
1596-
expect(moduleMocker.isMockFunction(obj.methodOne)).toBe(true);
1597-
expect(moduleMocker.isMockFunction(obj.methodTwo)).toBe(true);
1598-
1599-
moduleMocker.resetAllMocks();
1600-
1601-
// After resetting all mocks, the methods are still mock functions.
1602-
expect(moduleMocker.isMockFunction(obj.methodOne)).toBe(true);
1603-
expect(moduleMocker.isMockFunction(obj.methodTwo)).toBe(true);
1604-
1605-
// After resetting all mocks, the methods return the original return value.
1606-
expect(methodOneReturn).toBe(10);
1607-
expect(methodTwoReturn).toBe(20);
1608-
expect(obj.methodOne()).toBe(10);
1609-
expect(obj.methodTwo()).toBe(20);
1610-
});
1611-
16121508
it('supports restoring a spy', () => {
16131509
let methodOneCalls = 0;
16141510
const obj = {
@@ -1814,59 +1710,6 @@ describe('moduleMocker', () => {
18141710
);
18151711
});
18161712

1817-
it('supports resetting a spy', () => {
1818-
const methodOneReturn = 0;
1819-
const obj = {
1820-
get methodOne() {
1821-
return methodOneReturn;
1822-
},
1823-
};
1824-
1825-
const spy1 = moduleMocker
1826-
.spyOn(obj, 'methodOne', 'get')
1827-
.mockReturnValue(10);
1828-
1829-
// Return value is mocked.
1830-
expect(methodOneReturn).toBe(0);
1831-
expect(obj.methodOne).toBe(10);
1832-
1833-
spy1.mockReset();
1834-
1835-
// After resetting the spy, the method returns the original return value.
1836-
expect(methodOneReturn).toBe(0);
1837-
expect(obj.methodOne).toBe(0);
1838-
});
1839-
1840-
it('supports resetting all spies', () => {
1841-
const methodOneReturn = 10;
1842-
const methodTwoReturn = 20;
1843-
const obj = {
1844-
get methodOne() {
1845-
return methodOneReturn;
1846-
},
1847-
get methodTwo() {
1848-
return methodTwoReturn;
1849-
},
1850-
};
1851-
1852-
moduleMocker.spyOn(obj, 'methodOne', 'get').mockReturnValue(100);
1853-
moduleMocker.spyOn(obj, 'methodTwo', 'get').mockReturnValue(200);
1854-
1855-
// Return values are mocked.
1856-
expect(methodOneReturn).toBe(10);
1857-
expect(methodTwoReturn).toBe(20);
1858-
expect(obj.methodOne).toBe(100);
1859-
expect(obj.methodTwo).toBe(200);
1860-
1861-
moduleMocker.resetAllMocks();
1862-
1863-
// After resetting all mocks, the methods return the original return value.
1864-
expect(methodOneReturn).toBe(10);
1865-
expect(methodTwoReturn).toBe(20);
1866-
expect(obj.methodOne).toBe(10);
1867-
expect(obj.methodTwo).toBe(20);
1868-
});
1869-
18701713
it('supports restoring a spy', () => {
18711714
let methodOneCalls = 0;
18721715
const obj = {
@@ -1999,61 +1842,6 @@ describe('moduleMocker', () => {
19991842
expect(obj.property).toBe(true);
20001843
});
20011844

2002-
it('supports resetting a spy on the prototype chain', () => {
2003-
const methodOneReturn = 0;
2004-
const prototype = {
2005-
get methodOne() {
2006-
return methodOneReturn;
2007-
},
2008-
};
2009-
const obj = Object.create(prototype, {});
2010-
2011-
const spy1 = moduleMocker
2012-
.spyOn(obj, 'methodOne', 'get')
2013-
.mockReturnValue(10);
2014-
2015-
// Return value is mocked.
2016-
expect(methodOneReturn).toBe(0);
2017-
expect(obj.methodOne).toBe(10);
2018-
2019-
spy1.mockReset();
2020-
2021-
// After resetting the spy, the method returns the original return value.
2022-
expect(methodOneReturn).toBe(0);
2023-
expect(obj.methodOne).toBe(0);
2024-
});
2025-
2026-
it('supports resetting all spies on the prototype chain', () => {
2027-
const methodOneReturn = 10;
2028-
const methodTwoReturn = 20;
2029-
const prototype = {
2030-
get methodOne() {
2031-
return methodOneReturn;
2032-
},
2033-
get methodTwo() {
2034-
return methodTwoReturn;
2035-
},
2036-
};
2037-
const obj = Object.create(prototype, {});
2038-
2039-
moduleMocker.spyOn(obj, 'methodOne', 'get').mockReturnValue(100);
2040-
moduleMocker.spyOn(obj, 'methodTwo', 'get').mockReturnValue(200);
2041-
2042-
// Return values are mocked.
2043-
expect(methodOneReturn).toBe(10);
2044-
expect(methodTwoReturn).toBe(20);
2045-
expect(obj.methodOne).toBe(100);
2046-
expect(obj.methodTwo).toBe(200);
2047-
2048-
moduleMocker.resetAllMocks();
2049-
2050-
// After resetting all mocks, the methods return the original return value.
2051-
expect(methodOneReturn).toBe(10);
2052-
expect(methodTwoReturn).toBe(20);
2053-
expect(obj.methodOne).toBe(10);
2054-
expect(obj.methodTwo).toBe(20);
2055-
});
2056-
20571845
it('supports restoring a spy on the prototype chain', () => {
20581846
let methodOneCalls = 0;
20591847
const prototype = {

0 commit comments

Comments
 (0)