From dcd39f09569ce4405b5fceded1892e0c04ed4e16 Mon Sep 17 00:00:00 2001 From: Tom Mrazauskas Date: Tue, 4 Oct 2022 20:52:30 +0300 Subject: [PATCH] fix(@jest/types): allow the `*ReturnedWith` matchers to be called with no argument (#13385) --- CHANGELOG.md | 1 + packages/expect/src/types.ts | 12 ++++++------ packages/jest-types/__typetests__/expect.test.ts | 12 ++++++------ 3 files changed, 13 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 692e7867bc51..30916146f9bb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ - `[babel-plugin-jest-hoist]` Ignore `TSTypeQuery` when checking for hoisted references ([#13367](https://github.com/facebook/jest/pull/13367)) - `[@jest/types]` Infer type of `each` table correctly when the table is a tuple or array ([#13381](https://github.com/facebook/jest/pull/13381)) +- `[@jest/types]` Rework typings to allow the `*ReturnedWith` matchers to be called with no argument ([#13385](https://github.com/facebook/jest/pull/13385)) ### Chore & Maintenance diff --git a/packages/expect/src/types.ts b/packages/expect/src/types.ts index 24c063f4022d..e2fc979165ff 100644 --- a/packages/expect/src/types.ts +++ b/packages/expect/src/types.ts @@ -139,7 +139,7 @@ export interface Matchers> { /** * Ensure that the last call to a mock function has returned a specified value. */ - lastReturnedWith(expected: unknown): R; + lastReturnedWith(expected?: unknown): R; /** * Ensure that a mock function is called with specific arguments on an Nth call. */ @@ -147,7 +147,7 @@ export interface Matchers> { /** * Ensure that the nth call to a mock function has returned a specified value. */ - nthReturnedWith(nth: number, expected: unknown): R; + nthReturnedWith(nth: number, expected?: unknown): R; /** * Checks that a value is what you expect. It calls `Object.is` to compare values. * Don't use `toBe` with floating-point numbers. @@ -262,7 +262,7 @@ export interface Matchers> { * If the last call to the mock function threw an error, then this matcher will fail * no matter what value you provided as the expected return value. */ - toHaveLastReturnedWith(expected: unknown): R; + toHaveLastReturnedWith(expected?: unknown): R; /** * Used to check that an object has a `.length` property * and it is set to a certain numeric value. @@ -273,7 +273,7 @@ export interface Matchers> { * If the nth call to the mock function threw an error, then this matcher will fail * no matter what value you provided as the expected return value. */ - toHaveNthReturnedWith(nth: number, expected: unknown): R; + toHaveNthReturnedWith(nth: number, expected?: unknown): R; /** * Use to check if property at provided reference keyPath exists for an object. * For checking deeply nested properties in an object you may use dot notation or an array containing @@ -303,7 +303,7 @@ export interface Matchers> { /** * Use to ensure that a mock function returned a specific value. */ - toHaveReturnedWith(expected: unknown): R; + toHaveReturnedWith(expected?: unknown): R; /** * Check that a string matches a regular expression. */ @@ -325,7 +325,7 @@ export interface Matchers> { /** * Ensure that a mock function has returned a specified value at least once. */ - toReturnWith(expected: unknown): R; + toReturnWith(expected?: unknown): R; /** * Use to test that objects have the same types as well as structure. */ diff --git a/packages/jest-types/__typetests__/expect.test.ts b/packages/jest-types/__typetests__/expect.test.ts index 37db168c7aab..79b68993379b 100644 --- a/packages/jest-types/__typetests__/expect.test.ts +++ b/packages/jest-types/__typetests__/expect.test.ts @@ -287,38 +287,39 @@ expectType(expect(jest.fn()).toHaveReturnedTimes(3)); expectError(expect(jest.fn()).toHaveReturnedTimes(true)); expectError(expect(jest.fn()).toHaveReturnedTimes()); +expectType(expect(jest.fn()).toReturnWith()); expectType(expect(jest.fn()).toReturnWith('value')); expectType( expect(jest.fn<() => string>()).toReturnWith( expect.stringContaining('value'), ), ); -expectError(expect(jest.fn()).toReturnWith()); +expectType(expect(jest.fn()).toHaveReturnedWith()); expectType(expect(jest.fn()).toHaveReturnedWith(123)); expectType( expect(jest.fn<() => string>()).toHaveReturnedWith( expect.stringContaining('value'), ), ); -expectError(expect(jest.fn()).toHaveReturnedWith()); +expectType(expect(jest.fn()).lastReturnedWith()); expectType(expect(jest.fn()).lastReturnedWith('value')); expectType( expect(jest.fn<() => string>()).lastReturnedWith( expect.stringContaining('value'), ), ); -expectError(expect(jest.fn()).lastReturnedWith()); +expectType(expect(jest.fn()).toHaveLastReturnedWith()); expectType(expect(jest.fn()).toHaveLastReturnedWith(123)); expectType( expect(jest.fn<() => string>()).toHaveLastReturnedWith( expect.stringContaining('value'), ), ); -expectError(expect(jest.fn()).toHaveLastReturnedWith()); +expectType(expect(jest.fn()).nthReturnedWith(1)); expectType(expect(jest.fn()).nthReturnedWith(1, 'value')); expectType( expect(jest.fn<() => string>()).nthReturnedWith( @@ -326,9 +327,9 @@ expectType( expect.stringContaining('value'), ), ); -expectError(expect(123).nthReturnedWith(3)); expectError(expect(jest.fn()).nthReturnedWith()); +expectType(expect(jest.fn()).nthReturnedWith(1)); expectType(expect(jest.fn()).nthReturnedWith(1, 'value')); expectType( expect(jest.fn<() => string>()).nthReturnedWith( @@ -336,7 +337,6 @@ expectType( expect.stringContaining('value'), ), ); -expectError(expect(123).toHaveNthReturnedWith(3)); expectError(expect(jest.fn()).toHaveNthReturnedWith()); // snapshot matchers