Skip to content

Commit dcd39f0

Browse files
authored
fix(@jest/types): allow the *ReturnedWith matchers to be called with no argument (#13385)
1 parent ae8a5a2 commit dcd39f0

File tree

3 files changed

+13
-12
lines changed

3 files changed

+13
-12
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
- `[babel-plugin-jest-hoist]` Ignore `TSTypeQuery` when checking for hoisted references ([#13367](https://github.com/facebook/jest/pull/13367))
1212
- `[@jest/types]` Infer type of `each` table correctly when the table is a tuple or array ([#13381](https://github.com/facebook/jest/pull/13381))
13+
- `[@jest/types]` Rework typings to allow the `*ReturnedWith` matchers to be called with no argument ([#13385](https://github.com/facebook/jest/pull/13385))
1314

1415
### Chore & Maintenance
1516

packages/expect/src/types.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -139,15 +139,15 @@ export interface Matchers<R extends void | Promise<void>> {
139139
/**
140140
* Ensure that the last call to a mock function has returned a specified value.
141141
*/
142-
lastReturnedWith(expected: unknown): R;
142+
lastReturnedWith(expected?: unknown): R;
143143
/**
144144
* Ensure that a mock function is called with specific arguments on an Nth call.
145145
*/
146146
nthCalledWith(nth: number, ...expected: Array<unknown>): R;
147147
/**
148148
* Ensure that the nth call to a mock function has returned a specified value.
149149
*/
150-
nthReturnedWith(nth: number, expected: unknown): R;
150+
nthReturnedWith(nth: number, expected?: unknown): R;
151151
/**
152152
* Checks that a value is what you expect. It calls `Object.is` to compare values.
153153
* Don't use `toBe` with floating-point numbers.
@@ -262,7 +262,7 @@ export interface Matchers<R extends void | Promise<void>> {
262262
* If the last call to the mock function threw an error, then this matcher will fail
263263
* no matter what value you provided as the expected return value.
264264
*/
265-
toHaveLastReturnedWith(expected: unknown): R;
265+
toHaveLastReturnedWith(expected?: unknown): R;
266266
/**
267267
* Used to check that an object has a `.length` property
268268
* and it is set to a certain numeric value.
@@ -273,7 +273,7 @@ export interface Matchers<R extends void | Promise<void>> {
273273
* If the nth call to the mock function threw an error, then this matcher will fail
274274
* no matter what value you provided as the expected return value.
275275
*/
276-
toHaveNthReturnedWith(nth: number, expected: unknown): R;
276+
toHaveNthReturnedWith(nth: number, expected?: unknown): R;
277277
/**
278278
* Use to check if property at provided reference keyPath exists for an object.
279279
* For checking deeply nested properties in an object you may use dot notation or an array containing
@@ -303,7 +303,7 @@ export interface Matchers<R extends void | Promise<void>> {
303303
/**
304304
* Use to ensure that a mock function returned a specific value.
305305
*/
306-
toHaveReturnedWith(expected: unknown): R;
306+
toHaveReturnedWith(expected?: unknown): R;
307307
/**
308308
* Check that a string matches a regular expression.
309309
*/
@@ -325,7 +325,7 @@ export interface Matchers<R extends void | Promise<void>> {
325325
/**
326326
* Ensure that a mock function has returned a specified value at least once.
327327
*/
328-
toReturnWith(expected: unknown): R;
328+
toReturnWith(expected?: unknown): R;
329329
/**
330330
* Use to test that objects have the same types as well as structure.
331331
*/

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -287,56 +287,56 @@ expectType<void>(expect(jest.fn()).toHaveReturnedTimes(3));
287287
expectError(expect(jest.fn()).toHaveReturnedTimes(true));
288288
expectError(expect(jest.fn()).toHaveReturnedTimes());
289289

290+
expectType<void>(expect(jest.fn()).toReturnWith());
290291
expectType<void>(expect(jest.fn()).toReturnWith('value'));
291292
expectType<void>(
292293
expect(jest.fn<() => string>()).toReturnWith(
293294
expect.stringContaining('value'),
294295
),
295296
);
296-
expectError(expect(jest.fn()).toReturnWith());
297297

298+
expectType<void>(expect(jest.fn()).toHaveReturnedWith());
298299
expectType<void>(expect(jest.fn()).toHaveReturnedWith(123));
299300
expectType<void>(
300301
expect(jest.fn<() => string>()).toHaveReturnedWith(
301302
expect.stringContaining('value'),
302303
),
303304
);
304-
expectError(expect(jest.fn()).toHaveReturnedWith());
305305

306+
expectType<void>(expect(jest.fn()).lastReturnedWith());
306307
expectType<void>(expect(jest.fn()).lastReturnedWith('value'));
307308
expectType<void>(
308309
expect(jest.fn<() => string>()).lastReturnedWith(
309310
expect.stringContaining('value'),
310311
),
311312
);
312-
expectError(expect(jest.fn()).lastReturnedWith());
313313

314+
expectType<void>(expect(jest.fn()).toHaveLastReturnedWith());
314315
expectType<void>(expect(jest.fn()).toHaveLastReturnedWith(123));
315316
expectType<void>(
316317
expect(jest.fn<() => string>()).toHaveLastReturnedWith(
317318
expect.stringContaining('value'),
318319
),
319320
);
320-
expectError(expect(jest.fn()).toHaveLastReturnedWith());
321321

322+
expectType<void>(expect(jest.fn()).nthReturnedWith(1));
322323
expectType<void>(expect(jest.fn()).nthReturnedWith(1, 'value'));
323324
expectType<void>(
324325
expect(jest.fn<() => string>()).nthReturnedWith(
325326
2,
326327
expect.stringContaining('value'),
327328
),
328329
);
329-
expectError(expect(123).nthReturnedWith(3));
330330
expectError(expect(jest.fn()).nthReturnedWith());
331331

332+
expectType<void>(expect(jest.fn()).nthReturnedWith(1));
332333
expectType<void>(expect(jest.fn()).nthReturnedWith(1, 'value'));
333334
expectType<void>(
334335
expect(jest.fn<() => string>()).nthReturnedWith(
335336
2,
336337
expect.stringContaining('value'),
337338
),
338339
);
339-
expectError(expect(123).toHaveNthReturnedWith(3));
340340
expectError(expect(jest.fn()).toHaveNthReturnedWith());
341341

342342
// snapshot matchers

0 commit comments

Comments
 (0)