Skip to content

Commit c6280e4

Browse files
jeysalSimenB
authored andcommitted
decentralize jest-mock types (#7971)
1 parent 2e41973 commit c6280e4

File tree

4 files changed

+45
-53
lines changed

4 files changed

+45
-53
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
- `[@jest/types]`: New package to handle shared types ([#7834](https://github.com/facebook/jest/pull/7834))
4040
- `[jest-util]`: Migrate to TypeScript ([#7844](https://github.com/facebook/jest/pull/7844))
4141
- `[jest-watcher]`: Migrate to TypeScript ([#7843](https://github.com/facebook/jest/pull/7843))
42-
- `[jest-mock]`: Migrate to TypeScript ([#7847](https://github.com/facebook/jest/pull/7847), [#7850](https://github.com/facebook/jest/pull/7850))
42+
- `[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))
4343
- `[jest-worker]`: Migrate to TypeScript ([#7853](https://github.com/facebook/jest/pull/7853))
4444
- `[jest-haste-map]`: Migrate to TypeScript ([#7854](https://github.com/facebook/jest/pull/7854), [#7951](https://github.com/facebook/jest/pull/7951))
4545
- `[docs]`: Fix image paths in SnapshotTesting.md for current and version 24 ([#7872](https://github.com/facebook/jest/pull/7872))

packages/jest-mock/src/index.ts

Lines changed: 44 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,36 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8-
import {Mocks} from '@jest/types';
9-
108
type Global = NodeJS.Global; // | Window – add once TS improves typings;
119

10+
namespace JestMock {
11+
export type ModuleMocker = ModuleMockerClass;
12+
export type MockFunctionMetadataType =
13+
| 'object'
14+
| 'array'
15+
| 'regexp'
16+
| 'function'
17+
| 'constant'
18+
| 'collection'
19+
| 'null'
20+
| 'undefined';
21+
22+
export type MockFunctionMetadata<
23+
T,
24+
Y extends unknown[],
25+
Type = MockFunctionMetadataType
26+
> = {
27+
ref?: number;
28+
members?: {[key: string]: MockFunctionMetadata<T, Y>};
29+
mockImpl?: (...args: Y) => T;
30+
name?: string;
31+
refID?: number;
32+
type?: Type;
33+
value?: T;
34+
length?: number;
35+
};
36+
}
37+
1238
/**
1339
* Possible types of a MockFunctionResult.
1440
* 'return': The call completed by returning normally.
@@ -273,7 +299,7 @@ function getObjectType(value: unknown): string {
273299
return Object.prototype.toString.apply(value).slice(8, -1);
274300
}
275301

276-
function getType(ref?: unknown): Mocks.MockFunctionMetadataType | null {
302+
function getType(ref?: unknown): JestMock.MockFunctionMetadataType | null {
277303
const typeName = getObjectType(ref);
278304
if (
279305
typeName === 'Function' ||
@@ -449,31 +475,31 @@ class ModuleMockerClass {
449475
}
450476

451477
private _makeComponent<T, Y extends unknown[]>(
452-
metadata: Mocks.MockFunctionMetadata<T, Y, 'object'>,
478+
metadata: JestMock.MockFunctionMetadata<T, Y, 'object'>,
453479
restore?: () => void,
454480
): Object;
455481
private _makeComponent<T, Y extends unknown[]>(
456-
metadata: Mocks.MockFunctionMetadata<T, Y, 'array'>,
482+
metadata: JestMock.MockFunctionMetadata<T, Y, 'array'>,
457483
restore?: () => void,
458484
): Array<unknown>;
459485
private _makeComponent<T, Y extends unknown[]>(
460-
metadata: Mocks.MockFunctionMetadata<T, Y, 'regexp'>,
486+
metadata: JestMock.MockFunctionMetadata<T, Y, 'regexp'>,
461487
restore?: () => void,
462488
): RegExp;
463489
private _makeComponent<T, Y extends unknown[]>(
464-
metadata: Mocks.MockFunctionMetadata<
490+
metadata: JestMock.MockFunctionMetadata<
465491
T,
466492
Y,
467493
'constant' | 'collection' | 'null' | 'undefined'
468494
>,
469495
restore?: () => void,
470496
): T;
471497
private _makeComponent<T, Y extends unknown[]>(
472-
metadata: Mocks.MockFunctionMetadata<T, Y, 'function'>,
498+
metadata: JestMock.MockFunctionMetadata<T, Y, 'function'>,
473499
restore?: () => void,
474500
): Mock<T, Y>;
475501
private _makeComponent<T, Y extends unknown[]>(
476-
metadata: Mocks.MockFunctionMetadata<T, Y>,
502+
metadata: JestMock.MockFunctionMetadata<T, Y>,
477503
restore?: () => void,
478504
): Object | Array<unknown> | RegExp | T | undefined | Mock<T, Y> {
479505
if (metadata.type === 'object') {
@@ -719,7 +745,7 @@ class ModuleMockerClass {
719745
}
720746

721747
private _createMockFunction<T, Y extends unknown[]>(
722-
metadata: Mocks.MockFunctionMetadata<T, Y>,
748+
metadata: JestMock.MockFunctionMetadata<T, Y>,
723749
mockConstructor: Function,
724750
): Function {
725751
let name = metadata.name;
@@ -779,7 +805,7 @@ class ModuleMockerClass {
779805
}
780806

781807
private _generateMock<T, Y extends unknown[]>(
782-
metadata: Mocks.MockFunctionMetadata<T, Y>,
808+
metadata: JestMock.MockFunctionMetadata<T, Y>,
783809
callbacks: Array<Function>,
784810
refs: {
785811
[key: string]:
@@ -829,7 +855,7 @@ class ModuleMockerClass {
829855
* getMetadata method of this module.
830856
*/
831857
generateFromMetadata<T, Y extends unknown[]>(
832-
_metadata: Mocks.MockFunctionMetadata<T, Y>,
858+
_metadata: JestMock.MockFunctionMetadata<T, Y>,
833859
): Mock<T, Y> {
834860
const callbacks: Function[] = [];
835861
const refs = {};
@@ -845,7 +871,7 @@ class ModuleMockerClass {
845871
getMetadata<T, Y extends unknown[]>(
846872
component: T,
847873
_refs?: Map<T, number>,
848-
): Mocks.MockFunctionMetadata<T, Y> | null {
874+
): JestMock.MockFunctionMetadata<T, Y> | null {
849875
const refs = _refs || new Map<T, number>();
850876
const ref = refs.get(component);
851877
if (ref != null) {
@@ -857,7 +883,7 @@ class ModuleMockerClass {
857883
return null;
858884
}
859885

860-
const metadata: Mocks.MockFunctionMetadata<T, Y> = {type};
886+
const metadata: JestMock.MockFunctionMetadata<T, Y> = {type};
861887
if (
862888
type === 'constant' ||
863889
type === 'collection' ||
@@ -880,7 +906,7 @@ class ModuleMockerClass {
880906
refs.set(component, metadata.refID);
881907

882908
let members: {
883-
[key: string]: Mocks.MockFunctionMetadata<T, Y>;
909+
[key: string]: JestMock.MockFunctionMetadata<T, Y>;
884910
} | null = null;
885911
// Leave arrays alone
886912
if (type !== 'array') {
@@ -1077,7 +1103,6 @@ class ModuleMockerClass {
10771103
}
10781104
}
10791105

1080-
// TODO: bring this type export back once done with TS migration
1081-
// export type ModuleMocker = ModuleMockerClass;
1082-
1083-
export = new ModuleMockerClass(global);
1106+
/* eslint-disable-next-line no-redeclare */
1107+
const JestMock = new ModuleMockerClass(global);
1108+
export = JestMock;

packages/jest-types/src/Mocks.ts

Lines changed: 0 additions & 31 deletions
This file was deleted.

packages/jest-types/src/index.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import * as Config from './Config';
99
import * as Console from './Console';
1010
import * as Matchers from './Matchers';
11-
import * as Mocks from './Mocks';
1211
import * as PrettyFormat from './PrettyFormat';
1312
import * as SourceMaps from './SourceMaps';
1413
import * as TestResult from './TestResult';
@@ -19,7 +18,6 @@ export {
1918
Config,
2019
Console,
2120
Matchers,
22-
Mocks,
2321
PrettyFormat,
2422
SourceMaps,
2523
TestResult,

0 commit comments

Comments
 (0)