-
Notifications
You must be signed in to change notification settings - Fork 56
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -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]>; | ||||||
} & | ||||||
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; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
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; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I did all of them for consistency. |
||||||
|
||||||
export interface MockOpts { | ||||||
deep?: boolean; | ||||||
|
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.
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.