|
8 | 8 |
|
9 | 9 | import type {EqualsFunction, Tester} from '@jest/expect-utils';
|
10 | 10 | import type * as jestMatcherUtils from 'jest-matcher-utils';
|
| 11 | +// TODO does this require dependency in package.json? |
| 12 | +import type {Mock} from 'jest-mock'; |
11 | 13 | import type {INTERNAL_MATCHER_FLAG} from './jestMatchersObject';
|
12 | 14 |
|
13 | 15 | export type SyncExpectationResult = {
|
@@ -231,16 +233,16 @@ export interface Matchers<R extends void | Promise<void>, T = unknown> {
|
231 | 233 | /**
|
232 | 234 | * Ensure that a mock function is called with specific arguments.
|
233 | 235 | */
|
234 |
| - toHaveBeenCalledWith(...expected: Array<unknown>): R; |
| 236 | + toHaveBeenCalledWith(...expected: FunctionParameters<T>): R; |
235 | 237 | /**
|
236 | 238 | * Ensure that a mock function is called with specific arguments on an Nth call.
|
237 | 239 | */
|
238 |
| - toHaveBeenNthCalledWith(nth: number, ...expected: Array<unknown>): R; |
| 240 | + toHaveBeenNthCalledWith(nth: number, ...expected: FunctionParameters<T>): R; |
239 | 241 | /**
|
240 | 242 | * If you have a mock function, you can use `.toHaveBeenLastCalledWith`
|
241 | 243 | * to test what arguments it was last called with.
|
242 | 244 | */
|
243 |
| - toHaveBeenLastCalledWith(...expected: Array<unknown>): R; |
| 245 | + toHaveBeenLastCalledWith(...expected: FunctionParameters<T>): R; |
244 | 246 | /**
|
245 | 247 | * Use to test the specific value that a mock function last returned.
|
246 | 248 | * If the last call to the mock function threw an error, then this matcher will fail
|
@@ -307,3 +309,99 @@ export interface Matchers<R extends void | Promise<void>, T = unknown> {
|
307 | 309 | */
|
308 | 310 | toThrow(expected?: unknown): R;
|
309 | 311 | }
|
| 312 | + |
| 313 | +// TODO add more overload options (up to 10?) |
| 314 | +type FunctionParameters<M> = |
| 315 | + M extends Mock<infer F> |
| 316 | + ? F extends OverloadedFunction4 |
| 317 | + ? FunctionParameters4<F> |
| 318 | + : F extends OverloadedFunction3 |
| 319 | + ? FunctionParameters3<F> |
| 320 | + : F extends OverloadedFunction2 |
| 321 | + ? FunctionParameters2<F> |
| 322 | + : F extends NoOverloadsFunction |
| 323 | + ? FunctionParameters1<F> |
| 324 | + : Array<unknown> |
| 325 | + : Array<unknown>; |
| 326 | + |
| 327 | +type NoOverloadsFunction = (...args: any) => any; |
| 328 | + |
| 329 | +type OverloadedFunction2 = { |
| 330 | + (...args: any): any; |
| 331 | + (...args: any): any; |
| 332 | +}; |
| 333 | +type OverloadedFunction3 = { |
| 334 | + (...args: any): any; |
| 335 | + (...args: any): any; |
| 336 | + (...args: any): any; |
| 337 | +}; |
| 338 | +type OverloadedFunction4 = { |
| 339 | + (...args: any): any; |
| 340 | + (...args: any): any; |
| 341 | + (...args: any): any; |
| 342 | + (...args: any): any; |
| 343 | +}; |
| 344 | + |
| 345 | +type WithAsymmetricMatchers<P extends Array<any>> = { |
| 346 | + [K in keyof P]: P[K] | AsymmetricMatcher; |
| 347 | +}; |
| 348 | + |
| 349 | +type FunctionParameters1<F extends NoOverloadsFunction> = F extends ( |
| 350 | + ...args: infer P |
| 351 | +) => any |
| 352 | + ? WithAsymmetricMatchers<P> |
| 353 | + : never; |
| 354 | + |
| 355 | +type FunctionParameters2<F extends OverloadedFunction2> = F extends { |
| 356 | + (...args: infer P1): any; |
| 357 | + (...args: infer P2): any; |
| 358 | +} |
| 359 | + ? WithAsymmetricMatchers<P1> | WithAsymmetricMatchers<P2> |
| 360 | + : never; |
| 361 | + |
| 362 | +type FunctionParameters3<F extends OverloadedFunction2> = F extends { |
| 363 | + (...args: infer P1): any; |
| 364 | + (...args: infer P2): any; |
| 365 | + (...args: infer P3): any; |
| 366 | +} |
| 367 | + ? |
| 368 | + | WithAsymmetricMatchers<P1> |
| 369 | + | WithAsymmetricMatchers<P2> |
| 370 | + | WithAsymmetricMatchers<P3> |
| 371 | + : never; |
| 372 | + |
| 373 | +type FunctionParameters4<F extends OverloadedFunction2> = F extends { |
| 374 | + (...args: infer P1): any; |
| 375 | + (...args: infer P2): any; |
| 376 | + (...args: infer P3): any; |
| 377 | + (...args: infer P4): any; |
| 378 | +} |
| 379 | + ? |
| 380 | + | WithAsymmetricMatchers<P1> |
| 381 | + | WithAsymmetricMatchers<P2> |
| 382 | + | WithAsymmetricMatchers<P3> |
| 383 | + | WithAsymmetricMatchers<P4> |
| 384 | + : never; |
| 385 | + |
| 386 | +// TODO delete |
| 387 | +// const ama: AsymmetricMatchers = null; |
| 388 | +// const x: Mock<(s: string, n: number) => void> = null; |
| 389 | +// const ex: Expect = null; |
| 390 | +// const y = ex(x); |
| 391 | +// y.toHaveBeenCalledWith('s', 1); |
| 392 | +// y.toHaveBeenCalledWith(ama.stringContaining('sd'), 1); |
| 393 | +// y.toHaveBeenCalledWith(); |
| 394 | + |
| 395 | +// function withOverload(): void; |
| 396 | +// function withOverload(n: number): void; |
| 397 | +// function withOverload(n: number, s: string): void; |
| 398 | +// function withOverload(n?: number, s?: string): void {} |
| 399 | +// |
| 400 | +// const pp: FunctionParameters<Mock<typeof withOverload>> = null; |
| 401 | +// |
| 402 | +// const wo: typeof withOverload = null; |
| 403 | +// const x: Mock<typeof withOverload> = null; |
| 404 | +// const ex: Expect = null; |
| 405 | +// const y = ex(x); |
| 406 | +// y.toHaveBeenCalledWith(); |
| 407 | +// y.toHaveBeenCalledWith(1, 's'); |
0 commit comments