-
-
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]: jest-mock/spyOn property name check is "truthy" instead of "defined" or "existent" #14077
Comments
Just open a PR to fix this. I guess the check should be: if (methodKey == null) {
throw new Error('No property name supplied');
} |
that would actually not fix the issue but would rather fail the check when empty string is passed as method name of many options to exclude all numbers that are converted to string |
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
29.5
Steps to reproduce
Minimal Typescript example with real-world use case
Without Typescript it's still an issue, though the pattern is not that common in human-written code
Expected behavior
I expect any existing object property (that is a
Function
) should be available for mocking and spying0 is perfectly valid object key, and can be used interchangeably with "0" due to javascript native type coercion, so I'd expect mocking out methods, referenced by those keys, should be possible. In typescript I'd expect it to work too, without any typecasting or
//@ts-ignore
, due to the nature of static type checking. That is fixed by using string Enums or starting them explicitly with 1, but that might not be the desired behaviour in some cases.Let alone I could as well use negative numbers as keys, which would also fail to work with
spyOn
Actual behavior
Pretty expectedly and non-positive integer keys in objects can't be mocked due to the way the property name check is done:
which is too broad of a restriction, and I'd further argue that the check like this is even redundant, 'cause few lines below there's a dereference with a sort of property existence check, which could be done in the first place
Additional context
No response
Environment
The text was updated successfully, but these errors were encountered: