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

refactor(jest-mock)!: rework Mocked* utility types #13123

Merged
merged 6 commits into from
Aug 12, 2022
Merged

refactor(jest-mock)!: rework Mocked* utility types #13123

merged 6 commits into from
Aug 12, 2022

Conversation

mrazauskas
Copy link
Contributor

Split from #12727

Summary

As noted in #13117 (comment), jest.mocked() does not work well with classes.

This PR is major refactor of Mocked* types which cleans up the implementation and solves the above mentioned issue.

To clean up exports the Mocked* utility types are renamed. MaybeMockedDeep and MaybeMocked became Mocked and MockedShallow respectively. Also only deep mocked variants of MockedClass, MockedFunction and MockedObject are exported.

Test plan

Lost of type tests added.

export type MockedClass<T extends ClassLike> = MockInstance<
(args: T extends new (...args: infer P) => any ? P : never) => InstanceType<T>
> & {
prototype: T extends {prototype: any} ? Mocked<T['prototype']> : never;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Might be this could be a solution for MockedClassShallow. Although I am in doubt if it is worse to make types more complicated than proposed in this PR.

Comment on lines -50 to -52
// TODO Figure out how to replace this with TS ConstructorParameters utility type
// https://www.typescriptlang.org/docs/handbook/utility-types.html#constructorparameterstype
type ConstructorParameters<T> = T extends new (...args: infer P) => any
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done ;D

@SimenB SimenB merged commit f9718de into jestjs:main Aug 12, 2022
@SimenB
Copy link
Member

SimenB commented Aug 12, 2022

Thanks!! This is awesome

@SimenB
Copy link
Member

SimenB commented Aug 19, 2022

@SimenB
Copy link
Member

SimenB commented Aug 25, 2022

@github-actions
Copy link

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.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Sep 25, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants