From 1afcb568aa15589e875c59b67a2449cb0d4e1783 Mon Sep 17 00:00:00 2001 From: Tom Mrazauskas Date: Tue, 14 Mar 2023 20:30:18 +0200 Subject: [PATCH 1/4] fix(jest-mock): tweak typings to allow `jest.replace` replace methods --- .../__typetests__/mock-functions.test.ts | 23 +++++++++++-------- packages/jest-mock/src/index.ts | 23 ++++++++----------- 2 files changed, 24 insertions(+), 22 deletions(-) diff --git a/packages/jest-mock/__typetests__/mock-functions.test.ts b/packages/jest-mock/__typetests__/mock-functions.test.ts index a1c2157787ab..039469ae9c34 100644 --- a/packages/jest-mock/__typetests__/mock-functions.test.ts +++ b/packages/jest-mock/__typetests__/mock-functions.test.ts @@ -497,20 +497,25 @@ expectError( // replaceProperty + Replaced -const obj = { - fn: () => {}, - +const replaceObject = { + method: () => {}, property: 1, }; -expectType>(replaceProperty(obj, 'property', 1)); -expectType(replaceProperty(obj, 'property', 1).replaceValue(1).restore()); +expectType>(replaceProperty(replaceObject, 'property', 1)); +expectType void>>( + replaceProperty(replaceObject, 'method', () => {}), +); +expectType( + replaceProperty(replaceObject, 'property', 1).replaceValue(1).restore(), +); -expectError(replaceProperty(obj, 'invalid', 1)); -expectError(replaceProperty(obj, 'property', 'not a number')); -expectError(replaceProperty(obj, 'fn', () => {})); +expectError(replaceProperty(replaceObject, 'invalid', 1)); +expectError(replaceProperty(replaceObject, 'property', 'not a number')); -expectError(replaceProperty(obj, 'property', 1).replaceValue('not a number')); +expectError( + replaceProperty(replaceObject, 'property', 1).replaceValue('not a number'), +); interface ComplexObject { numberOrUndefined: number | undefined; diff --git a/packages/jest-mock/src/index.ts b/packages/jest-mock/src/index.ts index 8456c1312ac7..616df0bdb85c 100644 --- a/packages/jest-mock/src/index.ts +++ b/packages/jest-mock/src/index.ts @@ -184,10 +184,7 @@ export interface Replaced { replaceValue(value: T): this; } -type ReplacedPropertyRestorer< - T extends object, - K extends PropertyLikeKeys, -> = { +type ReplacedPropertyRestorer = { (): void; object: T; property: K; @@ -995,10 +992,10 @@ export class ModuleMocker { /** * Check whether the given property of an object has been already replaced. */ - private _findReplacedProperty< - T extends object, - K extends PropertyLikeKeys, - >(object: T, propertyKey: K): ReplacedPropertyRestorer | undefined { + private _findReplacedProperty( + object: T, + propertyKey: K, + ): ReplacedPropertyRestorer | undefined { for (const {restore} of this._spyState) { if ( 'object' in restore && @@ -1328,11 +1325,11 @@ export class ModuleMocker { return descriptor[accessType] as Mock; } - replaceProperty< - T extends object, - K extends PropertyLikeKeys, - V extends T[K], - >(object: T, propertyKey: K, value: V): Replaced { + replaceProperty( + object: T, + propertyKey: K, + value: T[K], + ): Replaced { if (object === undefined || object == null) { throw new Error( `replaceProperty could not find an object on which to replace ${String( From 9151743dc74f2a30ace4c34c0925d6f5709f7e01 Mon Sep 17 00:00:00 2001 From: Tom Mrazauskas Date: Tue, 14 Mar 2023 20:39:59 +0200 Subject: [PATCH 2/4] change log --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index eaa379867814..e378e634e8a5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ ### Fixes - `[jest-environment-jsdom, jest-environment-node]` Fix assignment of `customExportConditions` via `testEnvironmentOptions` when custom env subclass defines a default value ([#13989](https://github.com/facebook/jest/pull/13989)) +- `[jest-mock]` Tweak typings to allow `jest.replaceProperty()` replace methods ([#14008](https://github.com/facebook/jest/pull/14008)) ### Chore & Maintenance From eca27d5092e647b8715834f2238a21c386d463b5 Mon Sep 17 00:00:00 2001 From: Tom Mrazauskas Date: Tue, 14 Mar 2023 20:42:14 +0200 Subject: [PATCH 3/4] clean up --- packages/jest-mock/src/index.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/jest-mock/src/index.ts b/packages/jest-mock/src/index.ts index 616df0bdb85c..6801429f1626 100644 --- a/packages/jest-mock/src/index.ts +++ b/packages/jest-mock/src/index.ts @@ -177,7 +177,6 @@ export interface Replaced { * Restore property to its original value known at the time of mocking. */ restore(): void; - /** * Change the value of the property. */ From 641c21b4bfa2de5b3a40eaaa72233c1c1dd74fef Mon Sep 17 00:00:00 2001 From: Tom Mrazauskas Date: Tue, 4 Apr 2023 09:54:14 +0300 Subject: [PATCH 4/4] clean up --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 17c81fff0048..793a73fd8fb2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,8 +5,8 @@ ### Fixes - `[jest-environment-jsdom, jest-environment-node]` Fix assignment of `customExportConditions` via `testEnvironmentOptions` when custom env subclass defines a default value ([#13989](https://github.com/facebook/jest/pull/13989)) -- `[jest-mock]` Tweak typings to allow `jest.replaceProperty()` replace methods ([#14008](https://github.com/facebook/jest/pull/14008)) - `[jest-matcher-utils]` Fix copying value of inherited getters ([#14007](https://github.com/facebook/jest/pull/14007)) +- `[jest-mock]` Tweak typings to allow `jest.replaceProperty()` replace methods ([#14008](https://github.com/facebook/jest/pull/14008)) - `[jest-snapshot]` Fix a potential bug when not using prettier and improve performance ([#14036](https://github.com/facebook/jest/pull/14036)) ### Chore & Maintenance