From c1171888c3f94170068ed1e00d6cbdd954ccbdeb Mon Sep 17 00:00:00 2001 From: Tom Mrazauskas Date: Tue, 24 Jan 2023 14:28:41 +0200 Subject: [PATCH 01/18] add `jest.isEnvironmentTornDown()` --- types/jest/index.d.ts | 17 +++++++++++++++-- types/jest/jest-tests.ts | 12 ++++++++++-- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/types/jest/index.d.ts b/types/jest/index.d.ts index ad9ba4bfab444c..fc4fdb325d20ae 100644 --- a/types/jest/index.d.ts +++ b/types/jest/index.d.ts @@ -258,6 +258,17 @@ declare namespace jest { */ // eslint-disable-next-line no-unnecessary-generics function genMockFromModule(moduleName: string): T; + /** + * Returns `true` if test environment has been torn down. + * + * @example + * + * if (jest.isEnvironmentTornDown()) { + * // The Jest environment has been torn down, so stop doing work + * return; + * } + */ + function isEnvironmentTornDown(): boolean; /** * Returns whether the given function is a mock function. */ @@ -430,7 +441,8 @@ declare namespace jest { type ConstructorArgumentsOf = T extends new (...args: infer A) => any ? A : never; type ConstructorReturnType = T extends new (...args: any) => infer C ? C : any; - interface MockWithArgs extends MockInstance, ArgumentsOf, ConstructorReturnType> { + interface MockWithArgs + extends MockInstance, ArgumentsOf, ConstructorReturnType> { new (...args: ConstructorArgumentsOf): T; (...args: ArgumentsOf): ReturnType; } @@ -1231,7 +1243,8 @@ declare namespace jest { ReturnType, ArgsType, T extends (this: infer C, ...args: any[]) => any ? C : never - > & T; + > & + T; /** * Wrap a class with mock definitions diff --git a/types/jest/jest-tests.ts b/types/jest/jest-tests.ts index 41eaa80f605408..c4c8cabe318bb5 100644 --- a/types/jest/jest-tests.ts +++ b/types/jest/jest-tests.ts @@ -373,6 +373,9 @@ jest.advanceTimersToNextTimer(2); // $ExpectType void jest.clearAllTimers(); +// $ExpectType boolean +jest.isEnvironmentTornDown(); + // $ExpectType void jest.runAllImmediates(); @@ -488,9 +491,14 @@ interface TestApi { } // $ExpectType Mock || Mock const mock12 = jest.fn, jest.ArgsType>(); -interface TestMockContext { mock: boolean; test?: number; } +interface TestMockContext { + mock: boolean; + test?: number; +} // $ExpectType Mock<0, [string], TestMockContext> || Mock<0, [check: string], TestMockContext> -const mock13 = jest.fn(function(this: TestMockContext, check: string) { return 0 as const; }); +const mock13 = jest.fn(function (this: TestMockContext, check: string) { + return 0 as const; +}); // $ExpectType TestMockContext[] mock13.mock.contexts; From 0ccce4fc9eee77b833015dd96fca18df1a08d673 Mon Sep 17 00:00:00 2001 From: Tom Mrazauskas Date: Tue, 24 Jan 2023 14:28:52 +0200 Subject: [PATCH 02/18] add `jest.isolateModulesAsync()` --- types/jest/index.d.ts | 7 ++++++- types/jest/jest-tests.ts | 5 +++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/types/jest/index.d.ts b/types/jest/index.d.ts index fc4fdb325d20ae..f6c98c1b51ed8b 100644 --- a/types/jest/index.d.ts +++ b/types/jest/index.d.ts @@ -307,10 +307,15 @@ declare namespace jest { */ function resetModules(): typeof jest; /** - * Creates a sandbox registry for the modules that are loaded inside the callback function.. + * Creates a sandbox registry for the modules that are loaded inside the callback function. * This is useful to isolate specific modules for every test so that local module state doesn't conflict between tests. */ function isolateModules(fn: () => void): typeof jest; + /** + * Equivalent of `jest.isolateModules()` for async functions to be wrapped. + * The caller is expected to `await` the completion of `jest.isolateModulesAsync()`. + */ + function isolateModulesAsync(fn: () => Promise): Promise; /** * Runs failed tests n-times until they pass or until the max number of retries is exhausted. * This only works with jest-circus! diff --git a/types/jest/jest-tests.ts b/types/jest/jest-tests.ts index c4c8cabe318bb5..f4e05f2f423ab7 100644 --- a/types/jest/jest-tests.ts +++ b/types/jest/jest-tests.ts @@ -376,6 +376,11 @@ jest.clearAllTimers(); // $ExpectType boolean jest.isEnvironmentTornDown(); +// $ExpectType Promise +jest.isolateModulesAsync(async () => {}); +// @ts-expect-error: does not allow sync callbacks +jest.isolateModulesAsync(() => {}); + // $ExpectType void jest.runAllImmediates(); From 75bbfbd011749c7d877b09c30951aa54fbad06a0 Mon Sep 17 00:00:00 2001 From: Tom Mrazauskas Date: Tue, 24 Jan 2023 15:32:56 +0200 Subject: [PATCH 03/18] add `jest.replaceProperty()` and `jest.Replaced` --- types/jest/index.d.ts | 34 +++++++++++++++++++++----- types/jest/jest-tests.ts | 53 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 81 insertions(+), 6 deletions(-) diff --git a/types/jest/index.d.ts b/types/jest/index.d.ts index f6c98c1b51ed8b..a0fb2033ac323c 100644 --- a/types/jest/index.d.ts +++ b/types/jest/index.d.ts @@ -175,11 +175,12 @@ declare namespace jest { */ function resetAllMocks(): typeof jest; /** - * available since Jest 21.1.0 - * Restores all mocks back to their original value. - * Equivalent to calling .mockRestore on every mocked function. - * Beware that jest.restoreAllMocks() only works when mock was created with - * jest.spyOn; other mocks will require you to manually restore them. + * Restores all mocks and replaced properties back to their original value. + * Equivalent to calling `.mockRestore()` on every mocked function + * and `.restore()` on every replaced property. + * + * Beware that `jest.restoreAllMocks()` only works when the mock was created + * with `jest.spyOn()`; other mocks will require you to manually restore them. */ function restoreAllMocks(): typeof jest; /** @@ -278,7 +279,17 @@ declare namespace jest { */ // eslint-disable-next-line no-unnecessary-generics function mock(moduleName: string, factory?: () => T, options?: MockOptions): typeof jest; - + /** + * Replaces property on an object with another value. + * + * @remarks + * For mocking functions, and 'get' or 'set' accessors, use `jest.spyOn()` instead. + */ + function replaceProperty, V extends T[K]>( + object: T, + propertyKey: K, + value: V, + ): Replaced; /** * Wraps types of the `source` object and its deep members with type definitions * of Jest mock function. Pass `{shallow: true}` option to disable the deeply @@ -1520,6 +1531,17 @@ declare namespace jest { */ results: Array>; } + + interface Replaced { + /** + * Restore property to its original value known at the time of mocking. + */ + restore(): void; + /** + * Change the value of the property. + */ + replaceValue(value: T): this; + } } // Jest ships with a copy of Jasmine. They monkey-patch its APIs and divergence/deprecation are expected. diff --git a/types/jest/jest-tests.ts b/types/jest/jest-tests.ts index f4e05f2f423ab7..42403492f2033b 100644 --- a/types/jest/jest-tests.ts +++ b/types/jest/jest-tests.ts @@ -906,6 +906,59 @@ switch (mockResult.type) { break; } +/* Replace property */ + +const replaceObjectA = { + method: () => {}, + property: 1, +}; + +// $ExpectType Replaced +jest.replaceProperty(replaceObjectA, 'property', 2); + +let replaced: jest.Replaced; +replaced = jest.replaceProperty(replaceObjectA, 'property', 2); + +// $ExpectType void +jest.replaceProperty(replaceObjectA, 'property', 2).replaceValue(3).restore(); + +// @ts-expect-error: property does not exist +jest.replaceProperty(replaceObjectA, 'invalid', 1); +// @ts-expect-error: wrong type of the value +jest.replaceProperty(replaceObjectA, 'property', 'some text'); +// @ts-expect-error: wrong type of the value +jest.replaceProperty(replaceObjectA, 'property', 1).replaceValue('some text'); +// @ts-expect-error: method cannot be replaced +jest.replaceProperty(replaceObjectA, 'method', () => {}); + +interface ReplaceComplexObject { + numberOrUndefined: number | undefined; + optionalString?: string; + multipleTypes: number | string | { foo: number } | null; +} +declare const replaceComplexObject: ReplaceComplexObject; + +// $ExpectType Replaced +jest.replaceProperty(replaceComplexObject, 'numberOrUndefined', undefined); + +// $ExpectType Replaced +jest.replaceProperty(replaceComplexObject, 'numberOrUndefined', 1); + +// @ts-expect-error: wrong type of the value +jest.replaceProperty(replaceComplexObject, 'numberOrUndefined', 'some string'); + +// $ExpectType Replaced +jest.replaceProperty(replaceComplexObject, 'optionalString', 'foo'); + +// $ExpectType Replaced +jest.replaceProperty(replaceComplexObject, 'optionalString', undefined); + +// $ExpectType Replaced +jest.replaceProperty(replaceComplexObject, 'multipleTypes', 1) + .replaceValue('foo') + .replaceValue({ foo: 1 }) + .replaceValue(null); + /* getState and setState */ // @ts-expect-error expect.setState(true); From d44db1d56116c440afe3f8293fdf803fb1df972e Mon Sep 17 00:00:00 2001 From: Tom Mrazauskas Date: Tue, 24 Jan 2023 15:33:03 +0200 Subject: [PATCH 04/18] bump version --- types/jest/index.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/types/jest/index.d.ts b/types/jest/index.d.ts index a0fb2033ac323c..0efbb0fe3b8b77 100644 --- a/types/jest/index.d.ts +++ b/types/jest/index.d.ts @@ -1,4 +1,4 @@ -// Type definitions for Jest 29.2 +// Type definitions for Jest 29.4 // Project: https://jestjs.io/ // Definitions by: Asana (https://asana.com) // Ivo Stratev From 5a454500ff7cfbd2c02861cf0aacdfbe9a787bd0 Mon Sep 17 00:00:00 2001 From: Tom Mrazauskas Date: Tue, 24 Jan 2023 15:58:08 +0200 Subject: [PATCH 05/18] tweak types --- types/jest/index.d.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/types/jest/index.d.ts b/types/jest/index.d.ts index 0efbb0fe3b8b77..0e0feee26ba564 100644 --- a/types/jest/index.d.ts +++ b/types/jest/index.d.ts @@ -285,10 +285,11 @@ declare namespace jest { * @remarks * For mocking functions, and 'get' or 'set' accessors, use `jest.spyOn()` instead. */ - function replaceProperty, V extends T[K]>( + // eslint-disable-next-line no-unnecessary-generics + function replaceProperty>( object: T, propertyKey: K, - value: V, + value: T[K], ): Replaced; /** * Wraps types of the `source` object and its deep members with type definitions From 0a76394e219b2f5fa7378204280cabc0f1b9a4e7 Mon Sep 17 00:00:00 2001 From: Tom Mrazauskas Date: Tue, 24 Jan 2023 16:12:46 +0200 Subject: [PATCH 06/18] bump TS version --- types/jest/index.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/types/jest/index.d.ts b/types/jest/index.d.ts index 0e0feee26ba564..e410e0f03c8b53 100644 --- a/types/jest/index.d.ts +++ b/types/jest/index.d.ts @@ -28,7 +28,7 @@ // Adam Jones // Tom Mrazauskas // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped -// Minimum TypeScript Version: 4.3 +// Minimum TypeScript Version: 4.5 declare var beforeAll: jest.Lifecycle; declare var beforeEach: jest.Lifecycle; From 0bac15123aad93cf6335e2c5cef6ff43457bfc51 Mon Sep 17 00:00:00 2001 From: Tom Mrazauskas Date: Tue, 24 Jan 2023 16:14:01 +0200 Subject: [PATCH 07/18] fix space-before-function-paren ??? --- types/jest/jest-tests.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/types/jest/jest-tests.ts b/types/jest/jest-tests.ts index 42403492f2033b..3ca25874077bc5 100644 --- a/types/jest/jest-tests.ts +++ b/types/jest/jest-tests.ts @@ -501,7 +501,7 @@ interface TestMockContext { test?: number; } // $ExpectType Mock<0, [string], TestMockContext> || Mock<0, [check: string], TestMockContext> -const mock13 = jest.fn(function (this: TestMockContext, check: string) { +const mock13 = jest.fn(function(this: TestMockContext, check: string) { return 0 as const; }); // $ExpectType TestMockContext[] From 268b1cdd8155572ca8384b4172909571c55c4713 Mon Sep 17 00:00:00 2001 From: Tom Mrazauskas Date: Tue, 24 Jan 2023 16:20:45 +0200 Subject: [PATCH 08/18] bump TS version --- types/expect-puppeteer/index.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/types/expect-puppeteer/index.d.ts b/types/expect-puppeteer/index.d.ts index 485007e72b4e7d..cbad2557d36a14 100644 --- a/types/expect-puppeteer/index.d.ts +++ b/types/expect-puppeteer/index.d.ts @@ -3,7 +3,7 @@ // Definitions by: Tanguy Krotoff // Jason Mong // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped -// Minimum TypeScript Version: 4.3 +// Minimum TypeScript Version: 4.5 /// From 0f9c38752e83647a42f8126cbb247e58aa703290 Mon Sep 17 00:00:00 2001 From: Tom Mrazauskas Date: Tue, 24 Jan 2023 16:33:18 +0200 Subject: [PATCH 09/18] bump TS version --- types/heft-jest/index.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/types/heft-jest/index.d.ts b/types/heft-jest/index.d.ts index 8149046b9641a6..4635718711628e 100644 --- a/types/heft-jest/index.d.ts +++ b/types/heft-jest/index.d.ts @@ -3,7 +3,7 @@ // Definitions by: Pete Gonzalez // Ian Clanton-Thuon // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped -// Minimum TypeScript Version: 4.3 +// Minimum TypeScript Version: 4.5 /// /// From 69b7185e3f59fadb1f8631beb98068ba5474f49c Mon Sep 17 00:00:00 2001 From: Tom Mrazauskas Date: Tue, 24 Jan 2023 16:38:49 +0200 Subject: [PATCH 10/18] clean up --- types/jest/jest-tests.ts | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/types/jest/jest-tests.ts b/types/jest/jest-tests.ts index 3ca25874077bc5..6e0ac87a7d214c 100644 --- a/types/jest/jest-tests.ts +++ b/types/jest/jest-tests.ts @@ -496,14 +496,9 @@ interface TestApi { } // $ExpectType Mock || Mock const mock12 = jest.fn, jest.ArgsType>(); -interface TestMockContext { - mock: boolean; - test?: number; -} +interface TestMockContext { mock: boolean; test?: number; } // $ExpectType Mock<0, [string], TestMockContext> || Mock<0, [check: string], TestMockContext> -const mock13 = jest.fn(function(this: TestMockContext, check: string) { - return 0 as const; -}); +const mock13 = jest.fn(function(this: TestMockContext, check: string) { return 0 as const; }); // $ExpectType TestMockContext[] mock13.mock.contexts; From fef6dc7044b7c194189043177d89e207a927f606 Mon Sep 17 00:00:00 2001 From: Tom Mrazauskas Date: Tue, 24 Jan 2023 16:39:26 +0200 Subject: [PATCH 11/18] bump TS version --- types/jest-axe/index.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/types/jest-axe/index.d.ts b/types/jest-axe/index.d.ts index 06297411684c12..65bd7c7c650f31 100644 --- a/types/jest-axe/index.d.ts +++ b/types/jest-axe/index.d.ts @@ -2,7 +2,7 @@ // Project: https://github.com/nickcolley/jest-axe // Definitions by: erbridge // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped -// Minimum TypeScript Version: 4.3 +// Minimum TypeScript Version: 4.5 /// From 77182375a9679816ee269c092258be277caf8219 Mon Sep 17 00:00:00 2001 From: Tom Mrazauskas Date: Tue, 24 Jan 2023 16:57:14 +0200 Subject: [PATCH 12/18] bump TS version --- types/jest-image-snapshot/index.d.ts | 2 +- types/jest-in-case/index.d.ts | 2 +- types/jest-json-schema/index.d.ts | 2 +- types/jest-matcher-one-of/index.d.ts | 2 +- types/jest-plugin-context/index.d.ts | 2 +- types/jest-sinon/index.d.ts | 2 +- types/jest-specific-snapshot/index.d.ts | 2 +- types/jest-when/index.d.ts | 2 +- types/mapbox__aws-sdk-jest/index.d.ts | 2 +- types/testing-library__jest-dom/index.d.ts | 2 +- 10 files changed, 10 insertions(+), 10 deletions(-) diff --git a/types/jest-image-snapshot/index.d.ts b/types/jest-image-snapshot/index.d.ts index 3fcd7e9bbfa2f4..9b221e8df2b92a 100644 --- a/types/jest-image-snapshot/index.d.ts +++ b/types/jest-image-snapshot/index.d.ts @@ -5,7 +5,7 @@ // Piotr Błażejewicz // Ayc0 // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped -// Minimum TypeScript Version: 4.3 +// Minimum TypeScript Version: 4.5 /// diff --git a/types/jest-in-case/index.d.ts b/types/jest-in-case/index.d.ts index b16b2fbed0bacb..7e90781dc3e981 100644 --- a/types/jest-in-case/index.d.ts +++ b/types/jest-in-case/index.d.ts @@ -2,7 +2,7 @@ // Project: https://github.com/thinkmill/jest-in-case#readme // Definitions by: Geovani de Souza // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped -// Minimum TypeScript Version: 4.3 +// Minimum TypeScript Version: 4.5 /// /// diff --git a/types/jest-json-schema/index.d.ts b/types/jest-json-schema/index.d.ts index e31d20fa7bae1e..711032953c666c 100644 --- a/types/jest-json-schema/index.d.ts +++ b/types/jest-json-schema/index.d.ts @@ -2,7 +2,7 @@ // Project: https://github.com/americanexpress/jest-json-schema#readme // Definitions by: Matt Scheurich // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped -// Minimum TypeScript Version: 4.3 +// Minimum TypeScript Version: 4.5 /// import * as ajv from 'ajv'; diff --git a/types/jest-matcher-one-of/index.d.ts b/types/jest-matcher-one-of/index.d.ts index 34549eb2fb1691..16ef0e86f1b41a 100644 --- a/types/jest-matcher-one-of/index.d.ts +++ b/types/jest-matcher-one-of/index.d.ts @@ -2,7 +2,7 @@ // Project: https://github.com/d4nyll/jest-matcher-one-of#readme // Definitions by: Joe Mitchard // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped -// Minimum TypeScript Version: 4.3 +// Minimum TypeScript Version: 4.5 /// declare namespace jest { diff --git a/types/jest-plugin-context/index.d.ts b/types/jest-plugin-context/index.d.ts index 7d005f3731be46..43916009112a61 100644 --- a/types/jest-plugin-context/index.d.ts +++ b/types/jest-plugin-context/index.d.ts @@ -2,7 +2,7 @@ // Project: https://github.com/negativetwelve/jest-plugins/tree/master/packages/jest-plugin-context, https://github.com/negativetwelve/jest-plugins // Definitions by: Jonas Heinrich // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped -// Minimum TypeScript Version: 4.3 +// Minimum TypeScript Version: 4.5 /// diff --git a/types/jest-sinon/index.d.ts b/types/jest-sinon/index.d.ts index 5147d279f0321a..7cb0a0db26ee7f 100644 --- a/types/jest-sinon/index.d.ts +++ b/types/jest-sinon/index.d.ts @@ -2,7 +2,7 @@ // Project: https://github.com/djkf/jest-sinon/#readme // Definitions by: Martin Seidel // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped -// Minimum TypeScript Version: 4.3 +// Minimum TypeScript Version: 4.5 /// /// diff --git a/types/jest-specific-snapshot/index.d.ts b/types/jest-specific-snapshot/index.d.ts index 434a76a7a3d87b..848dbab4e81839 100644 --- a/types/jest-specific-snapshot/index.d.ts +++ b/types/jest-specific-snapshot/index.d.ts @@ -2,7 +2,7 @@ // Project: https://github.com/igor-dv/jest-specific-snapshot#readme // Definitions by: Janeene Beeforth // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped -// Minimum TypeScript Version: 4.3 +// Minimum TypeScript Version: 4.5 /// diff --git a/types/jest-when/index.d.ts b/types/jest-when/index.d.ts index ba7e11aa7cbea7..4843a89304485f 100644 --- a/types/jest-when/index.d.ts +++ b/types/jest-when/index.d.ts @@ -6,7 +6,7 @@ // Nicholas Hehr // Bogi Napoleon Wennerström // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped -// Minimum TypeScript Version: 4.3 +// Minimum TypeScript Version: 4.5 /// diff --git a/types/mapbox__aws-sdk-jest/index.d.ts b/types/mapbox__aws-sdk-jest/index.d.ts index 9dbb28577475c9..802ca24b5cc1dd 100644 --- a/types/mapbox__aws-sdk-jest/index.d.ts +++ b/types/mapbox__aws-sdk-jest/index.d.ts @@ -2,7 +2,7 @@ // Project: https://github.com/mapbox/aws-sdk-jest#readme // Definitions by: stevensnoeijen // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped -// Minimum TypeScript Version: 4.3 +// Minimum TypeScript Version: 4.5 /// /// diff --git a/types/testing-library__jest-dom/index.d.ts b/types/testing-library__jest-dom/index.d.ts index 43ba6b7fe458e7..a6b6d57065d154 100644 --- a/types/testing-library__jest-dom/index.d.ts +++ b/types/testing-library__jest-dom/index.d.ts @@ -5,7 +5,7 @@ // Seth Macpherson // Andrew Leedham // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped -// Minimum TypeScript Version: 4.3 +// Minimum TypeScript Version: 4.5 /// From 437d48aa3b46a0523962f576b3e0c4500a000198 Mon Sep 17 00:00:00 2001 From: Tom Mrazauskas Date: Tue, 24 Jan 2023 19:44:48 +0200 Subject: [PATCH 13/18] Revert "bump TS version" This reverts commit 77182375a9679816ee269c092258be277caf8219. --- types/jest-image-snapshot/index.d.ts | 2 +- types/jest-in-case/index.d.ts | 2 +- types/jest-json-schema/index.d.ts | 2 +- types/jest-matcher-one-of/index.d.ts | 2 +- types/jest-plugin-context/index.d.ts | 2 +- types/jest-sinon/index.d.ts | 2 +- types/jest-specific-snapshot/index.d.ts | 2 +- types/jest-when/index.d.ts | 2 +- types/mapbox__aws-sdk-jest/index.d.ts | 2 +- types/testing-library__jest-dom/index.d.ts | 2 +- 10 files changed, 10 insertions(+), 10 deletions(-) diff --git a/types/jest-image-snapshot/index.d.ts b/types/jest-image-snapshot/index.d.ts index 9b221e8df2b92a..3fcd7e9bbfa2f4 100644 --- a/types/jest-image-snapshot/index.d.ts +++ b/types/jest-image-snapshot/index.d.ts @@ -5,7 +5,7 @@ // Piotr Błażejewicz // Ayc0 // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped -// Minimum TypeScript Version: 4.5 +// Minimum TypeScript Version: 4.3 /// diff --git a/types/jest-in-case/index.d.ts b/types/jest-in-case/index.d.ts index 7e90781dc3e981..b16b2fbed0bacb 100644 --- a/types/jest-in-case/index.d.ts +++ b/types/jest-in-case/index.d.ts @@ -2,7 +2,7 @@ // Project: https://github.com/thinkmill/jest-in-case#readme // Definitions by: Geovani de Souza // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped -// Minimum TypeScript Version: 4.5 +// Minimum TypeScript Version: 4.3 /// /// diff --git a/types/jest-json-schema/index.d.ts b/types/jest-json-schema/index.d.ts index 711032953c666c..e31d20fa7bae1e 100644 --- a/types/jest-json-schema/index.d.ts +++ b/types/jest-json-schema/index.d.ts @@ -2,7 +2,7 @@ // Project: https://github.com/americanexpress/jest-json-schema#readme // Definitions by: Matt Scheurich // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped -// Minimum TypeScript Version: 4.5 +// Minimum TypeScript Version: 4.3 /// import * as ajv from 'ajv'; diff --git a/types/jest-matcher-one-of/index.d.ts b/types/jest-matcher-one-of/index.d.ts index 16ef0e86f1b41a..34549eb2fb1691 100644 --- a/types/jest-matcher-one-of/index.d.ts +++ b/types/jest-matcher-one-of/index.d.ts @@ -2,7 +2,7 @@ // Project: https://github.com/d4nyll/jest-matcher-one-of#readme // Definitions by: Joe Mitchard // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped -// Minimum TypeScript Version: 4.5 +// Minimum TypeScript Version: 4.3 /// declare namespace jest { diff --git a/types/jest-plugin-context/index.d.ts b/types/jest-plugin-context/index.d.ts index 43916009112a61..7d005f3731be46 100644 --- a/types/jest-plugin-context/index.d.ts +++ b/types/jest-plugin-context/index.d.ts @@ -2,7 +2,7 @@ // Project: https://github.com/negativetwelve/jest-plugins/tree/master/packages/jest-plugin-context, https://github.com/negativetwelve/jest-plugins // Definitions by: Jonas Heinrich // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped -// Minimum TypeScript Version: 4.5 +// Minimum TypeScript Version: 4.3 /// diff --git a/types/jest-sinon/index.d.ts b/types/jest-sinon/index.d.ts index 7cb0a0db26ee7f..5147d279f0321a 100644 --- a/types/jest-sinon/index.d.ts +++ b/types/jest-sinon/index.d.ts @@ -2,7 +2,7 @@ // Project: https://github.com/djkf/jest-sinon/#readme // Definitions by: Martin Seidel // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped -// Minimum TypeScript Version: 4.5 +// Minimum TypeScript Version: 4.3 /// /// diff --git a/types/jest-specific-snapshot/index.d.ts b/types/jest-specific-snapshot/index.d.ts index 848dbab4e81839..434a76a7a3d87b 100644 --- a/types/jest-specific-snapshot/index.d.ts +++ b/types/jest-specific-snapshot/index.d.ts @@ -2,7 +2,7 @@ // Project: https://github.com/igor-dv/jest-specific-snapshot#readme // Definitions by: Janeene Beeforth // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped -// Minimum TypeScript Version: 4.5 +// Minimum TypeScript Version: 4.3 /// diff --git a/types/jest-when/index.d.ts b/types/jest-when/index.d.ts index 4843a89304485f..ba7e11aa7cbea7 100644 --- a/types/jest-when/index.d.ts +++ b/types/jest-when/index.d.ts @@ -6,7 +6,7 @@ // Nicholas Hehr // Bogi Napoleon Wennerström // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped -// Minimum TypeScript Version: 4.5 +// Minimum TypeScript Version: 4.3 /// diff --git a/types/mapbox__aws-sdk-jest/index.d.ts b/types/mapbox__aws-sdk-jest/index.d.ts index 802ca24b5cc1dd..9dbb28577475c9 100644 --- a/types/mapbox__aws-sdk-jest/index.d.ts +++ b/types/mapbox__aws-sdk-jest/index.d.ts @@ -2,7 +2,7 @@ // Project: https://github.com/mapbox/aws-sdk-jest#readme // Definitions by: stevensnoeijen // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped -// Minimum TypeScript Version: 4.5 +// Minimum TypeScript Version: 4.3 /// /// diff --git a/types/testing-library__jest-dom/index.d.ts b/types/testing-library__jest-dom/index.d.ts index a6b6d57065d154..43ba6b7fe458e7 100644 --- a/types/testing-library__jest-dom/index.d.ts +++ b/types/testing-library__jest-dom/index.d.ts @@ -5,7 +5,7 @@ // Seth Macpherson // Andrew Leedham // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped -// Minimum TypeScript Version: 4.5 +// Minimum TypeScript Version: 4.3 /// From ede07276c8aaab9149a9d6cb7fcc33dabef0aa88 Mon Sep 17 00:00:00 2001 From: Tom Mrazauskas Date: Tue, 24 Jan 2023 19:45:06 +0200 Subject: [PATCH 14/18] Revert "bump TS version" This reverts commit fef6dc7044b7c194189043177d89e207a927f606. --- types/jest-axe/index.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/types/jest-axe/index.d.ts b/types/jest-axe/index.d.ts index 65bd7c7c650f31..06297411684c12 100644 --- a/types/jest-axe/index.d.ts +++ b/types/jest-axe/index.d.ts @@ -2,7 +2,7 @@ // Project: https://github.com/nickcolley/jest-axe // Definitions by: erbridge // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped -// Minimum TypeScript Version: 4.5 +// Minimum TypeScript Version: 4.3 /// From 0f2974a51c118f633881374823bdcdea7df69a78 Mon Sep 17 00:00:00 2001 From: Tom Mrazauskas Date: Tue, 24 Jan 2023 19:45:25 +0200 Subject: [PATCH 15/18] Revert "bump TS version" This reverts commit 0f9c38752e83647a42f8126cbb247e58aa703290. --- types/heft-jest/index.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/types/heft-jest/index.d.ts b/types/heft-jest/index.d.ts index 4635718711628e..8149046b9641a6 100644 --- a/types/heft-jest/index.d.ts +++ b/types/heft-jest/index.d.ts @@ -3,7 +3,7 @@ // Definitions by: Pete Gonzalez // Ian Clanton-Thuon // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped -// Minimum TypeScript Version: 4.5 +// Minimum TypeScript Version: 4.3 /// /// From f084b8253389751b38be96c13be2ee4001d5c9a8 Mon Sep 17 00:00:00 2001 From: Tom Mrazauskas Date: Tue, 24 Jan 2023 19:45:38 +0200 Subject: [PATCH 16/18] Revert "bump TS version" This reverts commit 268b1cdd8155572ca8384b4172909571c55c4713. --- types/expect-puppeteer/index.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/types/expect-puppeteer/index.d.ts b/types/expect-puppeteer/index.d.ts index cbad2557d36a14..485007e72b4e7d 100644 --- a/types/expect-puppeteer/index.d.ts +++ b/types/expect-puppeteer/index.d.ts @@ -3,7 +3,7 @@ // Definitions by: Tanguy Krotoff // Jason Mong // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped -// Minimum TypeScript Version: 4.5 +// Minimum TypeScript Version: 4.3 /// From de310783aa2bfb4ba5d36bd1e84f77189b8940f0 Mon Sep 17 00:00:00 2001 From: Tom Mrazauskas Date: Tue, 24 Jan 2023 19:45:58 +0200 Subject: [PATCH 17/18] Revert "bump TS version" This reverts commit 0a76394e219b2f5fa7378204280cabc0f1b9a4e7. --- types/jest/index.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/types/jest/index.d.ts b/types/jest/index.d.ts index e410e0f03c8b53..0e0feee26ba564 100644 --- a/types/jest/index.d.ts +++ b/types/jest/index.d.ts @@ -28,7 +28,7 @@ // Adam Jones // Tom Mrazauskas // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped -// Minimum TypeScript Version: 4.5 +// Minimum TypeScript Version: 4.3 declare var beforeAll: jest.Lifecycle; declare var beforeEach: jest.Lifecycle; From d7ce00d455f742beeb0d51d2ffb8af575a7e7532 Mon Sep 17 00:00:00 2001 From: Tom Mrazauskas Date: Tue, 24 Jan 2023 19:46:53 +0200 Subject: [PATCH 18/18] Revert "add `jest.replaceProperty()` and `jest.Replaced`" This reverts commit 75bbfbd011749c7d877b09c30951aa54fbad06a0. --- types/jest/index.d.ts | 34 ++++---------------------- types/jest/jest-tests.ts | 53 ---------------------------------------- 2 files changed, 5 insertions(+), 82 deletions(-) diff --git a/types/jest/index.d.ts b/types/jest/index.d.ts index 0e0feee26ba564..5201510824b0d1 100644 --- a/types/jest/index.d.ts +++ b/types/jest/index.d.ts @@ -175,12 +175,11 @@ declare namespace jest { */ function resetAllMocks(): typeof jest; /** - * Restores all mocks and replaced properties back to their original value. - * Equivalent to calling `.mockRestore()` on every mocked function - * and `.restore()` on every replaced property. - * - * Beware that `jest.restoreAllMocks()` only works when the mock was created - * with `jest.spyOn()`; other mocks will require you to manually restore them. + * available since Jest 21.1.0 + * Restores all mocks back to their original value. + * Equivalent to calling .mockRestore on every mocked function. + * Beware that jest.restoreAllMocks() only works when mock was created with + * jest.spyOn; other mocks will require you to manually restore them. */ function restoreAllMocks(): typeof jest; /** @@ -279,18 +278,6 @@ declare namespace jest { */ // eslint-disable-next-line no-unnecessary-generics function mock(moduleName: string, factory?: () => T, options?: MockOptions): typeof jest; - /** - * Replaces property on an object with another value. - * - * @remarks - * For mocking functions, and 'get' or 'set' accessors, use `jest.spyOn()` instead. - */ - // eslint-disable-next-line no-unnecessary-generics - function replaceProperty>( - object: T, - propertyKey: K, - value: T[K], - ): Replaced; /** * Wraps types of the `source` object and its deep members with type definitions * of Jest mock function. Pass `{shallow: true}` option to disable the deeply @@ -1532,17 +1519,6 @@ declare namespace jest { */ results: Array>; } - - interface Replaced { - /** - * Restore property to its original value known at the time of mocking. - */ - restore(): void; - /** - * Change the value of the property. - */ - replaceValue(value: T): this; - } } // Jest ships with a copy of Jasmine. They monkey-patch its APIs and divergence/deprecation are expected. diff --git a/types/jest/jest-tests.ts b/types/jest/jest-tests.ts index 6e0ac87a7d214c..0d75eda32b5e5a 100644 --- a/types/jest/jest-tests.ts +++ b/types/jest/jest-tests.ts @@ -901,59 +901,6 @@ switch (mockResult.type) { break; } -/* Replace property */ - -const replaceObjectA = { - method: () => {}, - property: 1, -}; - -// $ExpectType Replaced -jest.replaceProperty(replaceObjectA, 'property', 2); - -let replaced: jest.Replaced; -replaced = jest.replaceProperty(replaceObjectA, 'property', 2); - -// $ExpectType void -jest.replaceProperty(replaceObjectA, 'property', 2).replaceValue(3).restore(); - -// @ts-expect-error: property does not exist -jest.replaceProperty(replaceObjectA, 'invalid', 1); -// @ts-expect-error: wrong type of the value -jest.replaceProperty(replaceObjectA, 'property', 'some text'); -// @ts-expect-error: wrong type of the value -jest.replaceProperty(replaceObjectA, 'property', 1).replaceValue('some text'); -// @ts-expect-error: method cannot be replaced -jest.replaceProperty(replaceObjectA, 'method', () => {}); - -interface ReplaceComplexObject { - numberOrUndefined: number | undefined; - optionalString?: string; - multipleTypes: number | string | { foo: number } | null; -} -declare const replaceComplexObject: ReplaceComplexObject; - -// $ExpectType Replaced -jest.replaceProperty(replaceComplexObject, 'numberOrUndefined', undefined); - -// $ExpectType Replaced -jest.replaceProperty(replaceComplexObject, 'numberOrUndefined', 1); - -// @ts-expect-error: wrong type of the value -jest.replaceProperty(replaceComplexObject, 'numberOrUndefined', 'some string'); - -// $ExpectType Replaced -jest.replaceProperty(replaceComplexObject, 'optionalString', 'foo'); - -// $ExpectType Replaced -jest.replaceProperty(replaceComplexObject, 'optionalString', undefined); - -// $ExpectType Replaced -jest.replaceProperty(replaceComplexObject, 'multipleTypes', 1) - .replaceValue('foo') - .replaceValue({ foo: 1 }) - .replaceValue(null); - /* getState and setState */ // @ts-expect-error expect.setState(true);