-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
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
feat(@jest/environment, jest-runtime): allow passing a generic type argument to jest.createMockFromModule<T>()
method
#13202
feat(@jest/environment, jest-runtime): allow passing a generic type argument to jest.createMockFromModule<T>()
method
#13202
Conversation
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.
looks good, but needs to be compatible with the @types/jest
types
I disagree about that part - source of truth is this repo. Unless |
createMockFromModule()
type with its behaviourjest.createMockFromModule<T>()
method to take a generic type argument
expectType<Mocked<typeof someModule>>( | ||
jest.createMockFromModule<typeof someModule>('moduleName'), | ||
); |
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.
Just a minimal test, because the serious ones landed in #13207
jest.createMockFromModule<T>()
method to take a generic type argumentjest.createMockFromModule<T>()
method
private readonly _mockMetaDataCache: Map<string, MockFunctionMetadata>; | ||
private readonly _mockMetaDataCache: Map<string, MockMetadata<any>>; | ||
private _mockRegistry: Map<string, any>; | ||
private _isolatedMockRegistry: Map<string, any> | null; |
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.
Ideally these three any
s should be some T
(a type of some mock module). unknown
does not work unfortunately, because T
cannot be assigned to unknown
. The T
could be assigned to T
, but there is no way to have it here. Tricky indeed.
At the same time, here the shape of the mock is not important at all. Hence any
looked fine. Hm.. I will try one more time.
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.
Nope. No luck. It might make sense to create type MockModule = any
and to use it here instead of any
. Looks redundant, but perhaps that way this is more clear?
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.
nah, current approach is fine 👍
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.
great!
@viceice The PR has landed, but I do understand it did not solve your issue. Now someone should make similar change in They have By the way, you can also explicitly import all APIs from |
yeah, i think I'll remove that dependency too and manually declare the required globals until we refactored the whole solution (more that 1000 tests in hundreds of files) |
That should work too. If you will find something missing or incorrect, please open an issue. In general Jest built-in types are in very good shape. Some niche features like By the way, it might be tricky to have utility types like |
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. |
Summary
Closes #13201
Resolves #13199
As it was noticed in #13199 currently
jest.createMockFromModule()
returnsunknown
type. This does not align neither with@jest/types
, nor with the actual behaviour of the method.The PR adds a possibility to pass a generic type argument
T
to the method. This way the returned value can be typed asMocked<T>
which matches the shape of the value.Test plan
A type test is added.