Skip to content

Commit 3d626a5

Browse files
authored
feat(expect, @jest/expect): infer type of *ReturnedWith matchers argument (#13278)
1 parent d0f1b0a commit 3d626a5

File tree

4 files changed

+37
-9
lines changed

4 files changed

+37
-9
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
### Features
44

55
- `[expect, @jest/expect]` support type inference for function parameters in `CalledWith` assertions ([#13268](https://github.com/facebook/jest/pull/13268))
6+
- `[expect, @jest/expect]` Infer type of `*ReturnedWith` matchers argument ([#13278](https://github.com/facebook/jest/pull/13278))
67
- `[@jest/environment, jest-runtime]` Allow `jest.requireActual` and `jest.requireMock` to take a type argument ([#13253](https://github.com/facebook/jest/pull/13253))
78
- `[@jest/environment]` Allow `jest.mock` and `jest.doMock` to take a type argument ([#13254](https://github.com/facebook/jest/pull/13254))
89
- `[@jest/fake-timers]` Add `jest.now()` to return the current fake clock time ([#13244](https://github.com/facebook/jest/pull/13244), [13246](https://github.com/facebook/jest/pull/13246))

packages/expect/__typetests__/expect.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ import {
1616
} from 'expect';
1717
import type * as jestMatcherUtils from 'jest-matcher-utils';
1818

19-
type M = Matchers<void>;
19+
type M = Matchers<void, unknown>;
20+
type N = Matchers<void>;
2021

2122
expectError(() => {
2223
type E = Matchers;

packages/expect/src/types.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -141,15 +141,15 @@ export interface Matchers<R extends void | Promise<void>, T = unknown> {
141141
/**
142142
* Ensure that the last call to a mock function has returned a specified value.
143143
*/
144-
lastReturnedWith(expected: unknown): R;
144+
lastReturnedWith(expected: ReturnType<EnsureFunctionLike<T>>): R;
145145
/**
146146
* Ensure that a mock function is called with specific arguments on an Nth call.
147147
*/
148148
nthCalledWith(nth: number, ...expected: Parameters<EnsureFunctionLike<T>>): R;
149149
/**
150150
* Ensure that the nth call to a mock function has returned a specified value.
151151
*/
152-
nthReturnedWith(nth: number, expected: unknown): R;
152+
nthReturnedWith(nth: number, expected: ReturnType<EnsureFunctionLike<T>>): R;
153153
/**
154154
* Checks that a value is what you expect. It calls `Object.is` to compare values.
155155
* Don't use `toBe` with floating-point numbers.
@@ -267,7 +267,7 @@ export interface Matchers<R extends void | Promise<void>, T = unknown> {
267267
* If the last call to the mock function threw an error, then this matcher will fail
268268
* no matter what value you provided as the expected return value.
269269
*/
270-
toHaveLastReturnedWith(expected: unknown): R;
270+
toHaveLastReturnedWith(expected: ReturnType<EnsureFunctionLike<T>>): R;
271271
/**
272272
* Used to check that an object has a `.length` property
273273
* and it is set to a certain numeric value.
@@ -278,7 +278,10 @@ export interface Matchers<R extends void | Promise<void>, T = unknown> {
278278
* If the nth call to the mock function threw an error, then this matcher will fail
279279
* no matter what value you provided as the expected return value.
280280
*/
281-
toHaveNthReturnedWith(nth: number, expected: unknown): R;
281+
toHaveNthReturnedWith(
282+
nth: number,
283+
expected: ReturnType<EnsureFunctionLike<T>>,
284+
): R;
282285
/**
283286
* Use to check if property at provided reference keyPath exists for an object.
284287
* For checking deeply nested properties in an object you may use dot notation or an array containing
@@ -308,7 +311,7 @@ export interface Matchers<R extends void | Promise<void>, T = unknown> {
308311
/**
309312
* Use to ensure that a mock function returned a specific value.
310313
*/
311-
toHaveReturnedWith(expected: unknown): R;
314+
toHaveReturnedWith(expected: ReturnType<EnsureFunctionLike<T>>): R;
312315
/**
313316
* Check that a string matches a regular expression.
314317
*/
@@ -330,7 +333,7 @@ export interface Matchers<R extends void | Promise<void>, T = unknown> {
330333
/**
331334
* Ensure that a mock function has returned a specified value at least once.
332335
*/
333-
toReturnWith(expected: unknown): R;
336+
toReturnWith(expected: ReturnType<EnsureFunctionLike<T>>): R;
334337
/**
335338
* Use to test that objects have the same types as well as structure.
336339
*/

packages/jest-types/__typetests__/expect.test.ts

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -278,21 +278,44 @@ expectError(expect(jest.fn()).toHaveReturnedTimes(true));
278278
expectError(expect(jest.fn()).toHaveReturnedTimes());
279279

280280
expectType<void>(expect(jest.fn()).toReturnWith('value'));
281+
expectType<void>(expect(jest.fn<() => string>()).toReturnWith('value'));
282+
expectError(expect(jest.fn<() => number>()).toReturnWith('value'));
283+
expectError(expect(123).toReturnWith('value'));
281284
expectError(expect(jest.fn()).toReturnWith());
285+
282286
expectType<void>(expect(jest.fn()).toHaveReturnedWith(123));
287+
expectType<void>(expect(jest.fn<() => number>()).toHaveReturnedWith(123));
288+
expectError(expect(jest.fn<() => string>()).toHaveReturnedWith(123));
289+
expectError(expect(123).toHaveReturnedWith(123));
283290
expectError(expect(jest.fn()).toHaveReturnedWith());
284291

285292
expectType<void>(expect(jest.fn()).lastReturnedWith('value'));
293+
expectType<void>(expect(jest.fn<() => string>()).lastReturnedWith('value'));
294+
expectError(expect(jest.fn<() => number>()).lastReturnedWith('value'));
295+
expectError(expect(123).lastReturnedWith('value'));
286296
expectError(expect(jest.fn()).lastReturnedWith());
297+
287298
expectType<void>(expect(jest.fn()).toHaveLastReturnedWith(123));
299+
expectType<void>(expect(jest.fn<() => number>()).toHaveLastReturnedWith(123));
300+
expectError(expect(jest.fn<() => string>()).toHaveLastReturnedWith(123));
301+
expectError(expect(123).toHaveLastReturnedWith(123));
288302
expectError(expect(jest.fn()).toHaveLastReturnedWith());
289303

290304
expectType<void>(expect(jest.fn()).nthReturnedWith(1, 'value'));
305+
expectType<void>(expect(jest.fn<() => string>()).nthReturnedWith(2, 'value'));
306+
expectError(expect(jest.fn<() => number>()).nthReturnedWith(3, 'value'));
307+
expectError(expect(123).nthReturnedWith(4, 'value'));
308+
expectError(expect(123).nthReturnedWith(5));
291309
expectError(expect(jest.fn()).nthReturnedWith());
292-
expectError(expect(jest.fn()).nthReturnedWith(2));
310+
293311
expectType<void>(expect(jest.fn()).toHaveNthReturnedWith(1, 'value'));
312+
expectType<void>(
313+
expect(jest.fn<() => string>()).toHaveNthReturnedWith(2, 'value'),
314+
);
315+
expectError(expect(jest.fn<() => number>()).toHaveNthReturnedWith(3, 'value'));
316+
expectError(expect(123).toHaveNthReturnedWith(4, 'value'));
317+
expectError(expect(123).toHaveNthReturnedWith(5));
294318
expectError(expect(jest.fn()).toHaveNthReturnedWith());
295-
expectError(expect(jest.fn()).toHaveNthReturnedWith(2));
296319

297320
// snapshot matchers
298321

0 commit comments

Comments
 (0)