Skip to content

Commit 33f45ad

Browse files
authored
fix: correct mocking of setters and getters again (#13472)
1 parent e61ea77 commit 33f45ad

8 files changed

+311
-394
lines changed

CHANGELOG.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@
77
### Fixes
88

99
- `[jest-environment-node]` make `globalThis.performance` writable for Node 19 and fake timers ([#13467](https://github.com/facebook/jest/pull/13467))
10+
- `[jest-mock]` Revert [#13398](https://github.com/facebook/jest/pull/13398) to restore mocking of setters ([#13472](https://github.com/facebook/jest/pull/13472))
1011

1112
### Chore & Maintenance
1213

1314
### Performance
1415

15-
- `[*]` Use sha1 instead of sha256 for hashing [#13421](https://github.com/facebook/jest/pull/13421)
16+
- `[*]` Use sha1 instead of sha256 for hashing ([#13421](https://github.com/facebook/jest/pull/13421))
1617

1718
## 29.2.0
1819

packages/jest-mock/src/__tests__/__fixtures__/class-mocks-types.ts

+13
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,16 @@ export default class SuperTestClass {
5252
}
5353

5454
export class TestClass extends SuperTestClass {}
55+
56+
export function testFunction1() {
57+
return 'testFunction1';
58+
}
59+
60+
function testFunction() {
61+
return 'testFunction2';
62+
}
63+
export const testFunction2 = testFunction;
64+
65+
export const testFunction3 = () => {
66+
return 'testFunction3';
67+
};

packages/jest-mock/src/__tests__/class-mocks-dual-import.test.ts

-92
Original file line numberDiff line numberDiff line change
@@ -35,96 +35,4 @@ describe('Testing the mocking of a class hierarchy defined in multiple imports',
3535
expect(testClassInstance.testMethod()).toBe('mockTestMethod');
3636
expect(mockTestMethod).toHaveBeenCalledTimes(1);
3737
});
38-
39-
it('can read a value from an instance getter - Auto-mocked class', () => {
40-
const mockTestMethod = jest
41-
.spyOn(SuperTestClass.prototype, 'testAccessor', 'get')
42-
.mockImplementation(() => {
43-
return 'mockTestAccessor';
44-
});
45-
const testClassInstance = new SuperTestClass();
46-
expect(testClassInstance.testAccessor).toBe('mockTestAccessor');
47-
expect(mockTestMethod).toHaveBeenCalledTimes(1);
48-
49-
mockTestMethod.mockClear();
50-
});
51-
52-
it('can read a value from a superclass instance getter - Auto-mocked class', () => {
53-
const mockTestMethod = jest
54-
.spyOn(TestClass.prototype, 'testAccessor', 'get')
55-
.mockImplementation(() => {
56-
return 'mockTestAccessor';
57-
});
58-
const testClassInstance = new TestClass();
59-
expect(testClassInstance.testAccessor).toBe('mockTestAccessor');
60-
expect(mockTestMethod).toHaveBeenCalledTimes(1);
61-
});
62-
63-
it('can write a value to an instance setter - Auto-mocked class', () => {
64-
const mockTestMethod = jest
65-
.spyOn(SuperTestClass.prototype, 'testAccessor', 'set')
66-
.mockImplementation((_x: string) => {
67-
return () => {};
68-
});
69-
const testClassInstance = new SuperTestClass();
70-
testClassInstance.testAccessor = '';
71-
expect(mockTestMethod).toHaveBeenCalledTimes(1);
72-
73-
mockTestMethod.mockClear();
74-
});
75-
76-
it('can write a value to a superclass instance setter - Auto-mocked class', () => {
77-
const mockTestMethod = jest
78-
.spyOn(TestClass.prototype, 'testAccessor', 'set')
79-
.mockImplementation((_x: string) => {
80-
return () => {};
81-
});
82-
const testClassInstance = new TestClass();
83-
testClassInstance.testAccessor = '';
84-
expect(mockTestMethod).toHaveBeenCalledTimes(1);
85-
});
86-
87-
it('can read a value from a static getter - Auto-mocked class', () => {
88-
const mockTestMethod = jest
89-
.spyOn(SuperTestClass, 'staticTestAccessor', 'get')
90-
.mockImplementation(() => {
91-
return 'mockStaticTestAccessor';
92-
});
93-
expect(SuperTestClass.staticTestAccessor).toBe('mockStaticTestAccessor');
94-
expect(mockTestMethod).toHaveBeenCalledTimes(1);
95-
96-
mockTestMethod.mockClear();
97-
});
98-
99-
it('can read a value from a superclass static getter - Auto-mocked class', () => {
100-
const mockTestMethod = jest
101-
.spyOn(TestClass, 'staticTestAccessor', 'get')
102-
.mockImplementation(() => {
103-
return 'mockStaticTestAccessor';
104-
});
105-
expect(TestClass.staticTestAccessor).toBe('mockStaticTestAccessor');
106-
expect(mockTestMethod).toHaveBeenCalledTimes(1);
107-
});
108-
109-
it('can write a value to a static setter - Auto-mocked class', () => {
110-
const mockTestMethod = jest
111-
.spyOn(SuperTestClass, 'staticTestAccessor', 'set')
112-
.mockImplementation((_x: string) => {
113-
return () => {};
114-
});
115-
SuperTestClass.staticTestAccessor = '';
116-
expect(mockTestMethod).toHaveBeenCalledTimes(1);
117-
118-
mockTestMethod.mockClear();
119-
});
120-
121-
it('can write a value to a superclass static setter - Auto-mocked class', () => {
122-
const mockTestMethod = jest
123-
.spyOn(TestClass, 'staticTestAccessor', 'set')
124-
.mockImplementation((_x: string) => {
125-
return () => {};
126-
});
127-
TestClass.staticTestAccessor = '';
128-
expect(mockTestMethod).toHaveBeenCalledTimes(1);
129-
});
13038
});

packages/jest-mock/src/__tests__/class-mocks-single-import.test.ts

+36-105
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,32 @@
66
*
77
*/
88

9-
import SuperTestClass, {TestClass} from './__fixtures__/class-mocks-types';
9+
import SuperTestClass, * as testTypes from './__fixtures__/class-mocks-types';
1010
jest.mock('./__fixtures__/class-mocks-types');
1111

12+
describe('Testing the mocking of exported functions', () => {
13+
it('can mock a directly exported function', () => {
14+
jest.spyOn(testTypes, 'testFunction1').mockImplementation(() => {
15+
return 'mockTestFunction';
16+
});
17+
expect(testTypes.testFunction1()).toBe('mockTestFunction');
18+
});
19+
20+
it('can mock an indirectly exported function', () => {
21+
jest.spyOn(testTypes, 'testFunction2').mockImplementation(() => {
22+
return 'mockTestFunction';
23+
});
24+
expect(testTypes.testFunction2()).toBe('mockTestFunction');
25+
});
26+
27+
it('can mock an indirectly exported anonymous function', () => {
28+
jest.spyOn(testTypes, 'testFunction3').mockImplementation(() => {
29+
return 'mockTestFunction';
30+
});
31+
expect(testTypes.testFunction3()).toBe('mockTestFunction');
32+
});
33+
});
34+
1235
describe('Testing the mocking of a class hierarchy defined in a single import', () => {
1336
it('can call an instance method - Auto-mocked class', () => {
1437
const mockTestMethod = jest
@@ -25,11 +48,11 @@ describe('Testing the mocking of a class hierarchy defined in a single import',
2548

2649
it('can call a superclass instance method - Auto-mocked class', () => {
2750
const mockTestMethod = jest
28-
.spyOn(TestClass.prototype, 'testMethod')
51+
.spyOn(testTypes.TestClass.prototype, 'testMethod')
2952
.mockImplementation(() => {
3053
return 'mockTestMethod';
3154
});
32-
const testClassInstance = new TestClass();
55+
const testClassInstance = new testTypes.TestClass();
3356
expect(testClassInstance.testMethod()).toBe('mockTestMethod');
3457
expect(mockTestMethod).toHaveBeenCalledTimes(1);
3558
});
@@ -49,11 +72,11 @@ describe('Testing the mocking of a class hierarchy defined in a single import',
4972

5073
it('can call a superclass instance method named "get" - Auto-mocked class', () => {
5174
const mockTestMethod = jest
52-
.spyOn(TestClass.prototype, 'get')
75+
.spyOn(testTypes.TestClass.prototype, 'get')
5376
.mockImplementation(() => {
5477
return 'mockTestMethod';
5578
});
56-
const testClassInstance = new TestClass();
79+
const testClassInstance = new testTypes.TestClass();
5780
expect(testClassInstance.get()).toBe('mockTestMethod');
5881
expect(mockTestMethod).toHaveBeenCalledTimes(1);
5982

@@ -75,65 +98,17 @@ describe('Testing the mocking of a class hierarchy defined in a single import',
7598

7699
it('can call a superclass instance method named "set" - Auto-mocked class', () => {
77100
const mockTestMethod = jest
78-
.spyOn(TestClass.prototype, 'set')
101+
.spyOn(testTypes.TestClass.prototype, 'set')
79102
.mockImplementation(() => {
80103
return 'mockTestMethod';
81104
});
82-
const testClassInstance = new TestClass();
105+
const testClassInstance = new testTypes.TestClass();
83106
expect(testClassInstance.set()).toBe('mockTestMethod');
84107
expect(mockTestMethod).toHaveBeenCalledTimes(1);
85108

86109
mockTestMethod.mockClear();
87110
});
88111

89-
it('can read a value from an instance getter - Auto-mocked class', () => {
90-
const mockTestMethod = jest
91-
.spyOn(SuperTestClass.prototype, 'testAccessor', 'get')
92-
.mockImplementation(() => {
93-
return 'mockTestAccessor';
94-
});
95-
const testClassInstance = new SuperTestClass();
96-
expect(testClassInstance.testAccessor).toBe('mockTestAccessor');
97-
expect(mockTestMethod).toHaveBeenCalledTimes(1);
98-
99-
mockTestMethod.mockClear();
100-
});
101-
102-
it('can read a value from a superclass instance getter - Auto-mocked class', () => {
103-
const mockTestMethod = jest
104-
.spyOn(TestClass.prototype, 'testAccessor', 'get')
105-
.mockImplementation(() => {
106-
return 'mockTestAccessor';
107-
});
108-
const testClassInstance = new TestClass();
109-
expect(testClassInstance.testAccessor).toBe('mockTestAccessor');
110-
expect(mockTestMethod).toHaveBeenCalledTimes(1);
111-
});
112-
113-
it('can write a value to an instance setter - Auto-mocked class', () => {
114-
const mockTestMethod = jest
115-
.spyOn(SuperTestClass.prototype, 'testAccessor', 'set')
116-
.mockImplementation((_x: string) => {
117-
return () => {};
118-
});
119-
const testClassInstance = new SuperTestClass();
120-
testClassInstance.testAccessor = '';
121-
expect(mockTestMethod).toHaveBeenCalledTimes(1);
122-
123-
mockTestMethod.mockClear();
124-
});
125-
126-
it('can write a value to a superclass instance setter - Auto-mocked class', () => {
127-
const mockTestMethod = jest
128-
.spyOn(TestClass.prototype, 'testAccessor', 'set')
129-
.mockImplementation((_x: string) => {
130-
return () => {};
131-
});
132-
const testClassInstance = new TestClass();
133-
testClassInstance.testAccessor = '';
134-
expect(mockTestMethod).toHaveBeenCalledTimes(1);
135-
});
136-
137112
it('can call a static method - Auto-mocked class', () => {
138113
const mockTestMethod = jest
139114
.spyOn(SuperTestClass, 'staticTestMethod')
@@ -148,11 +123,11 @@ describe('Testing the mocking of a class hierarchy defined in a single import',
148123

149124
it('can call a superclass static method - Auto-mocked class', () => {
150125
const mockTestMethod = jest
151-
.spyOn(TestClass, 'staticTestMethod')
126+
.spyOn(testTypes.TestClass, 'staticTestMethod')
152127
.mockImplementation(() => {
153128
return 'mockTestMethod';
154129
});
155-
expect(TestClass.staticTestMethod()).toBe('mockTestMethod');
130+
expect(testTypes.TestClass.staticTestMethod()).toBe('mockTestMethod');
156131
expect(mockTestMethod).toHaveBeenCalledTimes(1);
157132
});
158133

@@ -170,11 +145,11 @@ describe('Testing the mocking of a class hierarchy defined in a single import',
170145

171146
it('can call a superclass static method named "get" - Auto-mocked class', () => {
172147
const mockTestMethod = jest
173-
.spyOn(TestClass, 'get')
148+
.spyOn(testTypes.TestClass, 'get')
174149
.mockImplementation(() => {
175150
return 'mockTestMethod';
176151
});
177-
expect(TestClass.get()).toBe('mockTestMethod');
152+
expect(testTypes.TestClass.get()).toBe('mockTestMethod');
178153
expect(mockTestMethod).toHaveBeenCalledTimes(1);
179154

180155
mockTestMethod.mockClear();
@@ -194,57 +169,13 @@ describe('Testing the mocking of a class hierarchy defined in a single import',
194169

195170
it('can call a superclass static method named "set" - Auto-mocked class', () => {
196171
const mockTestMethod = jest
197-
.spyOn(TestClass, 'set')
172+
.spyOn(testTypes.TestClass, 'set')
198173
.mockImplementation(() => {
199174
return 'mockTestMethod';
200175
});
201-
expect(TestClass.set()).toBe('mockTestMethod');
202-
expect(mockTestMethod).toHaveBeenCalledTimes(1);
203-
204-
mockTestMethod.mockClear();
205-
});
206-
207-
it('can read a value from a static getter - Auto-mocked class', () => {
208-
const mockTestMethod = jest
209-
.spyOn(SuperTestClass, 'staticTestAccessor', 'get')
210-
.mockImplementation(() => {
211-
return 'mockStaticTestAccessor';
212-
});
213-
expect(SuperTestClass.staticTestAccessor).toBe('mockStaticTestAccessor');
214-
expect(mockTestMethod).toHaveBeenCalledTimes(1);
215-
216-
mockTestMethod.mockClear();
217-
});
218-
219-
it('can read a value from a superclass static getter - Auto-mocked class', () => {
220-
const mockTestMethod = jest
221-
.spyOn(TestClass, 'staticTestAccessor', 'get')
222-
.mockImplementation(() => {
223-
return 'mockStaticTestAccessor';
224-
});
225-
expect(TestClass.staticTestAccessor).toBe('mockStaticTestAccessor');
226-
expect(mockTestMethod).toHaveBeenCalledTimes(1);
227-
});
228-
229-
it('can write a value to a static setter - Auto-mocked class', () => {
230-
const mockTestMethod = jest
231-
.spyOn(SuperTestClass, 'staticTestAccessor', 'set')
232-
.mockImplementation((_x: string) => {
233-
return () => {};
234-
});
235-
SuperTestClass.staticTestAccessor = '';
176+
expect(testTypes.TestClass.set()).toBe('mockTestMethod');
236177
expect(mockTestMethod).toHaveBeenCalledTimes(1);
237178

238179
mockTestMethod.mockClear();
239180
});
240-
241-
it('can write a value to a superclass static setter - Auto-mocked class', () => {
242-
const mockTestMethod = jest
243-
.spyOn(TestClass, 'staticTestAccessor', 'set')
244-
.mockImplementation((_x: string) => {
245-
return () => {};
246-
});
247-
TestClass.staticTestAccessor = '';
248-
expect(mockTestMethod).toHaveBeenCalledTimes(1);
249-
});
250181
});

0 commit comments

Comments
 (0)