From 418c88892ea39121063092b8e3f326774bb1d037 Mon Sep 17 00:00:00 2001 From: Tom Mrazauskas Date: Wed, 31 Aug 2022 19:05:45 +0300 Subject: [PATCH 01/13] feat: `createMockFromModule` return type --- packages/jest-environment/src/index.ts | 6 +++--- packages/jest-mock/src/index.ts | 18 +++++++++--------- packages/jest-runtime/src/index.ts | 4 ++-- 3 files changed, 14 insertions(+), 14 deletions(-) diff --git a/packages/jest-environment/src/index.ts b/packages/jest-environment/src/index.ts index 4d4df64eaad7..340c9179687f 100644 --- a/packages/jest-environment/src/index.ts +++ b/packages/jest-environment/src/index.ts @@ -8,7 +8,7 @@ import type {Context} from 'vm'; import type {LegacyFakeTimers, ModernFakeTimers} from '@jest/fake-timers'; import type {Circus, Config, Global} from '@jest/types'; -import type {ModuleMocker} from 'jest-mock'; +import type {Mocked, ModuleMocker} from 'jest-mock'; export type EnvironmentContext = { console: Console; @@ -92,7 +92,7 @@ export interface Jest { * This is useful when you want to create a manual mock that extends the * automatic mock's behavior. */ - createMockFromModule(moduleName: string): unknown; + createMockFromModule(moduleName: string): Mocked; /** * Indicates that the module system should never return a mocked version of * the specified module and its dependencies. @@ -138,7 +138,7 @@ export interface Jest { * * @deprecated Use `jest.createMockFromModule()` instead */ - genMockFromModule(moduleName: string): unknown; + genMockFromModule(moduleName: string): Mocked; /** * When mocking time, `Date.now()` will also be mocked. If you for some reason * need access to the real current time, you can invoke this function. diff --git a/packages/jest-mock/src/index.ts b/packages/jest-mock/src/index.ts index 305699b0a50c..93bbc5501928 100644 --- a/packages/jest-mock/src/index.ts +++ b/packages/jest-mock/src/index.ts @@ -862,8 +862,8 @@ export class ModuleMocker { return createConstructor(mockConstructor); } - private _generateMock( - metadata: MockFunctionMetadata, + private _generateMock( + metadata: MockFunctionMetadata, callbacks: Array, refs: { [key: string]: @@ -872,9 +872,9 @@ export class ModuleMocker { | RegExp | UnknownFunction | undefined - | Mock; + | Mock; }, - ): Mock { + ): Mocked { // metadata not compatible but it's the same type, maybe problem with // overloading of _makeComponent and not _generateMock? // @ts-expect-error - unsure why TSC complains here? @@ -905,7 +905,7 @@ export class ModuleMocker { mock.prototype.constructor = mock; } - return mock as Mock; + return mock as Mocked; } /** @@ -913,12 +913,12 @@ export class ModuleMocker { * @param metadata Metadata for the mock in the schema returned by the * getMetadata method of this module. */ - generateFromMetadata( - metadata: MockFunctionMetadata, - ): Mock { + generateFromMetadata( + metadata: MockFunctionMetadata, + ): Mocked { const callbacks: Array = []; const refs = {}; - const mock = this._generateMock(metadata, callbacks, refs); + const mock = this._generateMock(metadata, callbacks, refs); callbacks.forEach(setter => setter()); return mock; } diff --git a/packages/jest-runtime/src/index.ts b/packages/jest-runtime/src/index.ts index 4541cd44d6e0..124d39ff0368 100644 --- a/packages/jest-runtime/src/index.ts +++ b/packages/jest-runtime/src/index.ts @@ -1710,7 +1710,7 @@ export default class Runtime { return Module; } - private _generateMock(from: string, moduleName: string) { + private _generateMock(from: string, moduleName: string) { const modulePath = this._resolver.resolveStubModuleName(from, moduleName) || this._resolveCjsModule(from, moduleName); @@ -1747,7 +1747,7 @@ export default class Runtime { } this._mockMetaDataCache.set(modulePath, mockMetadata); } - return this._moduleMocker.generateFromMetadata( + return this._moduleMocker.generateFromMetadata( // added above if missing this._mockMetaDataCache.get(modulePath)!, ); From 3650836cda7829e202fd60ef261919a069e7030e Mon Sep 17 00:00:00 2001 From: Tom Mrazauskas Date: Thu, 1 Sep 2022 15:30:06 +0300 Subject: [PATCH 02/13] rework generics --- packages/jest-environment/src/index.ts | 4 +- packages/jest-mock/src/index.ts | 76 +++++++++++++------------- packages/jest-runtime/src/index.ts | 13 +++-- 3 files changed, 49 insertions(+), 44 deletions(-) diff --git a/packages/jest-environment/src/index.ts b/packages/jest-environment/src/index.ts index 340c9179687f..bc8e372aa717 100644 --- a/packages/jest-environment/src/index.ts +++ b/packages/jest-environment/src/index.ts @@ -92,7 +92,7 @@ export interface Jest { * This is useful when you want to create a manual mock that extends the * automatic mock's behavior. */ - createMockFromModule(moduleName: string): Mocked; + createMockFromModule(moduleName: string): Mocked; /** * Indicates that the module system should never return a mocked version of * the specified module and its dependencies. @@ -138,7 +138,7 @@ export interface Jest { * * @deprecated Use `jest.createMockFromModule()` instead */ - genMockFromModule(moduleName: string): Mocked; + genMockFromModule(moduleName: string): Mocked; /** * When mocking time, `Date.now()` will also be mocked. If you for some reason * need access to the real current time, you can invoke this function. diff --git a/packages/jest-mock/src/index.ts b/packages/jest-mock/src/index.ts index 93bbc5501928..f7709287554d 100644 --- a/packages/jest-mock/src/index.ts +++ b/packages/jest-mock/src/index.ts @@ -7,7 +7,7 @@ /* eslint-disable local/ban-types-eventually, local/prefer-rest-params-eventually */ -export type MockFunctionMetadataType = +export type MockMetadataType = | 'object' | 'array' | 'regexp' @@ -17,20 +17,27 @@ export type MockFunctionMetadataType = | 'null' | 'undefined'; -export type MockFunctionMetadata< - T extends UnknownFunction = UnknownFunction, - MetadataType = MockFunctionMetadataType, -> = { +// TODO remove re-export in Jest 30 +export type MockFunctionMetadataType = MockMetadataType; + +export type MetaDataValue = T extends UnknownFunction + ? ReturnType + : undefined; + +export type MockMetadata = { ref?: number; - members?: Record>; + members?: Record>; mockImpl?: T; name?: string; refID?: number; type?: MetadataType; - value?: ReturnType; + value?: MetaDataValue; length?: number; }; +// TODO remove re-export in Jest 30 +export type MockFunctionMetadata = MockMetadata; + export type ClassLike = {new (...args: any): any}; export type FunctionLike = (...args: any) => any; @@ -75,7 +82,7 @@ type MockedObjectShallow = { : T[K]; } & T; -export type Mocked = T extends ClassLike +export type Mocked = T extends ClassLike ? MockedClass : T extends FunctionLike ? MockedFunction @@ -83,7 +90,7 @@ export type Mocked = T extends ClassLike ? MockedObject : T; -export type MockedShallow = T extends ClassLike +export type MockedShallow = T extends ClassLike ? MockedClass : T extends FunctionLike ? MockedFunctionShallow @@ -386,7 +393,7 @@ function getObjectType(value: unknown): string { return Object.prototype.toString.apply(value).slice(8, -1); } -function getType(ref?: unknown): MockFunctionMetadataType | null { +function getType(ref?: unknown): MockMetadataType | null { const typeName = getObjectType(ref); if ( typeName === 'Function' || @@ -560,31 +567,28 @@ export class ModuleMocker { }; } - private _makeComponent( - metadata: MockFunctionMetadata, + private _makeComponent( + metadata: MockMetadata, restore?: () => void, ): Record; - private _makeComponent( - metadata: MockFunctionMetadata, + private _makeComponent( + metadata: MockMetadata, restore?: () => void, ): Array; - private _makeComponent( - metadata: MockFunctionMetadata, + private _makeComponent( + metadata: MockMetadata, restore?: () => void, ): RegExp; - private _makeComponent( - metadata: MockFunctionMetadata< - T, - 'constant' | 'collection' | 'null' | 'undefined' - >, + private _makeComponent( + metadata: MockMetadata, restore?: () => void, ): T; private _makeComponent( - metadata: MockFunctionMetadata, + metadata: MockMetadata, restore?: () => void, ): Mock; private _makeComponent( - metadata: MockFunctionMetadata, + metadata: MockMetadata, restore?: () => void, ): | Record @@ -808,7 +812,7 @@ export class ModuleMocker { } private _createMockFunction( - metadata: MockFunctionMetadata, + metadata: MockMetadata, mockConstructor: Function, ): Function { let name = metadata.name; @@ -862,8 +866,8 @@ export class ModuleMocker { return createConstructor(mockConstructor); } - private _generateMock( - metadata: MockFunctionMetadata, + private _generateMock( + metadata: MockMetadata, callbacks: Array, refs: { [key: string]: @@ -913,9 +917,7 @@ export class ModuleMocker { * @param metadata Metadata for the mock in the schema returned by the * getMetadata method of this module. */ - generateFromMetadata( - metadata: MockFunctionMetadata, - ): Mocked { + generateFromMetadata(metadata: MockMetadata): Mocked { const callbacks: Array = []; const refs = {}; const mock = this._generateMock(metadata, callbacks, refs); @@ -927,11 +929,11 @@ export class ModuleMocker { * @see README.md * @param component The component for which to retrieve metadata. */ - getMetadata( - component: ReturnType, - _refs?: Map, number>, - ): MockFunctionMetadata | null { - const refs = _refs || new Map, number>(); + getMetadata( + component: MetaDataValue, + _refs?: Map, number>, + ): MockMetadata | null { + const refs = _refs || new Map, number>(); const ref = refs.get(component); if (ref != null) { return {ref}; @@ -942,7 +944,7 @@ export class ModuleMocker { return null; } - const metadata: MockFunctionMetadata = {type}; + const metadata: MockMetadata = {}; if ( type === 'constant' || type === 'collection' || @@ -967,7 +969,7 @@ export class ModuleMocker { refs.set(component, metadata.refID); let members: { - [key: string]: MockFunctionMetadata; + [key: string]: MockMetadata; } | null = null; // Leave arrays alone if (type !== 'array') { @@ -1007,7 +1009,7 @@ export class ModuleMocker { ): fn is Mock<(...args: P) => R>; isMockFunction(fn: unknown): fn is Mock; isMockFunction(fn: unknown): fn is Mock { - return fn != null && (fn as any)._isMockFunction === true; + return fn != null && (fn as Mock)._isMockFunction === true; } fn(implementation?: T): Mock { diff --git a/packages/jest-runtime/src/index.ts b/packages/jest-runtime/src/index.ts index 124d39ff0368..d085269e3778 100644 --- a/packages/jest-runtime/src/index.ts +++ b/packages/jest-runtime/src/index.ts @@ -50,7 +50,7 @@ import { import type {Config, Global} from '@jest/types'; import HasteMap, {IModuleMap} from 'jest-haste-map'; import {formatStackTrace, separateMessageFromStack} from 'jest-message-util'; -import type {MockFunctionMetadata, ModuleMocker} from 'jest-mock'; +import type {MetaDataValue, MockMetadata, ModuleMocker} from 'jest-mock'; import {escapePathForRegex} from 'jest-regex-util'; import Resolver, {ResolveModuleConfig} from 'jest-resolve'; import {EXTENSION as SnapshotExtension} from 'jest-snapshot'; @@ -168,7 +168,7 @@ export default class Runtime { private _isCurrentlyExecutingManualMock: string | null; private _mainModule: Module | null; private readonly _mockFactories: Map unknown>; - private readonly _mockMetaDataCache: Map; + private readonly _mockMetaDataCache: Map>; private _mockRegistry: Map; private _isolatedMockRegistry: Map | null; private _moduleMockRegistry: Map; @@ -1710,7 +1710,7 @@ export default class Runtime { return Module; } - private _generateMock(from: string, moduleName: string) { + private _generateMock(from: string, moduleName: string) { const modulePath = this._resolver.resolveStubModuleName(from, moduleName) || this._resolveCjsModule(from, moduleName); @@ -1720,7 +1720,7 @@ export default class Runtime { this._mockMetaDataCache.set( modulePath, - this._moduleMocker.getMetadata({}) || {}, + this._moduleMocker.getMetadata({} as MetaDataValue) || {}, ); // In order to avoid it being possible for automocking to potentially @@ -1732,7 +1732,10 @@ export default class Runtime { this._mockRegistry = new Map(); this._moduleRegistry = new Map(); - const moduleExports = this.requireModule(from, moduleName); + const moduleExports = this.requireModule>( + from, + moduleName, + ); // Restore the "real" module/mock registries this._mockRegistry = origMockRegistry; From a9a310c2e4e80f5af7c5381390085f891d199017 Mon Sep 17 00:00:00 2001 From: Tom Mrazauskas Date: Thu, 1 Sep 2022 15:47:21 +0300 Subject: [PATCH 03/13] revert --- packages/jest-mock/src/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/jest-mock/src/index.ts b/packages/jest-mock/src/index.ts index f7709287554d..afa880e16d6c 100644 --- a/packages/jest-mock/src/index.ts +++ b/packages/jest-mock/src/index.ts @@ -944,7 +944,7 @@ export class ModuleMocker { return null; } - const metadata: MockMetadata = {}; + const metadata: MockMetadata = {type}; if ( type === 'constant' || type === 'collection' || From 8eb8277a61f292486219794f008c10d89e3805d4 Mon Sep 17 00:00:00 2001 From: Tom Mrazauskas Date: Fri, 2 Sep 2022 07:31:04 +0300 Subject: [PATCH 04/13] use `unknown` --- packages/jest-mock/src/index.ts | 12 +++++------- packages/jest-runtime/src/index.ts | 9 +++------ 2 files changed, 8 insertions(+), 13 deletions(-) diff --git a/packages/jest-mock/src/index.ts b/packages/jest-mock/src/index.ts index afa880e16d6c..2cc45c434d8f 100644 --- a/packages/jest-mock/src/index.ts +++ b/packages/jest-mock/src/index.ts @@ -20,9 +20,7 @@ export type MockMetadataType = // TODO remove re-export in Jest 30 export type MockFunctionMetadataType = MockMetadataType; -export type MetaDataValue = T extends UnknownFunction - ? ReturnType - : undefined; +type MetaDataValue = T extends UnknownFunction ? ReturnType : undefined; export type MockMetadata = { ref?: number; @@ -930,10 +928,10 @@ export class ModuleMocker { * @param component The component for which to retrieve metadata. */ getMetadata( - component: MetaDataValue, - _refs?: Map, number>, + component: unknown, + _refs?: Map, ): MockMetadata | null { - const refs = _refs || new Map, number>(); + const refs = _refs || new Map(); const ref = refs.get(component); if (ref != null) { return {ref}; @@ -944,7 +942,7 @@ export class ModuleMocker { return null; } - const metadata: MockMetadata = {type}; + const metadata: MockMetadata = {type}; if ( type === 'constant' || type === 'collection' || diff --git a/packages/jest-runtime/src/index.ts b/packages/jest-runtime/src/index.ts index d085269e3778..0ed320d0154d 100644 --- a/packages/jest-runtime/src/index.ts +++ b/packages/jest-runtime/src/index.ts @@ -50,7 +50,7 @@ import { import type {Config, Global} from '@jest/types'; import HasteMap, {IModuleMap} from 'jest-haste-map'; import {formatStackTrace, separateMessageFromStack} from 'jest-message-util'; -import type {MetaDataValue, MockMetadata, ModuleMocker} from 'jest-mock'; +import type {MockMetadata, ModuleMocker} from 'jest-mock'; import {escapePathForRegex} from 'jest-regex-util'; import Resolver, {ResolveModuleConfig} from 'jest-resolve'; import {EXTENSION as SnapshotExtension} from 'jest-snapshot'; @@ -1720,7 +1720,7 @@ export default class Runtime { this._mockMetaDataCache.set( modulePath, - this._moduleMocker.getMetadata({} as MetaDataValue) || {}, + this._moduleMocker.getMetadata({}) || {}, ); // In order to avoid it being possible for automocking to potentially @@ -1732,10 +1732,7 @@ export default class Runtime { this._mockRegistry = new Map(); this._moduleRegistry = new Map(); - const moduleExports = this.requireModule>( - from, - moduleName, - ); + const moduleExports = this.requireModule(from, moduleName); // Restore the "real" module/mock registries this._mockRegistry = origMockRegistry; From c4cfc83fc712929e6e62210a3b7131a0c55b4935 Mon Sep 17 00:00:00 2001 From: Tom Mrazauskas Date: Fri, 2 Sep 2022 09:39:03 +0300 Subject: [PATCH 05/13] fix MockMetadata type --- packages/jest-mock/src/index.ts | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/packages/jest-mock/src/index.ts b/packages/jest-mock/src/index.ts index 2cc45c434d8f..98e8dbfef5d6 100644 --- a/packages/jest-mock/src/index.ts +++ b/packages/jest-mock/src/index.ts @@ -20,8 +20,6 @@ export type MockMetadataType = // TODO remove re-export in Jest 30 export type MockFunctionMetadataType = MockMetadataType; -type MetaDataValue = T extends UnknownFunction ? ReturnType : undefined; - export type MockMetadata = { ref?: number; members?: Record>; @@ -29,7 +27,7 @@ export type MockMetadata = { name?: string; refID?: number; type?: MetadataType; - value?: MetaDataValue; + value?: T; length?: number; }; @@ -927,11 +925,11 @@ export class ModuleMocker { * @see README.md * @param component The component for which to retrieve metadata. */ - getMetadata( - component: unknown, - _refs?: Map, + getMetadata( + component: T, + _refs?: Map, ): MockMetadata | null { - const refs = _refs || new Map(); + const refs = _refs || new Map(); const ref = refs.get(component); if (ref != null) { return {ref}; @@ -942,7 +940,7 @@ export class ModuleMocker { return null; } - const metadata: MockMetadata = {type}; + const metadata: MockMetadata = {type}; if ( type === 'constant' || type === 'collection' || From ce1c24906cf50a0e1b24b9932456e2e14c58f2d8 Mon Sep 17 00:00:00 2001 From: Tom Mrazauskas Date: Fri, 2 Sep 2022 09:47:54 +0300 Subject: [PATCH 06/13] clean up --- packages/jest-mock/src/index.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/packages/jest-mock/src/index.ts b/packages/jest-mock/src/index.ts index 98e8dbfef5d6..43bb43641ac3 100644 --- a/packages/jest-mock/src/index.ts +++ b/packages/jest-mock/src/index.ts @@ -563,18 +563,18 @@ export class ModuleMocker { }; } - private _makeComponent( + private _makeComponent>( metadata: MockMetadata, restore?: () => void, - ): Record; - private _makeComponent( + ): T; + private _makeComponent>( metadata: MockMetadata, restore?: () => void, - ): Array; - private _makeComponent( + ): T; + private _makeComponent( metadata: MockMetadata, restore?: () => void, - ): RegExp; + ): T; private _makeComponent( metadata: MockMetadata, restore?: () => void, From ce353cde0d9b9d0c2ff0a17b74dd35578e6b978f Mon Sep 17 00:00:00 2001 From: Tom Mrazauskas Date: Fri, 2 Sep 2022 10:02:29 +0300 Subject: [PATCH 07/13] backwards compatibility --- packages/jest-mock/src/index.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/jest-mock/src/index.ts b/packages/jest-mock/src/index.ts index 43bb43641ac3..8c2796f8d8cd 100644 --- a/packages/jest-mock/src/index.ts +++ b/packages/jest-mock/src/index.ts @@ -32,7 +32,10 @@ export type MockMetadata = { }; // TODO remove re-export in Jest 30 -export type MockFunctionMetadata = MockMetadata; +export type MockFunctionMetadata< + T = unknown, + MetadataType = MockMetadataType, +> = MockMetadata; export type ClassLike = {new (...args: any): any}; export type FunctionLike = (...args: any) => any; From 463667acae4e4e13da38a0289855964eaafce60d Mon Sep 17 00:00:00 2001 From: Tom Mrazauskas Date: Fri, 2 Sep 2022 10:47:23 +0300 Subject: [PATCH 08/13] add docs --- docs/JestObjectAPI.md | 52 ++++++++++++++++++++++++++++++++----------- 1 file changed, 39 insertions(+), 13 deletions(-) diff --git a/docs/JestObjectAPI.md b/docs/JestObjectAPI.md index add6b8c49a6c..b80a1759e1b1 100644 --- a/docs/JestObjectAPI.md +++ b/docs/JestObjectAPI.md @@ -5,6 +5,16 @@ title: The Jest Object The `jest` object is automatically in scope within every test file. The methods in the `jest` object help create mocks and let you control Jest's overall behavior. It can also be imported explicitly by via `import {jest} from '@jest/globals'`. +:::info + +The TypeScript examples from this page will only work as documented if you import global APIs from `'@jest/globals'`: + +```ts +import {expect, jest, test} from '@jest/globals'; +``` + +::: + ## Methods import TOCInline from '@theme/TOCInline'; @@ -96,18 +106,12 @@ _Note: this method was previously called `autoMockOn`. When using `babel-jest`, ### `jest.createMockFromModule(moduleName)` -##### renamed in Jest **26.0.0+** - -Also under the alias: `.genMockFromModule(moduleName)` - Given the name of a module, use the automatic mocking system to generate a mocked version of the module for you. -This is useful when you want to create a [manual mock](ManualMocks.md) that extends the automatic mock's behavior. +This is useful when you want to create a [manual mock](ManualMocks.md) that extends the automatic mock's behavior: -Example: - -```js title="utils.js" -export default { +```js tab={"span":2} title="utils.js" +module.exports = { authorize: () => { return 'token'; }, @@ -116,12 +120,34 @@ export default { ``` ```js title="__tests__/createMockFromModule.test.js" -const utils = jest.createMockFromModule('../utils').default; +const utils = jest.createMockFromModule('../utils'); + utils.isAuthorized = jest.fn(secret => secret === 'not wizard'); test('implementation created by jest.createMockFromModule', () => { - expect(utils.authorize.mock).toBeTruthy(); - expect(utils.isAuthorized('not wizard')).toEqual(true); + expect(jest.isMockFunction(utils.authorize)).toBe(true); + expect(utils.isAuthorized('not wizard')).toBe(true); +}); +``` + +```ts tab={"span":2} title="utils.ts" +export const utils = { + authorize: () => { + return 'token'; + }, + isAuthorized: (secret: string) => secret === 'wizard', +}; +``` + +```ts title="__tests__/createMockFromModule.test.ts" +const {utils} = + jest.createMockFromModule('../utils'); + +utils.isAuthorized = jest.fn((secret: string) => secret === 'not wizard'); + +test('implementation created by jest.createMockFromModule', () => { + expect(jest.isMockFunction(utils.authorize)).toBe(true); + expect(utils.isAuthorized('not wizard')).toBe(true); }); ``` @@ -180,7 +206,7 @@ module.exports = { ``` ```js title="__tests__/example.test.js" -const example = jest.createMockFromModule('./example'); +const example = jest.createMockFromModule('../example'); test('should run example code', () => { // creates a new mocked function with no formal arguments. From bd687d348a8d171ac696a8422a34ca02c2a68c1a Mon Sep 17 00:00:00 2001 From: Tom Mrazauskas Date: Fri, 2 Sep 2022 11:05:25 +0300 Subject: [PATCH 09/13] clean up --- packages/jest-mock/src/index.ts | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/packages/jest-mock/src/index.ts b/packages/jest-mock/src/index.ts index 8c2796f8d8cd..ca4c4d3a7aed 100644 --- a/packages/jest-mock/src/index.ts +++ b/packages/jest-mock/src/index.ts @@ -868,15 +868,15 @@ export class ModuleMocker { private _generateMock( metadata: MockMetadata, callbacks: Array, - refs: { - [key: string]: - | Record - | Array - | RegExp - | UnknownFunction - | undefined - | Mock; - }, + refs: Record< + number, + | Record + | Array + | RegExp + | UnknownFunction + | Mock + | undefined + >, ): Mocked { // metadata not compatible but it's the same type, maybe problem with // overloading of _makeComponent and not _generateMock? From e00c5c4e3576c48ca39b22fe714cc002c82cd36e Mon Sep 17 00:00:00 2001 From: Tom Mrazauskas Date: Fri, 2 Sep 2022 11:31:10 +0300 Subject: [PATCH 10/13] more clean up --- packages/jest-mock/src/index.ts | 15 ++------------- 1 file changed, 2 insertions(+), 13 deletions(-) diff --git a/packages/jest-mock/src/index.ts b/packages/jest-mock/src/index.ts index ca4c4d3a7aed..71f2bfc95376 100644 --- a/packages/jest-mock/src/index.ts +++ b/packages/jest-mock/src/index.ts @@ -589,13 +589,7 @@ export class ModuleMocker { private _makeComponent( metadata: MockMetadata, restore?: () => void, - ): - | Record - | Array - | RegExp - | ReturnType - | undefined - | Mock { + ): Record | Array | RegExp | T | Mock | undefined { if (metadata.type === 'object') { return new this._environmentGlobal.Object(); } else if (metadata.type === 'array') { @@ -870,12 +864,7 @@ export class ModuleMocker { callbacks: Array, refs: Record< number, - | Record - | Array - | RegExp - | UnknownFunction - | Mock - | undefined + Record | Array | RegExp | T | Mock | undefined >, ): Mocked { // metadata not compatible but it's the same type, maybe problem with From b8dd0191d7083e9822b22eb06c85ab303bf7a396 Mon Sep 17 00:00:00 2001 From: Tom Mrazauskas Date: Fri, 2 Sep 2022 11:32:51 +0300 Subject: [PATCH 11/13] add todo --- packages/jest-environment/src/index.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/jest-environment/src/index.ts b/packages/jest-environment/src/index.ts index bc8e372aa717..e5a4c3037e65 100644 --- a/packages/jest-environment/src/index.ts +++ b/packages/jest-environment/src/index.ts @@ -129,6 +129,7 @@ export interface Jest { * Creates a mock function. Optionally takes a mock implementation. */ fn: ModuleMocker['fn']; + // TODO remove `genMockFromModule()` in Jest 30 /** * Given the name of a module, use the automatic mocking system to generate a * mocked version of the module for you. From 1f0bf66fbdfa08047572737ee2cbc86ab17b328c Mon Sep 17 00:00:00 2001 From: Tom Mrazauskas Date: Sat, 3 Sep 2022 13:44:35 +0300 Subject: [PATCH 12/13] add type test --- packages/jest-types/__typetests__/jest.test.ts | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/packages/jest-types/__typetests__/jest.test.ts b/packages/jest-types/__typetests__/jest.test.ts index 997aad9cac69..188fe697072c 100644 --- a/packages/jest-types/__typetests__/jest.test.ts +++ b/packages/jest-types/__typetests__/jest.test.ts @@ -62,7 +62,15 @@ expectError(jest.autoMockOff(true)); expectType(jest.autoMockOn()); expectError(jest.autoMockOn(false)); +const someModule = { + methodA: () => {}, + propertyB: 'B', +}; + expectType(jest.createMockFromModule('moduleName')); +expectType>( + jest.createMockFromModule('moduleName'), +); expectError(jest.createMockFromModule()); expectType(jest.deepUnmock('moduleName')); From af670f7e53f4bd0b3ce58c89ce2fc05db24c1965 Mon Sep 17 00:00:00 2001 From: Tom Mrazauskas Date: Sat, 3 Sep 2022 13:52:59 +0300 Subject: [PATCH 13/13] add changelog entry --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6373e35b745b..b5cfce30413a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ### Features +- `[@jest/environment, jest-runtime]` Allow passing a generic type argument to `jest.createMockFromModule()` method ([#13202](https://github.com/facebook/jest/pull/13202)) + ### Fixes ### Chore & Maintenance