-
-
Notifications
You must be signed in to change notification settings - Fork 6.4k
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
feat: Type inference in 'have been called with' parameters #15129
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ | |
|
||
import type {EqualsFunction, Tester} from '@jest/expect-utils'; | ||
import type * as jestMatcherUtils from 'jest-matcher-utils'; | ||
import type {MockInstance} from 'jest-mock'; | ||
import type {INTERNAL_MATCHER_FLAG} from './jestMatchersObject'; | ||
|
||
export type SyncExpectationResult = { | ||
|
@@ -231,16 +232,16 @@ export interface Matchers<R extends void | Promise<void>, T = unknown> { | |
/** | ||
* Ensure that a mock function is called with specific arguments. | ||
*/ | ||
toHaveBeenCalledWith(...expected: Array<unknown>): R; | ||
toHaveBeenCalledWith(...expected: MockParameters<T>): R; | ||
/** | ||
* Ensure that a mock function is called with specific arguments on an Nth call. | ||
*/ | ||
toHaveBeenNthCalledWith(nth: number, ...expected: Array<unknown>): R; | ||
toHaveBeenNthCalledWith(nth: number, ...expected: MockParameters<T>): R; | ||
/** | ||
* If you have a mock function, you can use `.toHaveBeenLastCalledWith` | ||
* to test what arguments it was last called with. | ||
*/ | ||
toHaveBeenLastCalledWith(...expected: Array<unknown>): R; | ||
toHaveBeenLastCalledWith(...expected: MockParameters<T>): R; | ||
/** | ||
* Use to test the specific value that a mock function last returned. | ||
* If the last call to the mock function threw an error, then this matcher will fail | ||
|
@@ -307,3 +308,148 @@ export interface Matchers<R extends void | Promise<void>, T = unknown> { | |
*/ | ||
toThrow(expected?: unknown): R; | ||
} | ||
|
||
/** | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. thanks for the extensive comments 👍 |
||
* Obtains the parameters of the given function or {@link MockInstance}'s function type. | ||
* ```ts | ||
* type P = MockParameters<MockInstance<(foo: number) => void>>; | ||
* // or without an explicit mock | ||
* // type P = MockParameters<(foo: number) => void>; | ||
* | ||
* const params1: P = [1]; // compiles | ||
* const params2: P = ['bar']; // error | ||
* const params3: P = []; // error | ||
* ``` | ||
* | ||
* This is similar to {@link Parameters}, with these notable differences: | ||
* | ||
* 1. Each of the parameters can also accept an {@link AsymmetricMatcher}. | ||
* ```ts | ||
* const params4: P = [expect.anything()]; // compiles | ||
* ``` | ||
* This works with nested types as well: | ||
* ```ts | ||
* type Nested = MockParameters<MockInstance<(foo: { a: number }, bar: [string]) => void>>; | ||
* | ||
* const params1: Nested = [{ foo: { a: 1 }}, ['value']]; // compiles | ||
* const params2: Nested = [expect.anything(), expect.anything()]; // compiles | ||
* const params3: Nested = [{ foo: { a: expect.anything() }}, [expect.anything()]]; // compiles | ||
* ``` | ||
* | ||
* 2. This type works with overloaded functions (up to 15 overloads): | ||
* ```ts | ||
* function overloaded(): void; | ||
* function overloaded(foo: number): void; | ||
* function overloaded(foo: number, bar: string): void; | ||
* function overloaded(foo?: number, bar?: string): void {} | ||
* | ||
* type Overloaded = MockParameters<MockInstance<typeof overloaded>>; | ||
* | ||
* const params1: Overloaded = []; // compiles | ||
* const params2: Overloaded = [1]; // compiles | ||
* const params3: Overloaded = [1, 'value']; // compiles | ||
* const params4: Overloaded = ['value']; // error | ||
* const params5: Overloaded = ['value', 1]; // error | ||
* ``` | ||
* | ||
* Mocks generated with the default `MockInstance` type will evaluate to `Array<unknown>`: | ||
* ```ts | ||
* MockParameters<MockInstance> // Array<unknown> | ||
* ``` | ||
* | ||
* If the given type is not a `MockInstance` nor a function, this type will evaluate to `Array<unknown>`: | ||
* ```ts | ||
* MockParameters<boolean> // Array<unknown> | ||
* ``` | ||
*/ | ||
type MockParameters<M> = | ||
M extends MockInstance<infer F> | ||
? FunctionParameters<F> | ||
: FunctionParameters<M>; | ||
|
||
/** | ||
* A wrapper over `FunctionParametersInternal` which converts `never` evaluations to `Array<unknown>`. | ||
* | ||
* This is only necessary for Typescript versions prior to 5.3. | ||
* | ||
* In those versions, a function without parameters (`() => any`) is interpreted the same as an overloaded function, | ||
* causing `FunctionParametersInternal` to evaluate it to `[] | Array<unknown>`, which is incorrect. | ||
* | ||
* The workaround is to "catch" this edge-case in `WithAsymmetricMatchers` and interpret it as `never`. | ||
* However, this also affects {@link UnknownFunction} (the default generic type of `MockInstance`): | ||
* ```ts | ||
* FunctionParametersInternal<() => any> // [] | never --> [] --> correct | ||
* FunctionParametersInternal<UnknownFunction> // never --> incorrect | ||
* ``` | ||
* An empty array is the expected type for a function without parameters, | ||
* so all that's left is converting `never` to `Array<unknown>` for the case of `UnknownFunction`, | ||
* as it needs to accept _any_ combination of parameters. | ||
*/ | ||
type FunctionParameters<F> = | ||
FunctionParametersInternal<F> extends never | ||
? Array<unknown> | ||
: FunctionParametersInternal<F>; | ||
|
||
/** | ||
* 1. If the function is overloaded or has no parameters -> overloaded form (union of tuples). | ||
* 2. If the function has parameters -> simple form. | ||
* 3. else -> `never`. | ||
*/ | ||
type FunctionParametersInternal<F> = F extends { | ||
(...args: infer P1): any; | ||
(...args: infer P2): any; | ||
(...args: infer P3): any; | ||
(...args: infer P4): any; | ||
(...args: infer P5): any; | ||
(...args: infer P6): any; | ||
(...args: infer P7): any; | ||
(...args: infer P8): any; | ||
(...args: infer P9): any; | ||
(...args: infer P10): any; | ||
(...args: infer P11): any; | ||
(...args: infer P12): any; | ||
(...args: infer P13): any; | ||
(...args: infer P14): any; | ||
(...args: infer P15): any; | ||
} | ||
? | ||
| WithAsymmetricMatchers<P1> | ||
| WithAsymmetricMatchers<P2> | ||
| WithAsymmetricMatchers<P3> | ||
| WithAsymmetricMatchers<P4> | ||
| WithAsymmetricMatchers<P5> | ||
| WithAsymmetricMatchers<P6> | ||
| WithAsymmetricMatchers<P7> | ||
| WithAsymmetricMatchers<P8> | ||
| WithAsymmetricMatchers<P9> | ||
| WithAsymmetricMatchers<P10> | ||
| WithAsymmetricMatchers<P11> | ||
| WithAsymmetricMatchers<P12> | ||
| WithAsymmetricMatchers<P13> | ||
| WithAsymmetricMatchers<P14> | ||
| WithAsymmetricMatchers<P15> | ||
: F extends (...args: infer P) => any | ||
? WithAsymmetricMatchers<P> | ||
: never; | ||
|
||
/** | ||
* @see FunctionParameters | ||
*/ | ||
type WithAsymmetricMatchers<P extends Array<any>> = | ||
Array<unknown> extends P | ||
? never | ||
: {[K in keyof P]: DeepAsymmetricMatcher<P[K]>}; | ||
|
||
/** | ||
* Replaces `T` with `T | AsymmetricMatcher`. | ||
* | ||
* If `T` is an object or an array, recursively replaces all nested types with the same logic: | ||
* ```ts | ||
* type DeepAsymmetricMatcher<boolean>; // AsymmetricMatcher | boolean | ||
* type DeepAsymmetricMatcher<{ foo: number }>; // AsymmetricMatcher | { foo: AsymmetricMatcher | number } | ||
* type DeepAsymmetricMatcher<[string]>; // AsymmetricMatcher | [AsymmetricMatcher | string] | ||
* ``` | ||
*/ | ||
type DeepAsymmetricMatcher<T> = T extends object | ||
? AsymmetricMatcher | {[K in keyof T]: DeepAsymmetricMatcher<T[K]>} | ||
: AsymmetricMatcher | T; |
This file contains 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
This file contains 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
This file contains 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
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why is this a breaking change? the odl type was just
Function
or something?also, only
expect
seems to be impacted? I don't see any changes to@jest/expect
at least 😀There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@SimenB I'm thinking it will likely break some existing tests that use "have been called" functions due to incorrect typing, like it did in tests/examples in this repo. I'm not sure what are the full implications of this "tag", so obviously I will let you decide.
As for
@jest/expect
, I merely copied it from the last PR that tried to introduce this feature. I can obviously remove it.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's just used as a way to catch the reader's eye.
I really should have included a migration guide thing when I started landing breaking changes, where how the breaking change might affect you would be listed 😅 I'll have to add one