From e52c441deaafe865e82cc0fa6096eac4b001d12b Mon Sep 17 00:00:00 2001 From: Tim Seckinger Date: Sun, 24 Feb 2019 17:30:52 +0100 Subject: [PATCH 1/2] decentralize jest-mock types --- packages/jest-mock/src/index.ts | 63 ++++++++++++++++++++++---------- packages/jest-types/src/Mocks.ts | 31 ---------------- packages/jest-types/src/index.ts | 2 - 3 files changed, 44 insertions(+), 52 deletions(-) delete mode 100644 packages/jest-types/src/Mocks.ts diff --git a/packages/jest-mock/src/index.ts b/packages/jest-mock/src/index.ts index 6a620ed27efe..a23a49d2fe36 100644 --- a/packages/jest-mock/src/index.ts +++ b/packages/jest-mock/src/index.ts @@ -5,10 +5,36 @@ * LICENSE file in the root directory of this source tree. */ -import {Mocks} from '@jest/types'; - type Global = NodeJS.Global; // | Window – add once TS improves typings; +namespace JestMock { + export type ModuleMocker = ModuleMockerClass; + export type MockFunctionMetadataType = + | 'object' + | 'array' + | 'regexp' + | 'function' + | 'constant' + | 'collection' + | 'null' + | 'undefined'; + + export type MockFunctionMetadata< + T, + Y extends unknown[], + Type = MockFunctionMetadataType + > = { + ref?: number; + members?: {[key: string]: MockFunctionMetadata}; + mockImpl?: (...args: Y) => T; + name?: string; + refID?: number; + type?: Type; + value?: T; + length?: number; + }; +} + /** * Possible types of a MockFunctionResult. * 'return': The call completed by returning normally. @@ -273,7 +299,7 @@ function getObjectType(value: unknown): string { return Object.prototype.toString.apply(value).slice(8, -1); } -function getType(ref?: unknown): Mocks.MockFunctionMetadataType | null { +function getType(ref?: unknown): JestMock.MockFunctionMetadataType | null { const typeName = getObjectType(ref); if ( typeName === 'Function' || @@ -449,19 +475,19 @@ class ModuleMockerClass { } private _makeComponent( - metadata: Mocks.MockFunctionMetadata, + metadata: JestMock.MockFunctionMetadata, restore?: () => void, ): Object; private _makeComponent( - metadata: Mocks.MockFunctionMetadata, + metadata: JestMock.MockFunctionMetadata, restore?: () => void, ): Array; private _makeComponent( - metadata: Mocks.MockFunctionMetadata, + metadata: JestMock.MockFunctionMetadata, restore?: () => void, ): RegExp; private _makeComponent( - metadata: Mocks.MockFunctionMetadata< + metadata: JestMock.MockFunctionMetadata< T, Y, 'constant' | 'collection' | 'null' | 'undefined' @@ -469,11 +495,11 @@ class ModuleMockerClass { restore?: () => void, ): T; private _makeComponent( - metadata: Mocks.MockFunctionMetadata, + metadata: JestMock.MockFunctionMetadata, restore?: () => void, ): Mock; private _makeComponent( - metadata: Mocks.MockFunctionMetadata, + metadata: JestMock.MockFunctionMetadata, restore?: () => void, ): Object | Array | RegExp | T | undefined | Mock { if (metadata.type === 'object') { @@ -719,7 +745,7 @@ class ModuleMockerClass { } private _createMockFunction( - metadata: Mocks.MockFunctionMetadata, + metadata: JestMock.MockFunctionMetadata, mockConstructor: Function, ): Function { let name = metadata.name; @@ -779,7 +805,7 @@ class ModuleMockerClass { } private _generateMock( - metadata: Mocks.MockFunctionMetadata, + metadata: JestMock.MockFunctionMetadata, callbacks: Array, refs: { [key: string]: @@ -829,7 +855,7 @@ class ModuleMockerClass { * getMetadata method of this module. */ generateFromMetadata( - _metadata: Mocks.MockFunctionMetadata, + _metadata: JestMock.MockFunctionMetadata, ): Mock { const callbacks: Function[] = []; const refs = {}; @@ -845,7 +871,7 @@ class ModuleMockerClass { getMetadata( component: T, _refs?: Map, - ): Mocks.MockFunctionMetadata | null { + ): JestMock.MockFunctionMetadata | null { const refs = _refs || new Map(); const ref = refs.get(component); if (ref != null) { @@ -857,7 +883,7 @@ class ModuleMockerClass { return null; } - const metadata: Mocks.MockFunctionMetadata = {type}; + const metadata: JestMock.MockFunctionMetadata = {type}; if ( type === 'constant' || type === 'collection' || @@ -880,7 +906,7 @@ class ModuleMockerClass { refs.set(component, metadata.refID); let members: { - [key: string]: Mocks.MockFunctionMetadata; + [key: string]: JestMock.MockFunctionMetadata; } | null = null; // Leave arrays alone if (type !== 'array') { @@ -1077,7 +1103,6 @@ class ModuleMockerClass { } } -// TODO: bring this type export back once done with TS migration -// export type ModuleMocker = ModuleMockerClass; - -export = new ModuleMockerClass(global); +/* eslint-disable-next-line no-redeclare */ +const JestMock = new ModuleMockerClass(global); +export = JestMock; diff --git a/packages/jest-types/src/Mocks.ts b/packages/jest-types/src/Mocks.ts deleted file mode 100644 index 36e28a49ca01..000000000000 --- a/packages/jest-types/src/Mocks.ts +++ /dev/null @@ -1,31 +0,0 @@ -/** - * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - */ - -export type MockFunctionMetadataType = - | 'object' - | 'array' - | 'regexp' - | 'function' - | 'constant' - | 'collection' - | 'null' - | 'undefined'; - -export type MockFunctionMetadata< - T, - Y extends unknown[], - Type = MockFunctionMetadataType -> = { - ref?: number; - members?: {[key: string]: MockFunctionMetadata}; - mockImpl?: (...args: Y) => T; - name?: string; - refID?: number; - type?: Type; - value?: T; - length?: number; -}; diff --git a/packages/jest-types/src/index.ts b/packages/jest-types/src/index.ts index ed4c3acd0eac..5e38b8adc071 100644 --- a/packages/jest-types/src/index.ts +++ b/packages/jest-types/src/index.ts @@ -8,7 +8,6 @@ import * as Config from './Config'; import * as Console from './Console'; import * as Matchers from './Matchers'; -import * as Mocks from './Mocks'; import * as PrettyFormat from './PrettyFormat'; import * as Snapshot from './Snapshot'; import * as SourceMaps from './SourceMaps'; @@ -20,7 +19,6 @@ export { Config, Console, Matchers, - Mocks, PrettyFormat, Snapshot, SourceMaps, From 5e7248db076997044825957564f1d1ca8f4b9002 Mon Sep 17 00:00:00 2001 From: Tim Seckinger Date: Sun, 24 Feb 2019 18:38:12 +0100 Subject: [PATCH 2/2] Update CHANGELOG.md --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 227365427b90..3ae8ea7521dd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -39,7 +39,7 @@ - `[@jest/types]`: New package to handle shared types ([#7834](https://github.com/facebook/jest/pull/7834)) - `[jest-util]`: Migrate to TypeScript ([#7844](https://github.com/facebook/jest/pull/7844)) - `[jest-watcher]`: Migrate to TypeScript ([#7843](https://github.com/facebook/jest/pull/7843)) -- `[jest-mock]`: Migrate to TypeScript ([#7847](https://github.com/facebook/jest/pull/7847), [#7850](https://github.com/facebook/jest/pull/7850)) +- `[jest-mock]`: Migrate to TypeScript ([#7847](https://github.com/facebook/jest/pull/7847), [#7850](https://github.com/facebook/jest/pull/7850), [#7971](https://github.com/facebook/jest/pull/7971)) - `[jest-worker]`: Migrate to TypeScript ([#7853](https://github.com/facebook/jest/pull/7853)) - `[jest-haste-map]`: Migrate to TypeScript ([#7854](https://github.com/facebook/jest/pull/7854), [#7951](https://github.com/facebook/jest/pull/7951)) - `[docs]`: Fix image paths in SnapshotTesting.md for current and version 24 ([#7872](https://github.com/facebook/jest/pull/7872))