-
-
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
Proposal for jest.mock to assist in typescript environments #10948
Comments
We cannot change Also, Thirdly, there is no way in TypeScript to infer shape of an import except via All that is to say there'd still be some boilerplate and we'd need a new API in order to implement this. Not blockers of course, but a significant effort. The const fetch = mockAndRequire<typeof import('node-fetch')>('node-fetch');
function mockAndRequire<T = unknown>(moduleToImport: string): T {
jest.mock(moduleToImport)
const importedModule = require(moduleToImport);
return mocked(importedModule, true)
} or for ESM (though blocked on #10025 for now) const fetch = await mockAndImport<typeof import('node-fetch')>('node-fetch');
async function mockAndImport<T = unknown>(moduleToImport: string): Promise<T> {
jest.mockModule(moduleToImport)
const importedModule = await import(moduleToImport);
return mocked(importedModule, true)
} Still some boilerplate (you have to say |
I am successfully using https://github.com/userlike/joke for "mock and import" behaviour. In a recent version, they actually use the "mocked" helper mentioned above for typings, so far, I have not encountered any problems with the types whatsoever. |
This issue is stale because it has been open for 1 year with no activity. Remove stale label or comment or this will be closed in 14 days. |
This issue was closed because it has been stalled for 7 days with no activity. Please open a new issue if the issue is still relevant, linking to this one. |
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
🚀 Feature Proposal
It would be super helpful if jest.mock could return the module (maybe with updated typings).
Motivation
Right now, when writing unit tests with jest and typescript, mocking modules becomes boilerplatey. This is because the IDE does not understand that after calling
jest.mock("myModule")
all exported functions of that module are of typejest.Mock
. Therefore, when I try to call methods likemyModule.mockReturnValue()
or the likes, the IDE / typescript complains that this is nonsense - those functions do not exist on the module/function.In order to get correct typings, one has to import a mocked module like this:
Example
My idea is to let
jest.mock
return an instance of the mocked module - in the best case with correct typings. So the whole boilerplate could be reduced to:Even if its not possible to add the correct typings directly in jest, it would make re-assigning mock types much easier:
Or when importing just some features of a module:
Thats possibly a problem since I saw that jest.mock currently returns an instance of jest for method chaining. So changing the behaviour would possibly break some peoples unit tests. Adding a property to the config argument could change that or maybe introducing a completely new method that combines mock + require.
The text was updated successfully, but these errors were encountered: