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#13202SimenB merged 17 commits intojestjs:mainfrom mrazauskas:feat-createMockFromModule-return-type
jest.createMockFromModule<T>() method#13202Conversation
viceice
left a comment
There was a problem hiding this comment.
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.
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 _mockFactories: Map<string, () => unknown>; | ||
| 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.
Ideally these three anys 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.
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?
|
@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()returnsunknowntype. 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
Tto 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.