Skip to content
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

[jest] spyOn index signatures support #60971

Merged
merged 2 commits into from
Jun 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions types/jest/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -350,8 +350,8 @@ declare namespace jest {
function spyOn<T extends {}, M extends FunctionPropertyNames<Required<T>>>(
object: T,
method: M
): Required<T>[M] extends (...args: any[]) => any
? SpyInstance<ReturnType<Required<T>[M]>, ArgsType<Required<T>[M]>>
): FunctionProperties<Required<T>>[M] extends Func
? SpyInstance<ReturnType<FunctionProperties<Required<T>>[M]>, ArgsType<FunctionProperties<Required<T>>[M]>>
: never;
function spyOn<T extends {}, M extends ConstructorPropertyNames<Required<T>>>(
object: T,
Expand Down Expand Up @@ -413,13 +413,12 @@ declare namespace jest {
type RejectedValue<T> = T extends PromiseLike<any> ? any : never;
type ResolvedValue<T> = T extends PromiseLike<infer U> ? U | T : never;
// see https://github.com/Microsoft/TypeScript/issues/25215
type NonFunctionPropertyNames<T> = { [K in keyof T]: T[K] extends Func ? never : K }[keyof T] &
string;
type NonFunctionPropertyNames<T> = keyof { [K in keyof T as T[K] extends Func ? never : K]: T[K]; };
type GetAccessor = 'get';
type SetAccessor = 'set';
type PropertyAccessors<M extends keyof T, T extends {}> = M extends NonFunctionPropertyNames<Required<T>> ? GetAccessor | SetAccessor : never;
type FunctionPropertyNames<T> = { [K in keyof T]: T[K] extends Func ? K : never }[keyof T] &
string;
type FunctionProperties<T> = { [K in keyof T as T[K] extends (...args: any[]) => any ? K : never]: T[K] };
type FunctionPropertyNames<T> = keyof FunctionProperties<T>;
type ConstructorPropertyNames<T> = { [K in keyof T]: T[K] extends Constructor ? K : never }[keyof T] &
string;

Expand Down
24 changes: 24 additions & 0 deletions types/jest/jest-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,30 @@ class SpyableClass {
// $ExpectType SpyInstance<SpyableClass, [number, string]> || SpyInstance<SpyableClass, [a: number, b: string]>
jest.spyOn({ SpyableClass }, "SpyableClass");

interface SpyableWithIndexSignature {
[index: string]: {
[x: string]: any;
};
prop: { some: string };
methodOne: () => void;
methodTwo: (s: string, b: boolean) => { b: boolean; n: number };
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
}
methodTwo: (s: string, b: boolean) => { b: boolean; n: number };
}

It would be useful to add another test for a method with arguments and a non-void return value

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added in c8f0ac1 (#60971), thanks for review!

let spyWithIndexSignatureImpl: SpyableWithIndexSignature = {
methodOne: () => {},
methodTwo: (s, b) => ({ b, n: Number(s) }),
prop: { some: 'thing' },
};
// $ExpectType SpyInstance<void, []>
jest.spyOn(spyWithIndexSignatureImpl, "methodOne");
// $ExpectType SpyInstance<{ b: boolean; n: number; }, [s: string, b: boolean]>
jest.spyOn(spyWithIndexSignatureImpl, "methodTwo");
// @ts-expect-error
jest.spyOn(spyWithIndexSignatureImpl, "nonExistentMethod");
// @ts-expect-error
jest.spyOn(spyWithIndexSignatureImpl, "prop");
// $ExpectType SpyInstance<{ some: string; }, []>
jest.spyOn(spyWithIndexSignatureImpl, 'prop', 'get');

// $ExpectType MockedObject<{}>
jest.mocked({});
// @ts-expect-error
Expand Down