-
-
Notifications
You must be signed in to change notification settings - Fork 6.4k
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(jest-mock): spyOn should support 0
key in objects (#14077)
#14082
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
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
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
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.
In a way these message (and also some of "Cannot spy on the method property because it is not a function" below) are somewhat misleading. How about reworking the whole check logic like this or so:
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.
E.g.,
spyOn
getters or setters has property existence check:https://github.com/facebook/jest/blob/15af9b3b6f53a4f2f2e39d328eb6ef9f46ba91c4/packages/jest-mock/src/index.ts#L1268-L1270
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.
If you ask me, I have no objections. But the messages you refer to were implemented back in 2018 (#5107) for reasons I'm not aware of, and changing those go beyond the scope of fixing the related bug and this PR, I believe. Feel free to do it in a separate PR though, but please note that the code you suggested will not fix the bug and can possibly create more bugs, because
methodKey==null
is an incorrect way to check for an object key, since it'strue
only forundefined
andnull
, at least according to MDNThere 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.
Not sure I follow this.
methodKey == null
would exclude onlyundefined
andnull
, hence the message'No property name supplied'
is correct. This message does not sound correct if user passed''
orNaN
as amethodKey
. Do I miss something?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.
That message for sure doesn't sound correct, but still it was there for 5 years, and I don't believe I'm in position to decide the way it should be changed.
I personally don't see how
null
and''
are different in this context, for instance, yet getting a message like that would not help me much if I've passed a variable as the second argument (i.e. "supplied" something), which could possibly be any of the above. If I was to make a statement, I'd sayif (!object[methodKey])
is enough to cover all the edge cases here, resulting in the same error, i.e.Method ${methodKey} doesn't exist in the provided object
, assumingspyOn
expects a function. Also I don't believe testing for particular error strings is a good approach in general, at least when they are literal strings instead of shared constants. The aforementioned tests I've implemented do match the existing behaviour, which was not covered before.However, I don't mind putting in a few extra lines of code, but if are willing to take the responsibility for those changes, please provide the exact requirements.
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.
See #14087