fix(jest-mock): improve user input validation and error messages of spyOn and replaceProperty methods#14087
Merged
SimenB merged 4 commits intojestjs:mainfrom Apr 20, 2023
mrazauskas:mock-validation-and-messages
Merged
fix(jest-mock): improve user input validation and error messages of spyOn and replaceProperty methods#14087SimenB merged 4 commits intojestjs:mainfrom mrazauskas:mock-validation-and-messages
spyOn and replaceProperty methods#14087SimenB merged 4 commits intojestjs:mainfrom
mrazauskas:mock-validation-and-messages
Conversation
…spyOn` and `replaceProperty` methods
mrazauskas
commented
Apr 19, 2023
Comment on lines
1788
to
+1789
| moduleMocker.spyOn(null, 'method'); | ||
| }).toThrow('spyOn could not find an object to spy on for method'); | ||
| }).toThrow('Cannot use spyOn on a primitive value; null given'); |
Contributor
Author
There was a problem hiding this comment.
null should be treated as primitive. Or?
Comment on lines
1791
to
+1792
| moduleMocker.spyOn({}, 'method'); | ||
| }).toThrow( | ||
| "Cannot spy on the method property because it is not a function; undefined given instead. If you are trying to mock a property, use `jest.replaceProperty(object, 'method', value)` instead.", | ||
| ); | ||
| }).toThrow('Property `method` does not exist in the provided object'); |
Contributor
Author
There was a problem hiding this comment.
Hm.. It does not exist simply.
| ${Symbol()} | ${'symbol'} | ||
| ${true} | ${'boolean'} | ||
| ${false} | ${'boolean'} | ||
| ${() => {}} | ${'function'} |
Contributor
Author
There was a problem hiding this comment.
A function can have properties. Removed this case and moved null and undefined here from the above test.
| ); | ||
| } | ||
|
|
||
| if (!object) { |
Contributor
Author
There was a problem hiding this comment.
null is handled above. All what is possible here are objects and function. Looks like this check is not needed.
spyOn and replaceProperty methodsspyOn and replaceProperty methods
DmitryMakhnev
pushed a commit
to DmitryMakhnev/jest
that referenced
this pull request
May 5, 2023
…spyOn` and `replaceProperty` methods (jestjs#14087)
|
This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
Member
This file contains hidden or 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
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
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.
Fixes #14077
Closes #14082
Summary
Currently validation of user input and error messages differs between
spyOn,spyOnfor get/set accessors andreplacePropertymethods. Some message are somewhat incorrect (see tests). This PR cleans up the validation logic and error messages.Test plan
Unit tests added.