-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: mocking of getters/setters on automatically mocked classes #13398
Merged
Merged
Changes from 4 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
8d31725
Revert "Revert "fix: mocking of getters/setters on automatically mock…
staplespeter 46c92d6
bug 13140 - Added condition to prevent refs to auto-mocked superclass…
staplespeter 571ad48
Merge branch 'main' into bug-13140
staplespeter d315f78
bug 13140 - Lint changes
staplespeter 6c3af0d
bug 13140 - requested repo reorg
staplespeter c36a196
Update jest.config.mjs
staplespeter e7e4242
Update CHANGELOG.md
SimenB 9be113c
Merge branch 'main' into bug-13140
SimenB File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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()).toBe('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()).toBe('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).toBe('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).toBe('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).toBe('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).toBe('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); | ||
}); | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Minor detail. Could you move these files to
/packages/jest-mock/src/__tests__/__fixtures__/
? Recently I added/__fixtures__/
(see above) to this list to make it less noisy.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, though I cannot see the
/packages/jest-mock/src/__tests__/__fixtures__/
folder. Thepackages/jest-mock/__typetests__/__fixtures__/
folder does not exist either though I would have expected this to be a candidate for this reorg. Have I missed a commit/made a bad merge?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is no folder at the moment. Just create it (;
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was trying to say that if you create
src/__tests__/__fixtures__/
folder it will get ignored, because__fixtures__
was recently added to the ignore list. All would work without tweaking Jest config. That was the idea.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Apologies, I interpreted your initial comment as being that you had already done so, somewhere.