From d447395f118701610fee8c1142b57d0c679b9b9b Mon Sep 17 00:00:00 2001 From: Roy Hadad Date: Thu, 15 Sep 2022 17:46:57 +0300 Subject: [PATCH 01/21] feat(expect, jest-expect): support type inference for function parameters in CalledWith assertions --- packages/expect/src/types.ts | 33 +++++++++++++++++++------------ packages/jest-expect/src/types.ts | 2 +- 2 files changed, 21 insertions(+), 14 deletions(-) diff --git a/packages/expect/src/types.ts b/packages/expect/src/types.ts index 24c063f4022d..16cd6f62f4ed 100644 --- a/packages/expect/src/types.ts +++ b/packages/expect/src/types.ts @@ -94,9 +94,9 @@ export interface BaseExpect { } export type Expect = { - (actual: T): Matchers & - Inverse> & - PromiseMatchers; + (actual: T): Matchers & + Inverse> & + PromiseMatchers; } & BaseExpect & AsymmetricMatchers & Inverse>; @@ -118,24 +118,31 @@ export interface AsymmetricMatchers { stringMatching(sample: string | RegExp): AsymmetricMatcher; } -type PromiseMatchers = { +// if T is a function, return its parameters array type, otherwise return an unknown array type +type ConditionalFunctionParameters = T extends ( + ...args: Array + ) => unknown + ? Parameters + : Array; + +type PromiseMatchers = { /** * Unwraps the reason of a rejected promise so any other matcher can be chained. * If the promise is fulfilled the assertion fails. */ - rejects: Matchers> & Inverse>>; + rejects: Matchers> & Inverse, T>>; /** * Unwraps the value of a fulfilled promise so any other matcher can be chained. * If the promise is rejected the assertion fails. */ - resolves: Matchers> & Inverse>>; + resolves: Matchers> & Inverse, T>>; }; -export interface Matchers> { +export interface Matchers, T = unknown> { /** * Ensures the last call to a mock function was provided specific args. */ - lastCalledWith(...expected: Array): R; + lastCalledWith(...expected: ConditionalFunctionParameters): R; /** * Ensure that the last call to a mock function has returned a specified value. */ @@ -143,7 +150,7 @@ export interface Matchers> { /** * Ensure that a mock function is called with specific arguments on an Nth call. */ - nthCalledWith(nth: number, ...expected: Array): R; + nthCalledWith(nth: number, ...expected: ConditionalFunctionParameters): R; /** * Ensure that the nth call to a mock function has returned a specified value. */ @@ -164,7 +171,7 @@ export interface Matchers> { /** * Ensure that a mock function is called with specific arguments. */ - toBeCalledWith(...expected: Array): R; + toBeCalledWith(...expected: ConditionalFunctionParameters): R; /** * Using exact equality with floating point numbers is a bad idea. * Rounding means that intuitive things fail. @@ -247,16 +254,16 @@ export interface Matchers> { /** * Ensure that a mock function is called with specific arguments. */ - toHaveBeenCalledWith(...expected: Array): R; + toHaveBeenCalledWith(...expected: ConditionalFunctionParameters): R; /** * Ensure that a mock function is called with specific arguments on an Nth call. */ - toHaveBeenNthCalledWith(nth: number, ...expected: Array): R; + toHaveBeenNthCalledWith(nth: number, ...expected: ConditionalFunctionParameters): R; /** * If you have a mock function, you can use `.toHaveBeenLastCalledWith` * to test what arguments it was last called with. */ - toHaveBeenLastCalledWith(...expected: Array): R; + toHaveBeenLastCalledWith(...expected: ConditionalFunctionParameters): 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-expect/src/types.ts b/packages/jest-expect/src/types.ts index 10739da8481e..d1ba64a7f66f 100644 --- a/packages/jest-expect/src/types.ts +++ b/packages/jest-expect/src/types.ts @@ -29,7 +29,7 @@ type Inverse = { not: Matchers; }; -type JestMatchers, T> = Matchers & +type JestMatchers, T> = Matchers & SnapshotMatchers; type PromiseMatchers = { From 7c1f44ca4e20c5d8320c7afb80979d9d1f94eecc Mon Sep 17 00:00:00 2001 From: Roy Hadad Date: Thu, 15 Sep 2022 17:54:47 +0300 Subject: [PATCH 02/21] update changlog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 88cfba26a809..ad75217ef058 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ - `[feat(@jest/environment, jest-runtime)]` Allow `jest.requireActual` and `jest.requireMock` to take a type argument ([#13253](https://github.com/facebook/jest/pull/13253)) - `[feat(@jest/environment]` Allow `jest.mock` and `jest.doMock` to take a type argument ([#13254](https://github.com/facebook/jest/pull/13254)) - `[@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)) +- `[feat(expect, ject-expect)]` support type inference for function parameters in `CalledWith` assertions ([#13267](https://github.com/facebook/jest/issues/13267)) ### Fixes From f7d4b18d3291291c8ba3902e1dc7d9c4d3260b24 Mon Sep 17 00:00:00 2001 From: Roy Hadad Date: Thu, 15 Sep 2022 18:03:57 +0300 Subject: [PATCH 03/21] docs: add pull request link to changlog --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ad75217ef058..2b22c70e0886 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,7 +5,7 @@ - `[feat(@jest/environment, jest-runtime)]` Allow `jest.requireActual` and `jest.requireMock` to take a type argument ([#13253](https://github.com/facebook/jest/pull/13253)) - `[feat(@jest/environment]` Allow `jest.mock` and `jest.doMock` to take a type argument ([#13254](https://github.com/facebook/jest/pull/13254)) - `[@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)) -- `[feat(expect, ject-expect)]` support type inference for function parameters in `CalledWith` assertions ([#13267](https://github.com/facebook/jest/issues/13267)) +- `[feat(expect, ject-expect)]` support type inference for function parameters in `CalledWith` assertions ([#13268](https://github.com/facebook/jest/pull/13268)) ### Fixes From ff002fd15827c55d871e0e5b1c20ce52e1af4b81 Mon Sep 17 00:00:00 2001 From: Roy Hadad Date: Thu, 15 Sep 2022 18:13:05 +0300 Subject: [PATCH 04/21] docs: fix typo --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2b22c70e0886..c4ac2700c426 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,7 +5,7 @@ - `[feat(@jest/environment, jest-runtime)]` Allow `jest.requireActual` and `jest.requireMock` to take a type argument ([#13253](https://github.com/facebook/jest/pull/13253)) - `[feat(@jest/environment]` Allow `jest.mock` and `jest.doMock` to take a type argument ([#13254](https://github.com/facebook/jest/pull/13254)) - `[@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)) -- `[feat(expect, ject-expect)]` support type inference for function parameters in `CalledWith` assertions ([#13268](https://github.com/facebook/jest/pull/13268)) +- `[feat(expect, jest-expect)]` support type inference for function parameters in `CalledWith` assertions ([#13268](https://github.com/facebook/jest/pull/13268)) ### Fixes From cac0bb1eda6aac856dae750198c11d4f0e39e012 Mon Sep 17 00:00:00 2001 From: Roy Hadad Date: Thu, 15 Sep 2022 18:15:31 +0300 Subject: [PATCH 05/21] style: fix prettier --- packages/expect/src/types.ts | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/packages/expect/src/types.ts b/packages/expect/src/types.ts index 16cd6f62f4ed..9301530db8df 100644 --- a/packages/expect/src/types.ts +++ b/packages/expect/src/types.ts @@ -120,8 +120,8 @@ export interface AsymmetricMatchers { // if T is a function, return its parameters array type, otherwise return an unknown array type type ConditionalFunctionParameters = T extends ( - ...args: Array - ) => unknown + ...args: Array +) => unknown ? Parameters : Array; @@ -258,7 +258,10 @@ export interface Matchers, T = unknown> { /** * Ensure that a mock function is called with specific arguments on an Nth call. */ - toHaveBeenNthCalledWith(nth: number, ...expected: ConditionalFunctionParameters): R; + toHaveBeenNthCalledWith( + nth: number, + ...expected: ConditionalFunctionParameters + ): R; /** * If you have a mock function, you can use `.toHaveBeenLastCalledWith` * to test what arguments it was last called with. From 4c37edcf375e720120b11b1d442147a33ad58e63 Mon Sep 17 00:00:00 2001 From: Roy Hadad <39004075+royhadad@users.noreply.github.com> Date: Fri, 16 Sep 2022 07:30:21 +0300 Subject: [PATCH 06/21] Update CHANGELOG.md Co-authored-by: Tom Mrazauskas --- CHANGELOG.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c4ac2700c426..8f7ffab04c15 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,10 +2,10 @@ ### Features -- `[feat(@jest/environment, jest-runtime)]` Allow `jest.requireActual` and `jest.requireMock` to take a type argument ([#13253](https://github.com/facebook/jest/pull/13253)) -- `[feat(@jest/environment]` Allow `jest.mock` and `jest.doMock` to take a type argument ([#13254](https://github.com/facebook/jest/pull/13254)) +- `[@jest/environment, jest-runtime]` Allow `jest.requireActual` and `jest.requireMock` to take a type argument ([#13253](https://github.com/facebook/jest/pull/13253)) +- `[@jest/environment]` Allow `jest.mock` and `jest.doMock` to take a type argument ([#13254](https://github.com/facebook/jest/pull/13254)) - `[@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)) -- `[feat(expect, jest-expect)]` support type inference for function parameters in `CalledWith` assertions ([#13268](https://github.com/facebook/jest/pull/13268)) +- `[expect, @jest/expect]` support type inference for function parameters in `CalledWith` assertions ([#13268](https://github.com/facebook/jest/pull/13268)) ### Fixes From 58603977d22c04ea3151acc6d30e29cce64fa5d9 Mon Sep 17 00:00:00 2001 From: Roy Hadad Date: Sun, 18 Sep 2022 15:00:11 +0300 Subject: [PATCH 07/21] try something --- packages/jest-types/__typetests__/expect.test.ts | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/packages/jest-types/__typetests__/expect.test.ts b/packages/jest-types/__typetests__/expect.test.ts index 945dbd380451..ab3e4c934a03 100644 --- a/packages/jest-types/__typetests__/expect.test.ts +++ b/packages/jest-types/__typetests__/expect.test.ts @@ -7,8 +7,10 @@ import {expectError, expectType} from 'tsd-lite'; import type {EqualsFunction, Tester} from '@jest/expect-utils'; -import {expect, jest} from '@jest/globals'; +// import {expect, jest} from '@jest/globals'; +import {jest} from '@jest/globals'; import type * as jestMatcherUtils from 'jest-matcher-utils'; +import {expect} from '../../expect/build/index'; // asymmetric matchers @@ -217,6 +219,13 @@ expectType(expect(jest.fn()).toBeCalledWith('value', 123)); expectType(expect(jest.fn()).toHaveBeenCalledWith()); expectType(expect(jest.fn()).toHaveBeenCalledWith(123)); expectType(expect(jest.fn()).toHaveBeenCalledWith(123, 'value')); +expectType(expect(jest.fn()).toHaveBeenCalledWith(123, 'value')); + +// type inference for "CalledWith" matchers parameters +const jestFnWithParams = jest.fn((a: string, b: number) => {}); +expectType(expect(jestFnWithParams).toHaveBeenCalledWith('value', 123)); +expectError(expect(jestFnWithParams).toHaveBeenCalledWith(123, 'value')); +expectError(expect(jestFnWithParams).toHaveBeenCalledWith('value', 123)); expectType(expect(jest.fn()).lastCalledWith()); expectType(expect(jest.fn()).lastCalledWith('value')); From 2c9dac786d4dfc1abcd6c00a9e59efa3e987033c Mon Sep 17 00:00:00 2001 From: Roy Hadad Date: Sun, 18 Sep 2022 15:23:02 +0300 Subject: [PATCH 08/21] wip --- packages/jest-types/__typetests__/expect.test.ts | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/packages/jest-types/__typetests__/expect.test.ts b/packages/jest-types/__typetests__/expect.test.ts index ab3e4c934a03..a248347286cf 100644 --- a/packages/jest-types/__typetests__/expect.test.ts +++ b/packages/jest-types/__typetests__/expect.test.ts @@ -7,10 +7,8 @@ import {expectError, expectType} from 'tsd-lite'; import type {EqualsFunction, Tester} from '@jest/expect-utils'; -// import {expect, jest} from '@jest/globals'; -import {jest} from '@jest/globals'; +import {expect, jest} from '@jest/globals'; import type * as jestMatcherUtils from 'jest-matcher-utils'; -import {expect} from '../../expect/build/index'; // asymmetric matchers @@ -225,7 +223,6 @@ expectType(expect(jest.fn()).toHaveBeenCalledWith(123, 'value')); const jestFnWithParams = jest.fn((a: string, b: number) => {}); expectType(expect(jestFnWithParams).toHaveBeenCalledWith('value', 123)); expectError(expect(jestFnWithParams).toHaveBeenCalledWith(123, 'value')); -expectError(expect(jestFnWithParams).toHaveBeenCalledWith('value', 123)); expectType(expect(jest.fn()).lastCalledWith()); expectType(expect(jest.fn()).lastCalledWith('value')); From e73f878662c24d807053812625ef3c3840617583 Mon Sep 17 00:00:00 2001 From: Roy Hadad Date: Sun, 18 Sep 2022 16:03:07 +0300 Subject: [PATCH 09/21] remove type param --- packages/jest-types/__typetests__/expect.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/jest-types/__typetests__/expect.test.ts b/packages/jest-types/__typetests__/expect.test.ts index a248347286cf..139db952097a 100644 --- a/packages/jest-types/__typetests__/expect.test.ts +++ b/packages/jest-types/__typetests__/expect.test.ts @@ -222,7 +222,7 @@ expectType(expect(jest.fn()).toHaveBeenCalledWith(123, 'value')); // type inference for "CalledWith" matchers parameters const jestFnWithParams = jest.fn((a: string, b: number) => {}); expectType(expect(jestFnWithParams).toHaveBeenCalledWith('value', 123)); -expectError(expect(jestFnWithParams).toHaveBeenCalledWith(123, 'value')); +expectError(expect(jestFnWithParams).toHaveBeenCalledWith(123, 'value')); expectType(expect(jest.fn()).lastCalledWith()); expectType(expect(jest.fn()).lastCalledWith('value')); From 40809501ad2bc04c47393573b2bcfb27080793ef Mon Sep 17 00:00:00 2001 From: Roy Hadad Date: Sun, 18 Sep 2022 16:45:34 +0300 Subject: [PATCH 10/21] refactor: use idea from pr #13278 --- packages/expect/src/types.ts | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/packages/expect/src/types.ts b/packages/expect/src/types.ts index 9301530db8df..b9c0337e208e 100644 --- a/packages/expect/src/types.ts +++ b/packages/expect/src/types.ts @@ -118,13 +118,6 @@ export interface AsymmetricMatchers { stringMatching(sample: string | RegExp): AsymmetricMatcher; } -// if T is a function, return its parameters array type, otherwise return an unknown array type -type ConditionalFunctionParameters = T extends ( - ...args: Array -) => unknown - ? Parameters - : Array; - type PromiseMatchers = { /** * Unwraps the reason of a rejected promise so any other matcher can be chained. @@ -138,11 +131,15 @@ type PromiseMatchers = { resolves: Matchers> & Inverse, T>>; }; +type EnsureFunctionLike = T extends (...args: Array) => unknown + ? T + : never; + export interface Matchers, T = unknown> { /** * Ensures the last call to a mock function was provided specific args. */ - lastCalledWith(...expected: ConditionalFunctionParameters): R; + lastCalledWith(...expected: Parameters>): R; /** * Ensure that the last call to a mock function has returned a specified value. */ @@ -150,7 +147,7 @@ export interface Matchers, T = unknown> { /** * Ensure that a mock function is called with specific arguments on an Nth call. */ - nthCalledWith(nth: number, ...expected: ConditionalFunctionParameters): R; + nthCalledWith(nth: number, ...expected: Parameters>): R; /** * Ensure that the nth call to a mock function has returned a specified value. */ @@ -171,7 +168,7 @@ export interface Matchers, T = unknown> { /** * Ensure that a mock function is called with specific arguments. */ - toBeCalledWith(...expected: ConditionalFunctionParameters): R; + toBeCalledWith(...expected: Parameters>): R; /** * Using exact equality with floating point numbers is a bad idea. * Rounding means that intuitive things fail. @@ -254,19 +251,19 @@ export interface Matchers, T = unknown> { /** * Ensure that a mock function is called with specific arguments. */ - toHaveBeenCalledWith(...expected: ConditionalFunctionParameters): R; + toHaveBeenCalledWith(...expected: Parameters>): R; /** * Ensure that a mock function is called with specific arguments on an Nth call. */ toHaveBeenNthCalledWith( nth: number, - ...expected: ConditionalFunctionParameters + ...expected: Parameters> ): R; /** * If you have a mock function, you can use `.toHaveBeenLastCalledWith` * to test what arguments it was last called with. */ - toHaveBeenLastCalledWith(...expected: ConditionalFunctionParameters): R; + toHaveBeenLastCalledWith(...expected: Parameters>): 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 From c87ad1c35a4c24fd2724209986320cd3422a784e Mon Sep 17 00:00:00 2001 From: Roy Hadad <39004075+royhadad@users.noreply.github.com> Date: Sun, 18 Sep 2022 17:15:08 +0300 Subject: [PATCH 11/21] use any instead of unknown in EnsureFunctionLike Co-authored-by: Tom Mrazauskas --- packages/expect/src/types.ts | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/packages/expect/src/types.ts b/packages/expect/src/types.ts index b9c0337e208e..0e00d20bddcb 100644 --- a/packages/expect/src/types.ts +++ b/packages/expect/src/types.ts @@ -131,9 +131,7 @@ type PromiseMatchers = { resolves: Matchers> & Inverse, T>>; }; -type EnsureFunctionLike = T extends (...args: Array) => unknown - ? T - : never; +type EnsureFunctionLike = T extends (...args: any) => any ? T : never; export interface Matchers, T = unknown> { /** From f2bfab96eab8c9771a02f4bb66e1b0c83ee25cc3 Mon Sep 17 00:00:00 2001 From: Roy Hadad <39004075+royhadad@users.noreply.github.com> Date: Sun, 18 Sep 2022 17:23:15 +0300 Subject: [PATCH 12/21] add more tests Co-authored-by: Tom Mrazauskas --- .../jest-types/__typetests__/expect.test.ts | 24 +++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/packages/jest-types/__typetests__/expect.test.ts b/packages/jest-types/__typetests__/expect.test.ts index 139db952097a..99277889c242 100644 --- a/packages/jest-types/__typetests__/expect.test.ts +++ b/packages/jest-types/__typetests__/expect.test.ts @@ -219,10 +219,26 @@ expectType(expect(jest.fn()).toHaveBeenCalledWith(123)); expectType(expect(jest.fn()).toHaveBeenCalledWith(123, 'value')); expectType(expect(jest.fn()).toHaveBeenCalledWith(123, 'value')); -// type inference for "CalledWith" matchers parameters -const jestFnWithParams = jest.fn((a: string, b: number) => {}); -expectType(expect(jestFnWithParams).toHaveBeenCalledWith('value', 123)); -expectError(expect(jestFnWithParams).toHaveBeenCalledWith(123, 'value')); +expectType( + expect( + jest.fn<(a: string, b: number, c?: boolean) => void>(), + ).toHaveBeenCalledWith('value', 123), +); +expectType( + expect( + jest.fn<(a: string, b: number, c?: boolean) => void>(), + ).toHaveBeenCalledWith('value', 123, true), +); +expectError( + expect( + jest.fn<(a: string, b: number, c?: boolean) => void>(), + ).toHaveBeenCalledWith(123, 'value'), +); +expectError( + expect( + jest.fn<(a: string, b: number, c?: boolean) => void>(), + ).toHaveBeenCalledWith('value', 123, 'nope'), +); expectType(expect(jest.fn()).lastCalledWith()); expectType(expect(jest.fn()).lastCalledWith('value')); From 8969360d91fd992810cf19874163b507133dea2b Mon Sep 17 00:00:00 2001 From: Roy Hadad Date: Sun, 18 Sep 2022 17:32:27 +0300 Subject: [PATCH 13/21] remove changlog fixes --- CHANGELOG.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8f7ffab04c15..15205d0be9e9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,8 +2,8 @@ ### Features -- `[@jest/environment, jest-runtime]` Allow `jest.requireActual` and `jest.requireMock` to take a type argument ([#13253](https://github.com/facebook/jest/pull/13253)) -- `[@jest/environment]` Allow `jest.mock` and `jest.doMock` to take a type argument ([#13254](https://github.com/facebook/jest/pull/13254)) +- `[feat(@jest/environment, jest-runtime)]` Allow `jest.requireActual` and `jest.requireMock` to take a type argument ([#13253](https://github.com/facebook/jest/pull/13253)) +- `feat([@jest/environment]` Allow `jest.mock` and `jest.doMock` to take a type argument ([#13254](https://github.com/facebook/jest/pull/13254)) - `[@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)) - `[expect, @jest/expect]` support type inference for function parameters in `CalledWith` assertions ([#13268](https://github.com/facebook/jest/pull/13268)) From 3bdf5f9f7cb7a0779d57bc793203b7cfb2de5047 Mon Sep 17 00:00:00 2001 From: Roy Hadad Date: Sun, 18 Sep 2022 17:34:15 +0300 Subject: [PATCH 14/21] try --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 15205d0be9e9..1b8243cab5a3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,7 +3,7 @@ ### Features - `[feat(@jest/environment, jest-runtime)]` Allow `jest.requireActual` and `jest.requireMock` to take a type argument ([#13253](https://github.com/facebook/jest/pull/13253)) -- `feat([@jest/environment]` Allow `jest.mock` and `jest.doMock` to take a type argument ([#13254](https://github.com/facebook/jest/pull/13254)) +- `[feat(@jest/environment]` Allow `jest.mock` and `jest.doMock` to take a type argument ([#13254](https://github.com/facebook/jest/pull/13254)) - `[@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)) - `[expect, @jest/expect]` support type inference for function parameters in `CalledWith` assertions ([#13268](https://github.com/facebook/jest/pull/13268)) From 0655ce0614b235d430d12a7ab8386446502994aa Mon Sep 17 00:00:00 2001 From: Roy Hadad Date: Sun, 18 Sep 2022 17:34:47 +0300 Subject: [PATCH 15/21] try --- CHANGELOG.md | 1 - 1 file changed, 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1b8243cab5a3..88cfba26a809 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,7 +5,6 @@ - `[feat(@jest/environment, jest-runtime)]` Allow `jest.requireActual` and `jest.requireMock` to take a type argument ([#13253](https://github.com/facebook/jest/pull/13253)) - `[feat(@jest/environment]` Allow `jest.mock` and `jest.doMock` to take a type argument ([#13254](https://github.com/facebook/jest/pull/13254)) - `[@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)) -- `[expect, @jest/expect]` support type inference for function parameters in `CalledWith` assertions ([#13268](https://github.com/facebook/jest/pull/13268)) ### Fixes From 8332a60a873966876cacc122bc7bc747fea2422c Mon Sep 17 00:00:00 2001 From: Roy Hadad Date: Sun, 18 Sep 2022 17:35:35 +0300 Subject: [PATCH 16/21] revert --- CHANGELOG.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 88cfba26a809..8f7ffab04c15 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,9 +2,10 @@ ### Features -- `[feat(@jest/environment, jest-runtime)]` Allow `jest.requireActual` and `jest.requireMock` to take a type argument ([#13253](https://github.com/facebook/jest/pull/13253)) -- `[feat(@jest/environment]` Allow `jest.mock` and `jest.doMock` to take a type argument ([#13254](https://github.com/facebook/jest/pull/13254)) +- `[@jest/environment, jest-runtime]` Allow `jest.requireActual` and `jest.requireMock` to take a type argument ([#13253](https://github.com/facebook/jest/pull/13253)) +- `[@jest/environment]` Allow `jest.mock` and `jest.doMock` to take a type argument ([#13254](https://github.com/facebook/jest/pull/13254)) - `[@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)) +- `[expect, @jest/expect]` support type inference for function parameters in `CalledWith` assertions ([#13268](https://github.com/facebook/jest/pull/13268)) ### Fixes From e483fa131727904d7da5e4084e0ae05eba78cb51 Mon Sep 17 00:00:00 2001 From: Roy Hadad Date: Sun, 18 Sep 2022 17:44:24 +0300 Subject: [PATCH 17/21] test: add test cases for other methods --- packages/jest-types/__typetests__/expect.test.ts | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/packages/jest-types/__typetests__/expect.test.ts b/packages/jest-types/__typetests__/expect.test.ts index 99277889c242..cb1fd2fe04a5 100644 --- a/packages/jest-types/__typetests__/expect.test.ts +++ b/packages/jest-types/__typetests__/expect.test.ts @@ -219,6 +219,16 @@ expectType(expect(jest.fn()).toHaveBeenCalledWith(123)); expectType(expect(jest.fn()).toHaveBeenCalledWith(123, 'value')); expectType(expect(jest.fn()).toHaveBeenCalledWith(123, 'value')); +/** + * type inference for "CalledWith" matchers parameters + */ +expectError(expect(jest.fn<(a: string) => void>()).toHaveBeenCalledWith(123)); +expectError( + expect(jest.fn<(a: string) => void>()).toHaveBeenNthCalledWith(123), +); +expectError( + expect(jest.fn<(a: string) => void>()).toHaveBeenLastCalledWith(123), +); expectType( expect( jest.fn<(a: string, b: number, c?: boolean) => void>(), @@ -231,13 +241,13 @@ expectType( ); expectError( expect( - jest.fn<(a: string, b: number, c?: boolean) => void>(), + jest.fn<(a: number, b: string, c?: boolean) => void>(), ).toHaveBeenCalledWith(123, 'value'), ); expectError( expect( jest.fn<(a: string, b: number, c?: boolean) => void>(), - ).toHaveBeenCalledWith('value', 123, 'nope'), + ).toHaveBeenCalledWith('value', 123, 'not a boolean'), ); expectType(expect(jest.fn()).lastCalledWith()); From b7e41d12ca07a4e5b90a41d26bedc72e0274d7f3 Mon Sep 17 00:00:00 2001 From: Roy Hadad Date: Sun, 18 Sep 2022 17:49:15 +0300 Subject: [PATCH 18/21] fix test --- packages/jest-types/__typetests__/expect.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/jest-types/__typetests__/expect.test.ts b/packages/jest-types/__typetests__/expect.test.ts index cb1fd2fe04a5..f1fb848c5b8a 100644 --- a/packages/jest-types/__typetests__/expect.test.ts +++ b/packages/jest-types/__typetests__/expect.test.ts @@ -241,7 +241,7 @@ expectType( ); expectError( expect( - jest.fn<(a: number, b: string, c?: boolean) => void>(), + jest.fn<(a: string, b: number, c?: boolean) => void>(), ).toHaveBeenCalledWith(123, 'value'), ); expectError( From 61d3fbcd64b08daf7145f16bec6af97a615870bc Mon Sep 17 00:00:00 2001 From: Roy Hadad <39004075+royhadad@users.noreply.github.com> Date: Sun, 18 Sep 2022 18:38:35 +0300 Subject: [PATCH 19/21] fix test Co-authored-by: Tom Mrazauskas --- packages/jest-types/__typetests__/expect.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/jest-types/__typetests__/expect.test.ts b/packages/jest-types/__typetests__/expect.test.ts index f1fb848c5b8a..e214d1fad1e2 100644 --- a/packages/jest-types/__typetests__/expect.test.ts +++ b/packages/jest-types/__typetests__/expect.test.ts @@ -224,7 +224,7 @@ expectType(expect(jest.fn()).toHaveBeenCalledWith(123, 'value')); */ expectError(expect(jest.fn<(a: string) => void>()).toHaveBeenCalledWith(123)); expectError( - expect(jest.fn<(a: string) => void>()).toHaveBeenNthCalledWith(123), + expect(jest.fn<(a: string) => void>()).toHaveBeenNthCalledWith(1, 123), ); expectError( expect(jest.fn<(a: string) => void>()).toHaveBeenLastCalledWith(123), From 07333c1214cd702c83e11cc4be6865e15e706e71 Mon Sep 17 00:00:00 2001 From: Roy Hadad <39004075+royhadad@users.noreply.github.com> Date: Mon, 19 Sep 2022 10:51:03 +0300 Subject: [PATCH 20/21] remove redundant test Co-authored-by: Simen Bekkhus --- packages/jest-types/__typetests__/expect.test.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/jest-types/__typetests__/expect.test.ts b/packages/jest-types/__typetests__/expect.test.ts index e214d1fad1e2..aaefe2d9c752 100644 --- a/packages/jest-types/__typetests__/expect.test.ts +++ b/packages/jest-types/__typetests__/expect.test.ts @@ -217,7 +217,6 @@ expectType(expect(jest.fn()).toBeCalledWith('value', 123)); expectType(expect(jest.fn()).toHaveBeenCalledWith()); expectType(expect(jest.fn()).toHaveBeenCalledWith(123)); expectType(expect(jest.fn()).toHaveBeenCalledWith(123, 'value')); -expectType(expect(jest.fn()).toHaveBeenCalledWith(123, 'value')); /** * type inference for "CalledWith" matchers parameters From 4b15e901bbad581ad16a766b65a14d764495e7be Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Tue, 20 Sep 2022 08:26:20 +0200 Subject: [PATCH 21/21] Update CHANGELOG.md --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8f7ffab04c15..a5abaceaa795 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,10 +2,10 @@ ### Features +- `[expect, @jest/expect]` support type inference for function parameters in `CalledWith` assertions ([#13268](https://github.com/facebook/jest/pull/13268)) - `[@jest/environment, jest-runtime]` Allow `jest.requireActual` and `jest.requireMock` to take a type argument ([#13253](https://github.com/facebook/jest/pull/13253)) - `[@jest/environment]` Allow `jest.mock` and `jest.doMock` to take a type argument ([#13254](https://github.com/facebook/jest/pull/13254)) - `[@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)) -- `[expect, @jest/expect]` support type inference for function parameters in `CalledWith` assertions ([#13268](https://github.com/facebook/jest/pull/13268)) ### Fixes