-
Notifications
You must be signed in to change notification settings - Fork 29.9k
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
[@types/jest] spyOn Argument is not assignable to parameter of type #33173
Comments
why can't you just use? let defaultProps: PopoverProps = {
onOpenChange: jest.fn(),
}; |
In 23, |
Well, what about this? (if i understood your type needs correctly): let defaultProps: jest.Mocked<PopoverProps> = {
onOpenChange: jest.fn(),
};
defaultProps.onOpenChange.mock // typed mock, i.e. jest.MockInstance<void, [boolean]> |
I don't disagree that there are ways to work around the issue I think the purpose of me opening this bug is to determine why the specific typing of |
I'll look at it. Seems weird to me that it's working with objects but not interfaces. Maybe it's a TypeScript bug. |
Digging into this it seems the issue is that the type is optional which is causing the "extends" functionality of which determines which spyOn to use to not work. What is weird about this specific case is if you go through a generic (e.g. ArgsType) it works successfully but attempting to pick out the types directly doesn't. For example:
Hopefully this helps when investigating... Fairly sure I'm at the length of my typescript knowledge now :-) |
The optional field makes the conditional on array bug. function test<T extends {}, M extends keyof T>(object: T, method: M): T[M] extends (...args: any) => any ? T[M] : never;
export interface PopoverProps {
wrapperElementProps?: any;
onOpenChange?: (isOpen: boolean) => void;
}
let defaultProps: PopoverProps = {
onOpenChange: () => {}
};
test(defaultProps, 'onOpenChange') // returns never instead of (isOpen: boolean) => void |
This seems to work. I'll add more tests. function spyOn<T extends {}, Y extends Required<T>, M extends FunctionPropertyNames<Y>>(object: T, method: M): Y[M] extends (...args: any[]) => any ? SpyInstance<ReturnType<Y[M]>, ArgsType<Y[M]>> : never; |
@antoinebrault : 👍 Thank you for fixing this issue. If possible, could you please update the |
I'm getting something similar to this when doing: import * as reactNavigation from '@react-navigation/core';
// ...
const useRouter = jest.spyOn(reactNavigation, 'useRouter'); It results in:
I'm using According to #33214 (comment) @antoinebrault merged a fix in version I would be really glad if anyone could tell my why this is happening (again?) because the only solution I see right now is using Edit:Even if I do: // FIXME: remove ts-ignore
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
const useRouter = jest.spyOn(reactNavigation, 'useRouter'); this wont work: useRouter.mockReturnValue({key: '', name: '', params: {/* ... */}}); because it thinks that it's assigning the mocked return value to Edit 2:While not as nice as import * as reactNavigation from '@react-navigation/core';
jest.mock('@react-navigation/core', () => ({
...(jest.requireActual('@react-navigation/core') as Record<string, unknown>),
useRoute: jest.fn(),
}));
(reactNavigation.useRoute as jest.Mock).mockReturnValue({
...jest.requireActual('@react-navigation/core').useRoute,
params: {/* ... */},
}); |
This works for me jest.spyOn<MyComponent, any>(component,'myMethod'); But I haven't been able to fix the any. |
@RamonSchmitt
To avoid using any. |
@types/jest
package and had problems. (Version 24.0.6)Since @types/jest@24.0.* has been released I've been running into a typing issue where
spyOn
can't seem to pick up the correct property out of the object that's being spied on. Here is a short piece of sample code:Which results in the following error:
I think the issue is around
type ArgsType<T> = T extends (...args: infer A) => any ? A : never;
but that just maybe me projecting my lack of knowledge around howinfer
actually works.The text was updated successfully, but these errors were encountered: