Skip to content

Commit

Permalink
refactor: use idea from pr jestjs#13278
Browse files Browse the repository at this point in the history
  • Loading branch information
royhadad committed Sep 18, 2022
1 parent e73f878 commit 4080950
Showing 1 changed file with 10 additions and 13 deletions.
23 changes: 10 additions & 13 deletions packages/expect/src/types.ts
Expand Up @@ -118,13 +118,6 @@ export interface AsymmetricMatchers {
stringMatching(sample: string | RegExp): AsymmetricMatcher;
}

// if T is a function, return its parameters array type, otherwise return an unknown array type
type ConditionalFunctionParameters<T> = T extends (
...args: Array<unknown>
) => unknown
? Parameters<T>
: Array<unknown>;

type PromiseMatchers<T = unknown> = {
/**
* Unwraps the reason of a rejected promise so any other matcher can be chained.
Expand All @@ -138,19 +131,23 @@ type PromiseMatchers<T = unknown> = {
resolves: Matchers<Promise<void>> & Inverse<Matchers<Promise<void>, T>>;
};

type EnsureFunctionLike<T> = T extends (...args: Array<unknown>) => unknown
? T
: never;

export interface Matchers<R extends void | Promise<void>, T = unknown> {
/**
* Ensures the last call to a mock function was provided specific args.
*/
lastCalledWith(...expected: ConditionalFunctionParameters<T>): R;
lastCalledWith(...expected: Parameters<EnsureFunctionLike<T>>): R;
/**
* Ensure that the last call to a mock function has returned a specified value.
*/
lastReturnedWith(expected: unknown): R;
/**
* Ensure that a mock function is called with specific arguments on an Nth call.
*/
nthCalledWith(nth: number, ...expected: ConditionalFunctionParameters<T>): R;
nthCalledWith(nth: number, ...expected: Parameters<EnsureFunctionLike<T>>): R;
/**
* Ensure that the nth call to a mock function has returned a specified value.
*/
Expand All @@ -171,7 +168,7 @@ export interface Matchers<R extends void | Promise<void>, T = unknown> {
/**
* Ensure that a mock function is called with specific arguments.
*/
toBeCalledWith(...expected: ConditionalFunctionParameters<T>): R;
toBeCalledWith(...expected: Parameters<EnsureFunctionLike<T>>): R;
/**
* Using exact equality with floating point numbers is a bad idea.
* Rounding means that intuitive things fail.
Expand Down Expand Up @@ -254,19 +251,19 @@ export interface Matchers<R extends void | Promise<void>, T = unknown> {
/**
* Ensure that a mock function is called with specific arguments.
*/
toHaveBeenCalledWith(...expected: ConditionalFunctionParameters<T>): R;
toHaveBeenCalledWith(...expected: Parameters<EnsureFunctionLike<T>>): R;
/**
* Ensure that a mock function is called with specific arguments on an Nth call.
*/
toHaveBeenNthCalledWith(
nth: number,
...expected: ConditionalFunctionParameters<T>
...expected: Parameters<EnsureFunctionLike<T>>
): R;
/**
* If you have a mock function, you can use `.toHaveBeenLastCalledWith`
* to test what arguments it was last called with.
*/
toHaveBeenLastCalledWith(...expected: ConditionalFunctionParameters<T>): R;
toHaveBeenLastCalledWith(...expected: Parameters<EnsureFunctionLike<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
Expand Down

0 comments on commit 4080950

Please sign in to comment.