|
8 | 8 |
|
9 | 9 | import type {EqualsFunction, Tester} from '@jest/expect-utils';
|
10 | 10 | import type * as jestMatcherUtils from 'jest-matcher-utils';
|
| 11 | +import type {Mock} from 'jest-mock'; |
11 | 12 | import type {INTERNAL_MATCHER_FLAG} from './jestMatchersObject';
|
12 | 13 |
|
13 | 14 | export type SyncExpectationResult = {
|
@@ -231,16 +232,16 @@ export interface Matchers<R extends void | Promise<void>, T = unknown> {
|
231 | 232 | /**
|
232 | 233 | * Ensure that a mock function is called with specific arguments.
|
233 | 234 | */
|
234 |
| - toHaveBeenCalledWith(...expected: Array<unknown>): R; |
| 235 | + toHaveBeenCalledWith(...expected: MockParameters<T>): R; |
235 | 236 | /**
|
236 | 237 | * Ensure that a mock function is called with specific arguments on an Nth call.
|
237 | 238 | */
|
238 |
| - toHaveBeenNthCalledWith(nth: number, ...expected: Array<unknown>): R; |
| 239 | + toHaveBeenNthCalledWith(nth: number, ...expected: MockParameters<T>): R; |
239 | 240 | /**
|
240 | 241 | * If you have a mock function, you can use `.toHaveBeenLastCalledWith`
|
241 | 242 | * to test what arguments it was last called with.
|
242 | 243 | */
|
243 |
| - toHaveBeenLastCalledWith(...expected: Array<unknown>): R; |
| 244 | + toHaveBeenLastCalledWith(...expected: MockParameters<T>): R; |
244 | 245 | /**
|
245 | 246 | * Use to test the specific value that a mock function last returned.
|
246 | 247 | * If the last call to the mock function threw an error, then this matcher will fail
|
@@ -307,3 +308,128 @@ export interface Matchers<R extends void | Promise<void>, T = unknown> {
|
307 | 308 | */
|
308 | 309 | toThrow(expected?: unknown): R;
|
309 | 310 | }
|
| 311 | + |
| 312 | +/** |
| 313 | + * Obtains the parameters of the given {@link Mock}'s function type. |
| 314 | + * ```ts |
| 315 | + * type P = MockParameters<Mock<(foo: number) => void>>; |
| 316 | + * |
| 317 | + * const params1: P = [1]; // compiles |
| 318 | + * const params2: P = ['bar']; // error |
| 319 | + * const params3: P = []; // error |
| 320 | + * ``` |
| 321 | + * |
| 322 | + * This is similar to {@link Parameters}, with these notable differences: |
| 323 | + * |
| 324 | + * 1. Each of the parameters can also accept an {@link AsymmetricMatcher}. |
| 325 | + * ```ts |
| 326 | + * const params4: P = [expect.anything()]; // compiles |
| 327 | + * ``` |
| 328 | + * This works with nested types as well: |
| 329 | + * ```ts |
| 330 | + * type Nested = MockParameters<Mock<(foo: { a: number }, bar: [string]) => void>>; |
| 331 | + * |
| 332 | + * const params1: Nested = [{ foo: { a: 1 }}, ['value']]; // compiles |
| 333 | + * const params2: Nested = [expect.anything(), expect.anything()]; // compiles |
| 334 | + * const params3: Nested = [{ foo: { a: expect.anything() }}, [expect.anything()]]; // compiles |
| 335 | + * ``` |
| 336 | + * |
| 337 | + * 2. This type works with overloaded functions (up to 15 overloads): |
| 338 | + * ```ts |
| 339 | + * function overloaded(): void; |
| 340 | + * function overloaded(foo: number): void; |
| 341 | + * function overloaded(foo: number, bar: string): void; |
| 342 | + * function overloaded(foo?: number, bar?: string): void {} |
| 343 | + * |
| 344 | + * type Overloaded = MockParameters<Mock<typeof overloaded>>; |
| 345 | + * |
| 346 | + * const params1: Overloaded = []; // compiles |
| 347 | + * const params2: Overloaded = [1]; // compiles |
| 348 | + * const params3: Overloaded = [1, 'value']; // compiles |
| 349 | + * const params4: Overloaded = ['value']; // error |
| 350 | + * const params5: Overloaded = ['value', 1]; // error |
| 351 | + * ``` |
| 352 | + * |
| 353 | + * Mocks generated with the default `Mock` type will evaluate to `Array<unknown>`: |
| 354 | + * ```ts |
| 355 | + * MockParameters<Mock> // Array<unknown> |
| 356 | + * ``` |
| 357 | + * |
| 358 | + * If the given type is not a `Mock`, this type will evaluate to `Array<unknown>`: |
| 359 | + * ```ts |
| 360 | + * MockParameters<() => void> // Array<unknown> |
| 361 | + * ``` |
| 362 | + */ |
| 363 | +type MockParameters<M> = |
| 364 | + MockParametersInternal<M> extends never |
| 365 | + ? Array<unknown> |
| 366 | + : MockParametersInternal<M>; |
| 367 | + |
| 368 | +/** |
| 369 | + * 1. If `M` is not a `Mock` -> `never`. |
| 370 | + * 2. If the mock function is overloaded or has no parameters -> overloaded form (union of tuples). |
| 371 | + * 3. If the mock function has parameters -> simple form. |
| 372 | + * 4. else -> `never`. |
| 373 | + */ |
| 374 | +type MockParametersInternal<M> = |
| 375 | + M extends Mock<infer F> |
| 376 | + ? F extends { |
| 377 | + (...args: infer P1): any; |
| 378 | + (...args: infer P2): any; |
| 379 | + (...args: infer P3): any; |
| 380 | + (...args: infer P4): any; |
| 381 | + (...args: infer P5): any; |
| 382 | + (...args: infer P6): any; |
| 383 | + (...args: infer P7): any; |
| 384 | + (...args: infer P8): any; |
| 385 | + (...args: infer P9): any; |
| 386 | + (...args: infer P10): any; |
| 387 | + (...args: infer P11): any; |
| 388 | + (...args: infer P12): any; |
| 389 | + (...args: infer P13): any; |
| 390 | + (...args: infer P14): any; |
| 391 | + (...args: infer P15): any; |
| 392 | + } |
| 393 | + ? |
| 394 | + | WithAsymmetricMatchers<P1> |
| 395 | + | WithAsymmetricMatchers<P2> |
| 396 | + | WithAsymmetricMatchers<P3> |
| 397 | + | WithAsymmetricMatchers<P4> |
| 398 | + | WithAsymmetricMatchers<P5> |
| 399 | + | WithAsymmetricMatchers<P6> |
| 400 | + | WithAsymmetricMatchers<P7> |
| 401 | + | WithAsymmetricMatchers<P8> |
| 402 | + | WithAsymmetricMatchers<P9> |
| 403 | + | WithAsymmetricMatchers<P10> |
| 404 | + | WithAsymmetricMatchers<P11> |
| 405 | + | WithAsymmetricMatchers<P12> |
| 406 | + | WithAsymmetricMatchers<P13> |
| 407 | + | WithAsymmetricMatchers<P14> |
| 408 | + | WithAsymmetricMatchers<P15> |
| 409 | + : F extends (...args: infer P) => any |
| 410 | + ? WithAsymmetricMatchers<P> |
| 411 | + : never |
| 412 | + : never; |
| 413 | + |
| 414 | +/** |
| 415 | + * The condition here "catches" the parameters of the default `Mock` type ({@link UnknownFunction}), |
| 416 | + * evaluating to `never`, and later "wrapped" by `MockParameters` and finally evaluates to `Array<unknown>`. |
| 417 | + */ |
| 418 | +type WithAsymmetricMatchers<P extends Array<any>> = |
| 419 | + Array<unknown> extends P |
| 420 | + ? never |
| 421 | + : {[K in keyof P]: DeepAsymmetricMatcher<P[K]>}; |
| 422 | + |
| 423 | +/** |
| 424 | + * Replaces `T` with `T | AsymmetricMatcher`. |
| 425 | + * |
| 426 | + * If `T` is an object or an array, recursively replaces all nested types with the same logic: |
| 427 | + * ```ts |
| 428 | + * type DeepAsymmetricMatcher<boolean>; // AsymmetricMatcher | boolean |
| 429 | + * type DeepAsymmetricMatcher<{ foo: number }>; // AsymmetricMatcher | { foo: AsymmetricMatcher | number } |
| 430 | + * type DeepAsymmetricMatcher<[string]>; // AsymmetricMatcher | [AsymmetricMatcher | string] |
| 431 | + * ``` |
| 432 | + */ |
| 433 | +type DeepAsymmetricMatcher<T> = T extends object |
| 434 | + ? AsymmetricMatcher | {[K in keyof T]: DeepAsymmetricMatcher<T[K]>} |
| 435 | + : AsymmetricMatcher | T; |
0 commit comments