From 278ad1744d053c8e4a0c9d07a17945e05977b063 Mon Sep 17 00:00:00 2001 From: Tom Mrazauskas Date: Wed, 13 Jul 2022 15:41:24 +0300 Subject: [PATCH 1/2] refactor(jest-mock): simplify PropertyLikeKeys --- .../jest-mock/__typetests__/mock-functions.test.ts | 2 -- .../jest-mock/__typetests__/utility-types.test.ts | 10 +++++----- packages/jest-mock/src/index.ts | 11 ++++------- 3 files changed, 9 insertions(+), 14 deletions(-) diff --git a/packages/jest-mock/__typetests__/mock-functions.test.ts b/packages/jest-mock/__typetests__/mock-functions.test.ts index daf115d5bf52..6a7d6b82d0b6 100644 --- a/packages/jest-mock/__typetests__/mock-functions.test.ts +++ b/packages/jest-mock/__typetests__/mock-functions.test.ts @@ -374,8 +374,6 @@ expectType>( spyOn(indexSpiedObject, 'methodE'), ); -expectError(spyOn(indexSpiedObject, 'propertyA')); - expectType {a: string}>>( spyOn(indexSpiedObject, 'propertyA', 'get'), ); diff --git a/packages/jest-mock/__typetests__/utility-types.test.ts b/packages/jest-mock/__typetests__/utility-types.test.ts index 2faa5521d4ce..1a991b0b4d5b 100644 --- a/packages/jest-mock/__typetests__/utility-types.test.ts +++ b/packages/jest-mock/__typetests__/utility-types.test.ts @@ -86,10 +86,10 @@ type IndexObject = { methodA(): void; methodB(b: string): boolean; - methodC: (c: number) => true; + methodC: (c: number) => boolean; - propertyA: {a: 123}; - propertyB: {b: 'value'}; + propertyA: {a: number}; + propertyB: {b: string}; }; // ClassLike @@ -142,6 +142,6 @@ declare const objectProperties: PropertyLikeKeys; declare const indexObjectProperties: PropertyLikeKeys; expectType<'propertyA' | 'propertyB' | 'propertyC'>(classProperties); -expectType(indexClassProperties); +expectType(indexClassProperties); expectType<'propertyA' | 'propertyB' | 'someClassInstance'>(objectProperties); -expectType(indexObjectProperties); +expectType(indexObjectProperties); diff --git a/packages/jest-mock/src/index.ts b/packages/jest-mock/src/index.ts index 283e06b1920d..af36d752bf70 100644 --- a/packages/jest-mock/src/index.ts +++ b/packages/jest-mock/src/index.ts @@ -42,13 +42,10 @@ export type MethodLikeKeys = keyof { [K in keyof T as T[K] extends FunctionLike ? K : never]: T[K]; }; -export type PropertyLikeKeys = { - [K in keyof T]: T[K] extends FunctionLike - ? never - : T[K] extends ClassLike - ? never - : K; -}[keyof T]; +export type PropertyLikeKeys = Exclude< + keyof T, + ConstructorLikeKeys | MethodLikeKeys +>; // TODO Figure out how to replace this with TS ConstructorParameters utility type // https://www.typescriptlang.org/docs/handbook/utility-types.html#constructorparameterstype From a7af78c54f56792892f78f0b1dbc27c2903415f2 Mon Sep 17 00:00:00 2001 From: Tom Mrazauskas Date: Wed, 13 Jul 2022 15:56:21 +0300 Subject: [PATCH 2/2] add a note in changelog --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f2f972043922..052492261ef8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,7 +8,7 @@ - `[jest-changed-files]` Fix a lock-up after repeated invocations ([#12757](https://github.com/facebook/jest/issues/12757)) - `[@jest/expect-utils]` Fix deep equality of ImmutableJS OrderedSets ([#12977](https://github.com/facebook/jest/pull/12977)) -- `[jest-mock]` Add index signature support for `spyOn` types ([#13013](https://github.com/facebook/jest/pull/13013)) +- `[jest-mock]` Add index signature support for `spyOn` types ([#13013](https://github.com/facebook/jest/pull/13013), [#13020](https://github.com/facebook/jest/pull/13020)) - `[jest-snapshot]` Fix indentation of awaited inline snapshots ([#12986](https://github.com/facebook/jest/pull/12986)) ### Chore & Maintenance