-
-
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
[Feature]: Ergonomic way to jest.fn over a function type in TypeScript #12479
Comments
We should definitely have that, at the very least as an overload. @mrazauskas since you've been working on the types in |
Indeed I was looking at this yesterday. Some hour before the issue came in. Might be possible to do and really worth to a try. |
FWIW I'm happy to replace the two generics we have today with a single function one - the types we have are inherited from |
Working on it. Almost there. (Shape it up, just have to figure out a couple of details.) |
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
TypeScript users need a good way to represent that a
jest.fn()
should adhere to a particular function type. I propose its generic type parameters be switched to a single type parameter representing a function type.Motivation
Right now
jest.fn
is generic on aT
for the return type andY
for the args. That means there's no easy way to go from some function type to ajest.fn
of that type.The closest is to use
ReturnType
andParameters
:https://www.typescriptlang.org/play?#code/JYWwDg9gTgLgBAbzgKwKYGd4F84DMoQhwDkAAmpgPQDmANhAEYCGt6xA3AFCcwCeYqOAFleAMQCuAOwDGMYBEkAVfoIC8cABTBJYcTABccTFG3UAlHFUA+OJPEgGqKF07SFmI2F6WUGGADpcSQAeACVUGHEoJRVgkQkZOQVlASsAGjgABSYoJhAIp3Q4sSlZeRjUqw0zLiA
Edit: or a wrapper like...
Example
Pitch
Proper function mock types is going to be an increasingly relevant pain point as ES Modules become more popular.
@types/jest
defaults the generics forjest.fn()
mocks toany
instead ofunknown
, so many users haven't needed to have proper test types till now.The text was updated successfully, but these errors were encountered: