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

perf(types): prevent immediate deep and circular resolution for deep mock #123

Merged
merged 2 commits into from
Aug 3, 2023
Merged
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
37 changes: 25 additions & 12 deletions src/Mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,35 @@ export interface CalledWithMock<T, Y extends any[]> extends jest.Mock<T, Y> {
calledWith: (...args: Y | MatchersOrLiterals<Y>) => jest.Mock<T, Y>;
}

export type MockProxy<T> = {
[K in keyof T]: T[K] extends (...args: infer A) => infer B ? CalledWithMock<B, A> : T[K];
} &
T;
export type _MockProxy<T> = {
[K in keyof T]: T[K] extends (...args: infer A) => infer B
? T[K] & CalledWithMock<B, A>
: T[K];
};

export type MockProxy<T> = _MockProxy<T> & T;

export type DeepMockProxy<T> = {
export type _DeepMockProxy<T> = {
// This supports deep mocks in the else branch
[K in keyof T]: T[K] extends (...args: infer A) => infer B ? CalledWithMock<B, A> : DeepMockProxy<T[K]>;
} &
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This causes TypeScript to expand and immediately compute the type, potentially doing dangerous eager loading on deep and recursive or mutually self-referencing objects - potentially causing perf issues.

T;
[K in keyof T]: T[K] extends (...args: infer A) => infer B
? T[K] & CalledWithMock<B, A>
: T[K] & _DeepMockProxy<T[K]>;
};

export type DeepMockProxyWithFuncPropSupport<T> = {
// we intersect with T here instead of on the mapped type above to
// prevent immediate type resolution on a recursive type, this will
// help to improve performance for deeply nested recursive mocking
// at the same time, this intersection preserves private properties
export type DeepMockProxy<T> = _DeepMockProxy<T> & T;
Copy link
Contributor Author

@millsp millsp Aug 3, 2023

Choose a reason for hiding this comment

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

Suggested change
export type DeepMockProxy<T> = _DeepMockProxy<T> & T;
export type DeepMockProxy<T> = _DeepMockProxy<T> & T;

In theory re-intersecting should not be necessary because we did already in the mapped types. But because the original type can contain private fields, using a mapped type on it erases that information. So the solution is to re-intersect once only.


export type _DeepMockProxyWithFuncPropSupport<T> = {
// This supports deep mocks in the else branch
[K in keyof T]: T[K] extends (...args: infer A) => infer B ? CalledWithMock<B, A> & DeepMockProxy<T[K]> : DeepMockProxy<T[K]>;
} &
T;
[K in keyof T]: T[K] extends (...args: infer A) => infer B
? CalledWithMock<B, A> & DeepMockProxy<T[K]>
: DeepMockProxy<T[K]>;
};

export type DeepMockProxyWithFuncPropSupport<T> = _DeepMockProxyWithFuncPropSupport<T> & T;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I did all of them for consistency.


export interface MockOpts {
deep?: boolean;
Expand Down