Skip to content

Commit f126336

Browse files
authored
fix: mocking of getters/setters on automatically mocked classes (#13398)
1 parent 30da552 commit f126336

File tree

9 files changed

+966
-122
lines changed

9 files changed

+966
-122
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
### Fixes
1010

1111
- `[babel-plugin-jest-hoist]` Ignore `TSTypeQuery` when checking for hoisted references ([#13367](https://github.com/facebook/jest/pull/13367))
12+
- `[jest-mock]` Fix mocking of getters and setters on classes ([#13398](https://github.com/facebook/jest/pull/13398))
1213
- `[jest-reporters]` Revert: Transform file paths into hyperlinks ([#13399](https://github.com/facebook/jest/pull/13399))
1314
- `[@jest/types]` Infer type of `each` table correctly when the table is a tuple or array ([#13381](https://github.com/facebook/jest/pull/13381))
1415
- `[@jest/types]` Rework typings to allow the `*ReturnedWith` matchers to be called with no argument ([#13385](https://github.com/facebook/jest/pull/13385))
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
*/
8+
9+
export class SuperTestClass {
10+
static staticTestProperty = 'staticTestProperty';
11+
12+
static get staticTestAccessor(): string {
13+
return 'staticTestAccessor';
14+
}
15+
16+
static set staticTestAccessor(_x: string) {
17+
return;
18+
}
19+
20+
static staticTestMethod(): string {
21+
return 'staticTestMethod';
22+
}
23+
24+
testProperty = 'testProperty';
25+
26+
get testAccessor(): string {
27+
return 'testAccessor';
28+
}
29+
set testAccessor(_x: string) {
30+
return;
31+
}
32+
33+
testMethod(): string {
34+
return 'testMethod';
35+
}
36+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
*/
8+
9+
import {SuperTestClass} from './SuperTestClass';
10+
11+
export default class TestClass extends SuperTestClass {}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
*/
8+
9+
export default class SuperTestClass {
10+
static staticTestProperty = 'staticTestProperty';
11+
12+
static get staticTestAccessor(): string {
13+
return 'staticTestAccessor';
14+
}
15+
16+
static set staticTestAccessor(_x: string) {
17+
return;
18+
}
19+
20+
static staticTestMethod(): string {
21+
return 'staticTestMethod';
22+
}
23+
24+
static get(): string {
25+
return 'get';
26+
}
27+
28+
static set(): string {
29+
return 'set';
30+
}
31+
32+
testProperty = 'testProperty';
33+
34+
get testAccessor(): string {
35+
return 'testAccessor';
36+
}
37+
set testAccessor(_x: string) {
38+
return;
39+
}
40+
41+
testMethod(): string {
42+
return 'testMethod';
43+
}
44+
45+
get(): string {
46+
return 'get';
47+
}
48+
49+
set(): string {
50+
return 'set';
51+
}
52+
}
53+
54+
export class TestClass extends SuperTestClass {}
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
*/
8+
9+
import {SuperTestClass} from './__fixtures__/SuperTestClass';
10+
import TestClass from './__fixtures__/TestClass';
11+
jest.mock('./__fixtures__/SuperTestClass');
12+
jest.mock('./__fixtures__/TestClass');
13+
14+
describe('Testing the mocking of a class hierarchy defined in multiple imports', () => {
15+
it('can call an instance method - Auto-mocked class', () => {
16+
const mockTestMethod = jest
17+
.spyOn(SuperTestClass.prototype, 'testMethod')
18+
.mockImplementation(() => {
19+
return 'mockTestMethod';
20+
});
21+
const testClassInstance = new SuperTestClass();
22+
expect(testClassInstance.testMethod()).toBe('mockTestMethod');
23+
expect(mockTestMethod).toHaveBeenCalledTimes(1);
24+
25+
mockTestMethod.mockClear();
26+
});
27+
28+
it('can call a superclass instance method - Auto-mocked class', () => {
29+
const mockTestMethod = jest
30+
.spyOn(TestClass.prototype, 'testMethod')
31+
.mockImplementation(() => {
32+
return 'mockTestMethod';
33+
});
34+
const testClassInstance = new TestClass();
35+
expect(testClassInstance.testMethod()).toBe('mockTestMethod');
36+
expect(mockTestMethod).toHaveBeenCalledTimes(1);
37+
});
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+
});
130+
});

0 commit comments

Comments
 (0)