-
-
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
[Bug]: Mocking of getters/setters on automatically mocked classes does not work #13140
Comments
I have defined an class with a getter objectName in a superclass.
This is what the debugger sees at this point (undefined), which is the same as what the executing code sees.
And this is what Node outputs directly for the type, rather than for the retrieved prototypes.
So I flattened out the class hierarchy and now the objectName getter is in the subclass and there is no superclass. Now the property is retrievable.
I don't know enough to guess why the descriptor resolves to undefined when further up the prototype hierarchy, e.g. if Jest is obtaining type information in an unusual way, or if the V8 engine doesn't store type information properly. I couldn't see anything other than standard Object methods for retrieving type info in the Jest code. |
So there are several issues with the coding for getter methods (this will likely effect setters too). Firstly, in /jest-mock/build/index.js the code that should add the named getter method explicitly ignores it if there is a get property defined (which there is of course). This will not effect setter only properties, at this point anyway.
Once the properties to mock have been retrieved they are identified and the mock implementation assigned.
If, however, the property descriptor were retrieved and it's get property tested instead then the getter would be identified correctly.
The function still needs successfully mocked so there may be other barriers I haven't reached yet. It does not seem that the documented feature of mocking getters/setters https://jestjs.io/docs/es6-class-mocks#static-getter-and-setter-methods is implemented at all. |
Did you try opening a Pull Request? |
I have now issued a pull request for this change |
This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 30 days. |
Hi Tom,
Sorry, new to collaborating, had cloned the Facebook/jest repo directly.
Have now copied it and will issue a pull request from that when I've
finished working through the remaining issues. Thanks for reaching out.
Regards,
Peter
…On Tue, 16 Aug 2022, 14:25 Tom Mrazauskas, ***@***.***> wrote:
Made some changes but have no access to push.
Did you try opening a Pull Request
<https://github.com/facebook/jest/blob/main/CONTRIBUTING.md#workflow-and-pull-requests>
?
—
Reply to this email directly, view it on GitHub
<#13140 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AGJV4SZKVWQYWZU3ICCRRD3VZOJCXANCNFSM56TMP2ZA>
.
You are receiving this because you authored the thread.Message ID:
***@***.***>
|
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
Version
28.1.3
Steps to reproduce
Follow the instructions in the guide for automatic mocks and getters/setters.
https://jestjs.io/docs/es6-class-mocks#automatic-mock
Expected behavior
Expect the mock to be created without error
Actual behavior
Error "property does not exist"
Additional context
related to #9675
Currently fixing this issue. Will provide a better bug report in due course.
Environment
The text was updated successfully, but these errors were encountered: