-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: mocking of getters/setters on automatically mocked classes (#13145)
Co-authored-by: Tom Mrazauskas <tom@mrazauskas.de>
- Loading branch information
1 parent
4adf6ab
commit 82e503f
Showing
10 changed files
with
680 additions
and
114 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
*/ | ||
|
||
export class SuperTestClass { | ||
static staticTestProperty = 'staticTestProperty'; | ||
|
||
static get staticTestAccessor(): string { | ||
return 'staticTestAccessor'; | ||
} | ||
|
||
static set staticTestAccessor(_x: string) { | ||
return; | ||
} | ||
|
||
static staticTestMethod(): string { | ||
return 'staticTestMethod'; | ||
} | ||
|
||
testProperty = 'testProperty'; | ||
|
||
get testAccessor(): string { | ||
return 'testAccessor'; | ||
} | ||
set testAccessor(_x: string) { | ||
return; | ||
} | ||
|
||
testMethod(): string { | ||
return 'testMethod'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
*/ | ||
|
||
import {SuperTestClass} from './SuperTestClass'; | ||
|
||
export default class TestClass extends SuperTestClass {} |
130 changes: 130 additions & 0 deletions
130
packages/jest-mock/src/__tests__/class-mocks-dual-import.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
*/ | ||
|
||
import {SuperTestClass} from './SuperTestClass'; | ||
import TestClass from './TestClass'; | ||
jest.mock('./SuperTestClass'); | ||
jest.mock('./TestClass'); | ||
|
||
describe('Testing the mocking of a class hierarchy defined in multiple imports', () => { | ||
it('can call an instance method - Auto-mocked class', () => { | ||
const mockTestMethod = jest | ||
.spyOn(SuperTestClass.prototype, 'testMethod') | ||
.mockImplementation(() => { | ||
return 'mockTestMethod'; | ||
}); | ||
const testClassInstance = new SuperTestClass(); | ||
expect(testClassInstance.testMethod()).toEqual('mockTestMethod'); | ||
expect(mockTestMethod).toHaveBeenCalledTimes(1); | ||
|
||
mockTestMethod.mockClear(); | ||
}); | ||
|
||
it('can call a superclass instance method - Auto-mocked class', () => { | ||
const mockTestMethod = jest | ||
.spyOn(TestClass.prototype, 'testMethod') | ||
.mockImplementation(() => { | ||
return 'mockTestMethod'; | ||
}); | ||
const testClassInstance = new TestClass(); | ||
expect(testClassInstance.testMethod()).toEqual('mockTestMethod'); | ||
expect(mockTestMethod).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it('can read a value from an instance getter - Auto-mocked class', () => { | ||
const mockTestMethod = jest | ||
.spyOn(SuperTestClass.prototype, 'testAccessor', 'get') | ||
.mockImplementation(() => { | ||
return 'mockTestAccessor'; | ||
}); | ||
const testClassInstance = new SuperTestClass(); | ||
expect(testClassInstance.testAccessor).toEqual('mockTestAccessor'); | ||
expect(mockTestMethod).toHaveBeenCalledTimes(1); | ||
|
||
mockTestMethod.mockClear(); | ||
}); | ||
|
||
it('can read a value from a superclass instance getter - Auto-mocked class', () => { | ||
const mockTestMethod = jest | ||
.spyOn(TestClass.prototype, 'testAccessor', 'get') | ||
.mockImplementation(() => { | ||
return 'mockTestAccessor'; | ||
}); | ||
const testClassInstance = new TestClass(); | ||
expect(testClassInstance.testAccessor).toEqual('mockTestAccessor'); | ||
expect(mockTestMethod).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it('can write a value to an instance setter - Auto-mocked class', () => { | ||
const mockTestMethod = jest | ||
.spyOn(SuperTestClass.prototype, 'testAccessor', 'set') | ||
.mockImplementation((_x: string) => { | ||
return () => {}; | ||
}); | ||
const testClassInstance = new SuperTestClass(); | ||
testClassInstance.testAccessor = ''; | ||
expect(mockTestMethod).toHaveBeenCalledTimes(1); | ||
|
||
mockTestMethod.mockClear(); | ||
}); | ||
|
||
it('can write a value to a superclass instance setter - Auto-mocked class', () => { | ||
const mockTestMethod = jest | ||
.spyOn(TestClass.prototype, 'testAccessor', 'set') | ||
.mockImplementation((_x: string) => { | ||
return () => {}; | ||
}); | ||
const testClassInstance = new TestClass(); | ||
testClassInstance.testAccessor = ''; | ||
expect(mockTestMethod).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it('can read a value from a static getter - Auto-mocked class', () => { | ||
const mockTestMethod = jest | ||
.spyOn(SuperTestClass, 'staticTestAccessor', 'get') | ||
.mockImplementation(() => { | ||
return 'mockStaticTestAccessor'; | ||
}); | ||
expect(SuperTestClass.staticTestAccessor).toEqual('mockStaticTestAccessor'); | ||
expect(mockTestMethod).toHaveBeenCalledTimes(1); | ||
|
||
mockTestMethod.mockClear(); | ||
}); | ||
|
||
it('can read a value from a superclass static getter - Auto-mocked class', () => { | ||
const mockTestMethod = jest | ||
.spyOn(TestClass, 'staticTestAccessor', 'get') | ||
.mockImplementation(() => { | ||
return 'mockStaticTestAccessor'; | ||
}); | ||
expect(TestClass.staticTestAccessor).toEqual('mockStaticTestAccessor'); | ||
expect(mockTestMethod).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it('can write a value to a static setter - Auto-mocked class', () => { | ||
const mockTestMethod = jest | ||
.spyOn(SuperTestClass, 'staticTestAccessor', 'set') | ||
.mockImplementation((_x: string) => { | ||
return () => {}; | ||
}); | ||
SuperTestClass.staticTestAccessor = ''; | ||
expect(mockTestMethod).toHaveBeenCalledTimes(1); | ||
|
||
mockTestMethod.mockClear(); | ||
}); | ||
|
||
it('can write a value to a superclass static setter - Auto-mocked class', () => { | ||
const mockTestMethod = jest | ||
.spyOn(TestClass, 'staticTestAccessor', 'set') | ||
.mockImplementation((_x: string) => { | ||
return () => {}; | ||
}); | ||
TestClass.staticTestAccessor = ''; | ||
expect(mockTestMethod).toHaveBeenCalledTimes(1); | ||
}); | ||
}); |
128 changes: 128 additions & 0 deletions
128
packages/jest-mock/src/__tests__/class-mocks-single-import.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
*/ | ||
|
||
import SuperTestClass, {TestClass} from './class-mocks-types'; | ||
jest.mock('./class-mocks-types'); | ||
|
||
describe('Testing the mocking of a class hierarchy defined in a single import', () => { | ||
it('can call an instance method - Auto-mocked class', () => { | ||
const mockTestMethod = jest | ||
.spyOn(SuperTestClass.prototype, 'testMethod') | ||
.mockImplementation(() => { | ||
return 'mockTestMethod'; | ||
}); | ||
const testClassInstance = new SuperTestClass(); | ||
expect(testClassInstance.testMethod()).toEqual('mockTestMethod'); | ||
expect(mockTestMethod).toHaveBeenCalledTimes(1); | ||
|
||
mockTestMethod.mockClear(); | ||
}); | ||
|
||
it('can call a superclass instance method - Auto-mocked class', () => { | ||
const mockTestMethod = jest | ||
.spyOn(TestClass.prototype, 'testMethod') | ||
.mockImplementation(() => { | ||
return 'mockTestMethod'; | ||
}); | ||
const testClassInstance = new TestClass(); | ||
expect(testClassInstance.testMethod()).toEqual('mockTestMethod'); | ||
expect(mockTestMethod).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it('can read a value from an instance getter - Auto-mocked class', () => { | ||
const mockTestMethod = jest | ||
.spyOn(SuperTestClass.prototype, 'testAccessor', 'get') | ||
.mockImplementation(() => { | ||
return 'mockTestAccessor'; | ||
}); | ||
const testClassInstance = new SuperTestClass(); | ||
expect(testClassInstance.testAccessor).toEqual('mockTestAccessor'); | ||
expect(mockTestMethod).toHaveBeenCalledTimes(1); | ||
|
||
mockTestMethod.mockClear(); | ||
}); | ||
|
||
it('can read a value from a superclass instance getter - Auto-mocked class', () => { | ||
const mockTestMethod = jest | ||
.spyOn(TestClass.prototype, 'testAccessor', 'get') | ||
.mockImplementation(() => { | ||
return 'mockTestAccessor'; | ||
}); | ||
const testClassInstance = new TestClass(); | ||
expect(testClassInstance.testAccessor).toEqual('mockTestAccessor'); | ||
expect(mockTestMethod).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it('can write a value to an instance setter - Auto-mocked class', () => { | ||
const mockTestMethod = jest | ||
.spyOn(SuperTestClass.prototype, 'testAccessor', 'set') | ||
.mockImplementation((_x: string) => { | ||
return () => {}; | ||
}); | ||
const testClassInstance = new SuperTestClass(); | ||
testClassInstance.testAccessor = ''; | ||
expect(mockTestMethod).toHaveBeenCalledTimes(1); | ||
|
||
mockTestMethod.mockClear(); | ||
}); | ||
|
||
it('can write a value to a superclass instance setter - Auto-mocked class', () => { | ||
const mockTestMethod = jest | ||
.spyOn(TestClass.prototype, 'testAccessor', 'set') | ||
.mockImplementation((_x: string) => { | ||
return () => {}; | ||
}); | ||
const testClassInstance = new TestClass(); | ||
testClassInstance.testAccessor = ''; | ||
expect(mockTestMethod).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it('can read a value from a static getter - Auto-mocked class', () => { | ||
const mockTestMethod = jest | ||
.spyOn(SuperTestClass, 'staticTestAccessor', 'get') | ||
.mockImplementation(() => { | ||
return 'mockStaticTestAccessor'; | ||
}); | ||
expect(SuperTestClass.staticTestAccessor).toEqual('mockStaticTestAccessor'); | ||
expect(mockTestMethod).toHaveBeenCalledTimes(1); | ||
|
||
mockTestMethod.mockClear(); | ||
}); | ||
|
||
it('can read a value from a superclass static getter - Auto-mocked class', () => { | ||
const mockTestMethod = jest | ||
.spyOn(TestClass, 'staticTestAccessor', 'get') | ||
.mockImplementation(() => { | ||
return 'mockStaticTestAccessor'; | ||
}); | ||
expect(TestClass.staticTestAccessor).toEqual('mockStaticTestAccessor'); | ||
expect(mockTestMethod).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it('can write a value to a static setter - Auto-mocked class', () => { | ||
const mockTestMethod = jest | ||
.spyOn(SuperTestClass, 'staticTestAccessor', 'set') | ||
.mockImplementation((_x: string) => { | ||
return () => {}; | ||
}); | ||
SuperTestClass.staticTestAccessor = ''; | ||
expect(mockTestMethod).toHaveBeenCalledTimes(1); | ||
|
||
mockTestMethod.mockClear(); | ||
}); | ||
|
||
it('can write a value to a superclass static setter - Auto-mocked class', () => { | ||
const mockTestMethod = jest | ||
.spyOn(TestClass, 'staticTestAccessor', 'set') | ||
.mockImplementation((_x: string) => { | ||
return () => {}; | ||
}); | ||
TestClass.staticTestAccessor = ''; | ||
expect(mockTestMethod).toHaveBeenCalledTimes(1); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
*/ | ||
|
||
export default class SuperTestClass { | ||
static staticTestProperty = 'staticTestProperty'; | ||
|
||
static get staticTestAccessor(): string { | ||
return 'staticTestAccessor'; | ||
} | ||
|
||
static set staticTestAccessor(_x: string) { | ||
return; | ||
} | ||
|
||
static staticTestMethod(): string { | ||
return 'staticTestMethod'; | ||
} | ||
|
||
testProperty = 'testProperty'; | ||
|
||
get testAccessor(): string { | ||
return 'testAccessor'; | ||
} | ||
set testAccessor(_x: string) { | ||
return; | ||
} | ||
|
||
testMethod(): string { | ||
return 'testMethod'; | ||
} | ||
} | ||
|
||
export class TestClass extends SuperTestClass {} |
Oops, something went wrong.