Skip to content

Commit 4080950

Browse files
author
Roy Hadad
committed
refactor: use idea from pr jestjs#13278
1 parent e73f878 commit 4080950

1 file changed

Lines changed: 10 additions & 13 deletions

File tree

packages/expect/src/types.ts

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -118,13 +118,6 @@ export interface AsymmetricMatchers {
118118
stringMatching(sample: string | RegExp): AsymmetricMatcher;
119119
}
120120

121-
// if T is a function, return its parameters array type, otherwise return an unknown array type
122-
type ConditionalFunctionParameters<T> = T extends (
123-
...args: Array<unknown>
124-
) => unknown
125-
? Parameters<T>
126-
: Array<unknown>;
127-
128121
type PromiseMatchers<T = unknown> = {
129122
/**
130123
* Unwraps the reason of a rejected promise so any other matcher can be chained.
@@ -138,19 +131,23 @@ type PromiseMatchers<T = unknown> = {
138131
resolves: Matchers<Promise<void>> & Inverse<Matchers<Promise<void>, T>>;
139132
};
140133

134+
type EnsureFunctionLike<T> = T extends (...args: Array<unknown>) => unknown
135+
? T
136+
: never;
137+
141138
export interface Matchers<R extends void | Promise<void>, T = unknown> {
142139
/**
143140
* Ensures the last call to a mock function was provided specific args.
144141
*/
145-
lastCalledWith(...expected: ConditionalFunctionParameters<T>): R;
142+
lastCalledWith(...expected: Parameters<EnsureFunctionLike<T>>): R;
146143
/**
147144
* Ensure that the last call to a mock function has returned a specified value.
148145
*/
149146
lastReturnedWith(expected: unknown): R;
150147
/**
151148
* Ensure that a mock function is called with specific arguments on an Nth call.
152149
*/
153-
nthCalledWith(nth: number, ...expected: ConditionalFunctionParameters<T>): R;
150+
nthCalledWith(nth: number, ...expected: Parameters<EnsureFunctionLike<T>>): R;
154151
/**
155152
* Ensure that the nth call to a mock function has returned a specified value.
156153
*/
@@ -171,7 +168,7 @@ export interface Matchers<R extends void | Promise<void>, T = unknown> {
171168
/**
172169
* Ensure that a mock function is called with specific arguments.
173170
*/
174-
toBeCalledWith(...expected: ConditionalFunctionParameters<T>): R;
171+
toBeCalledWith(...expected: Parameters<EnsureFunctionLike<T>>): R;
175172
/**
176173
* Using exact equality with floating point numbers is a bad idea.
177174
* Rounding means that intuitive things fail.
@@ -254,19 +251,19 @@ export interface Matchers<R extends void | Promise<void>, T = unknown> {
254251
/**
255252
* Ensure that a mock function is called with specific arguments.
256253
*/
257-
toHaveBeenCalledWith(...expected: ConditionalFunctionParameters<T>): R;
254+
toHaveBeenCalledWith(...expected: Parameters<EnsureFunctionLike<T>>): R;
258255
/**
259256
* Ensure that a mock function is called with specific arguments on an Nth call.
260257
*/
261258
toHaveBeenNthCalledWith(
262259
nth: number,
263-
...expected: ConditionalFunctionParameters<T>
260+
...expected: Parameters<EnsureFunctionLike<T>>
264261
): R;
265262
/**
266263
* If you have a mock function, you can use `.toHaveBeenLastCalledWith`
267264
* to test what arguments it was last called with.
268265
*/
269-
toHaveBeenLastCalledWith(...expected: ConditionalFunctionParameters<T>): R;
266+
toHaveBeenLastCalledWith(...expected: Parameters<EnsureFunctionLike<T>>): R;
270267
/**
271268
* Use to test the specific value that a mock function last returned.
272269
* If the last call to the mock function threw an error, then this matcher will fail

0 commit comments

Comments
 (0)