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

[Feature]: support type inference for function parameters expect(someFunction).toBeCalledWith() #13267

Closed
royhadad opened this issue Sep 15, 2022 · 4 comments · Fixed by #13268

Comments

@royhadad
Copy link
Contributor

🚀 Feature Proposal

When using the following code:

expect(someFunction).toBeCalledWith()

The type expected as arguments for toBeCalledWith is any[]
This can be overridden by passing a type argument:

expect(someFunction).toBeCalledWith<Parameters<someFunction>>()

My proposal is to have it inferred automatically by default.
https://www.typescriptlang.org/docs/handbook/utility-types.html#parameterstype

Similar methods should also be updated (e.g. toHaveBeenCalledWith, lastCalledWith)

Motivation

When using toBeCalledWith, users will get instant feedback and auto-completion for the arguments passed.

Example

No response

Pitch

It will improve the DX of using jest.

@SimenB
Copy link
Member

SimenB commented Sep 15, 2022

@mrazauskas can we do something clever here?

@mrazauskas
Copy link
Contributor

Good idea. I think it should be possible to type expected args in a similar fashion how propertyMatchers are typed here:

https://github.com/facebook/jest/blob/e703e6ed1861595d56e25a764642da5a32b32c5a/packages/jest-snapshot/src/types.ts#L38-L39

@royhadad
Copy link
Contributor Author

@mrazauskas @SimenB
I just opened a pull request to add this behavior, any feedback?

@github-actions
Copy link

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.
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 Oct 21, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants