diff --git a/CHANGELOG.md b/CHANGELOG.md index c4895d250a34..4753d2f6ec67 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ ### Fixes +- `[expect]` Adjust typings of `lastCalledWith`, `nthCalledWith`, `toBeCalledWith` matchers to allow a case there a mock was called with no arguments ([#12807](https://github.com/facebook/jest/pull/12807)) - `[@jest/expect-utils]` Fix deep equality of ImmutableJS Lists ([#12763](https://github.com/facebook/jest/pull/12763)) - `[jest-core]` Do not collect `SIGNREQUEST` as open handles ([#12789](https://github.com/facebook/jest/pull/12789)) diff --git a/packages/expect/src/types.ts b/packages/expect/src/types.ts index 2b3e8c6ac613..3faf7956e02b 100644 --- a/packages/expect/src/types.ts +++ b/packages/expect/src/types.ts @@ -122,12 +122,11 @@ type PromiseMatchers = { resolves: Matchers> & Inverse>>; }; -// This is a copy from https://github.com/DefinitelyTyped/DefinitelyTyped/blob/de6730f4463cba69904698035fafd906a72b9664/types/jest/index.d.ts#L570-L817 export interface Matchers> { /** * Ensures the last call to a mock function was provided specific args. */ - lastCalledWith(...expected: [unknown, ...Array]): R; + lastCalledWith(...expected: Array): R; /** * Ensure that the last call to a mock function has returned a specified value. */ @@ -135,13 +134,13 @@ export interface Matchers> { /** * Ensure that a mock function is called with specific arguments on an Nth call. */ - nthCalledWith(nth: number, ...expected: [unknown, ...Array]): R; + nthCalledWith(nth: number, ...expected: Array): R; /** * Ensure that the nth call to a mock function has returned a specified value. */ nthReturnedWith(nth: number, expected: unknown): R; /** - * Checks that a value is what you expect. It uses `===` to check strict equality. + * Checks that a value is what you expect. It calls `Object.is` to compare values. * Don't use `toBe` with floating-point numbers. */ toBe(expected: unknown): R; @@ -156,7 +155,7 @@ export interface Matchers> { /** * Ensure that a mock function is called with specific arguments. */ - toBeCalledWith(...expected: [unknown, ...Array]): R; + toBeCalledWith(...expected: Array): R; /** * Using exact equality with floating point numbers is a bad idea. * Rounding means that intuitive things fail. @@ -239,19 +238,16 @@ export interface Matchers> { /** * Ensure that a mock function is called with specific arguments. */ - toHaveBeenCalledWith(...expected: [unknown, ...Array]): R; + toHaveBeenCalledWith(...expected: Array): R; /** * Ensure that a mock function is called with specific arguments on an Nth call. */ - toHaveBeenNthCalledWith( - nth: number, - ...expected: [unknown, ...Array] - ): R; + toHaveBeenNthCalledWith(nth: number, ...expected: Array): R; /** * If you have a mock function, you can use `.toHaveBeenLastCalledWith` * to test what arguments it was last called with. */ - toHaveBeenLastCalledWith(...expected: [unknown, ...Array]): R; + toHaveBeenLastCalledWith(...expected: Array): 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 diff --git a/packages/jest-types/__typetests__/expect.test.ts b/packages/jest-types/__typetests__/expect.test.ts index 2e0ef4e7db07..53e3b06fe752 100644 --- a/packages/jest-types/__typetests__/expect.test.ts +++ b/packages/jest-types/__typetests__/expect.test.ts @@ -211,28 +211,28 @@ expectType(expect(jest.fn()).toHaveBeenCalledTimes(3)); expectError(expect(jest.fn()).toHaveBeenCalledTimes(true)); expectError(expect(jest.fn()).toHaveBeenCalledTimes()); +expectType(expect(jest.fn()).toBeCalledWith()); expectType(expect(jest.fn()).toBeCalledWith('value')); expectType(expect(jest.fn()).toBeCalledWith('value', 123)); -expectError(expect(jest.fn()).toBeCalledWith()); +expectType(expect(jest.fn()).toHaveBeenCalledWith()); expectType(expect(jest.fn()).toHaveBeenCalledWith(123)); expectType(expect(jest.fn()).toHaveBeenCalledWith(123, 'value')); -expectError(expect(jest.fn()).toHaveBeenCalledWith()); +expectType(expect(jest.fn()).lastCalledWith()); expectType(expect(jest.fn()).lastCalledWith('value')); expectType(expect(jest.fn()).lastCalledWith('value', 123)); -expectError(expect(jest.fn()).lastCalledWith()); +expectType(expect(jest.fn()).toHaveBeenLastCalledWith()); expectType(expect(jest.fn()).toHaveBeenLastCalledWith(123)); expectType(expect(jest.fn()).toHaveBeenLastCalledWith(123, 'value')); -expectError(expect(jest.fn()).toHaveBeenLastCalledWith()); +expectType(expect(jest.fn()).nthCalledWith(2)); expectType(expect(jest.fn()).nthCalledWith(1, 'value')); expectType(expect(jest.fn()).nthCalledWith(1, 'value', 123)); expectError(expect(jest.fn()).nthCalledWith()); -expectError(expect(jest.fn()).nthCalledWith(2)); +expectType(expect(jest.fn()).toHaveBeenNthCalledWith(2)); expectType(expect(jest.fn()).toHaveBeenNthCalledWith(1, 'value')); expectType(expect(jest.fn()).toHaveBeenNthCalledWith(1, 'value', 123)); expectError(expect(jest.fn()).toHaveBeenNthCalledWith()); -expectError(expect(jest.fn()).toHaveBeenNthCalledWith(2)); expectType(expect(jest.fn()).toReturn()); expectError(expect(jest.fn()).toReturn('value'));