-
-
Notifications
You must be signed in to change notification settings - Fork 6.6k
fix(jest-mock): improve spyOn typings to handle optional properties
#13247
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
Conversation
| spyOn< | ||
| T extends object, | ||
| K extends ConstructorLikeKeys<Required<T>>, | ||
| V extends Required<T>[K], |
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.
V is just a helper to avoid SpyInstance<(...args: ConstructorParameters<Required<T>[K]>) => InstanceType<Required<T>[K]>>
| spyOn<T extends object, K extends PropertyLikeKeys<T>>( | ||
| object: T, | ||
| methodName: M, | ||
| methodKey: K, |
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.
methodKey and K are just renames for consistency. MethodLikeKeys, PropertyLikeKeys, methodKey, propertyKey, K – always keys. Easier to understand what is happening.
| spyOn< | ||
| T extends object, | ||
| K extends MethodLikeKeys<Required<T>>, | ||
| V extends Required<T>[K], |
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.
Lines with Required do all the magic, because they turn types like (() => void)) | undefined into () => void). Something what wasn’t a function, becomes one. This part was not working, otherwise logic is the same.
| private _spyOnProperty<T extends object, K extends PropertyLikeKeys<T>>( | ||
| obj: T, | ||
| propertyName: M, | ||
| accessType: 'get' | 'set' = 'get', |
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.
Default is unnecessary, accessType is always passed:
SimenB
left a comment
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.
Nice!
|
Interestingly same things here DefinitelyTyped/DefinitelyTyped#62208 fails to compile on TS 4.4. But in Jest repo all is fine on TS 4.3. I always have a feeling that to go through CI on DT is simply luck ;D |
|
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. |
Summary
Apparently
spyOnoverloads do not work properly if an interfaces with optional props is encountered:So this got fixed.
Test plan
Type tests are added.