fix(expect, @jest/expect): do not infer argument types of *CalledWith and *ReturnedWith matchers - #13339
Merged
Conversation
*CalledWith and *ReturnedWith matchers*CalledWith and *ReturnedWith matchers
SimenB
reviewed
Sep 30, 2022
| expectType<void>(expect(jest.fn()).toBeCalledWith()); | ||
| expectType<void>(expect(jest.fn()).toBeCalledWith('value')); | ||
| expectType<void>(expect(jest.fn()).toBeCalledWith('value', 123)); | ||
| expectType<void>( |
Member
There was a problem hiding this comment.
great you've added tests that catch this 👍
Contributor
Author
There was a problem hiding this comment.
I was sure you wouldn’t accept this PR without these tests (;
|
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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #13337
Summary
Unfortunately #13268 and #13278 have to be reverted. As reported, type inference does not work with function overloads. I have to add that it also does not work with asymmetric matchers.
The asymmetric matchers return
AsymmetricMatchertype. To make the inference work with an argument of[a: string]type, it is needed to remap it into[a: string | AsymmetricMatcher]. Could be possible with something like:A tuple should be broken into elements and turned into remapped tuple. And this is the hard part:
The optional args are simply discarded during inference. That is because TypeScript assumes that the optional args are simply not there. It could be there or it could not in a tuple type, but if a type of tuple member is turned into a type of a variable? There isn’t such a thing as an optional variable. That’s why the
Remappedtype has no chance to work.I couldn’t find a solution. Perhaps it makes sense to revert it for now, because types are obviously buggy.
Test plan
Type tests with asymmetric matchers are added for the future. These are failing on
main.