1-
21/**
32 * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
43 *
@@ -18,7 +17,11 @@ export type MockFunctionMetadataType =
1817 | 'null'
1918 | 'undefined' ;
2019
21- export type MockFunctionMetadata < T , Y extends Array < unknown > , Type = MockFunctionMetadataType , > = {
20+ export type MockFunctionMetadata <
21+ T ,
22+ Y extends Array < unknown > ,
23+ Type = MockFunctionMetadataType ,
24+ > = {
2225 ref ?: number ;
2326 members ?: Record < string , MockFunctionMetadata < T , Y > > ;
2427 mockImpl ?: ( ...args : Y ) => T ;
@@ -29,56 +32,75 @@ export type MockFunctionMetadata< T, Y extends Array<unknown>, Type = MockFunct
2932 length ?: number ;
3033} ;
3134
32- export type MockableFunction = ( ...args : Array < any > ) => any
33- export type MethodKeysOf < T > = { [ K in keyof T ] : T [ K ] extends MockableFunction ? K : never } [ keyof T ]
34- export type PropertyKeysOf < T > = { [ K in keyof T ] : T [ K ] extends MockableFunction ? never : K } [ keyof T ]
35-
36- export type ArgumentsOf < T > = T extends ( ...args : infer A ) => any ? A : never
37-
38- export type ConstructorArgumentsOf < T > = T extends new ( ...args : infer A ) => any ? A : never
39- export type MaybeMockedConstructor < T > = T extends new ( ...args :Array < any > ) => infer R
35+ export type MockableFunction = ( ...args : Array < any > ) => any ;
36+ export type MethodKeysOf < T > = {
37+ [ K in keyof T ] : T [ K ] extends MockableFunction ? K : never ;
38+ } [ keyof T ] ;
39+ export type PropertyKeysOf < T > = {
40+ [ K in keyof T ] : T [ K ] extends MockableFunction ? never : K ;
41+ } [ keyof T ] ;
42+
43+ export type ArgumentsOf < T > = T extends ( ...args : infer A ) => any ? A : never ;
44+
45+ export type ConstructorArgumentsOf < T > = T extends new ( ...args : infer A ) => any
46+ ? A
47+ : never ;
48+ export type MaybeMockedConstructor < T > = T extends new (
49+ ...args : Array < any >
50+ ) => infer R
4051 ? MockInstance < R , ConstructorArgumentsOf < T > >
41- : T
42- export type MockedFunction < T extends MockableFunction > = MockWithArgs < T > & { [ K in keyof T ] : T [ K ] }
43- export type MockedFunctionDeep < T extends MockableFunction > = MockWithArgs < T > & MockedObjectDeep < T >
52+ : T ;
53+ export type MockedFunction < T extends MockableFunction > = MockWithArgs < T > & {
54+ [ K in keyof T ] : T [ K ] ;
55+ } ;
56+ export type MockedFunctionDeep < T extends MockableFunction > = MockWithArgs < T > &
57+ MockedObjectDeep < T > ;
4458export type MockedObject < T > = MaybeMockedConstructor < T > & {
45- [ K in MethodKeysOf < T > ] : T [ K ] extends MockableFunction ? MockedFunction < T [ K ] > : T [ K ]
46- } & { [ K in PropertyKeysOf < T > ] : T [ K ] }
59+ [ K in MethodKeysOf < T > ] : T [ K ] extends MockableFunction
60+ ? MockedFunction < T [ K ] >
61+ : T [ K ] ;
62+ } & { [ K in PropertyKeysOf < T > ] : T [ K ] } ;
4763export type MockedObjectDeep < T > = MaybeMockedConstructor < T > & {
48- [ K in MethodKeysOf < T > ] : T [ K ] extends MockableFunction ? MockedFunctionDeep < T [ K ] > : T [ K ]
49- } & { [ K in PropertyKeysOf < T > ] : MaybeMockedDeep < T [ K ] > }
64+ [ K in MethodKeysOf < T > ] : T [ K ] extends MockableFunction
65+ ? MockedFunctionDeep < T [ K ] >
66+ : T [ K ] ;
67+ } & { [ K in PropertyKeysOf < T > ] : MaybeMockedDeep < T [ K ] > } ;
5068
5169export type MaybeMockedDeep < T > = T extends MockableFunction
5270 ? MockedFunctionDeep < T >
53- : T extends object
71+ : T extends object
5472 ? MockedObjectDeep < T >
55- : T
73+ : T ;
5674
57- export type MaybeMocked < T > = T extends MockableFunction ? MockedFunction < T > : T extends object ? MockedObject < T > : T
75+ export type MaybeMocked < T > = T extends MockableFunction
76+ ? MockedFunction < T >
77+ : T extends object
78+ ? MockedObject < T >
79+ : T ;
5880
5981export type ArgsType < T > = T extends ( ...args : infer A ) => any ? A : never ;
6082export type Mocked < T > = {
6183 [ P in keyof T ] : T [ P ] extends ( ...args : Array < any > ) => any
62- ? MockInstance < ReturnType < T [ P ] > , ArgsType < T [ P ] > >
63- : T [ P ] extends Constructable
64- ? MockedClass < T [ P ] >
65- : T [ P ]
66- } &
67- T ;
84+ ? MockInstance < ReturnType < T [ P ] > , ArgsType < T [ P ] > >
85+ : T [ P ] extends Constructable
86+ ? MockedClass < T [ P ] >
87+ : T [ P ] ;
88+ } & T ;
6889export type MockedClass < T extends Constructable > = MockInstance <
69- InstanceType < T > ,
70- T extends new ( ...args : infer P ) => any ? P : never
71- > & {
72- prototype : T extends { prototype : any } ? Mocked < T [ 'prototype' ] > : never ;
73- } & T ;
90+ InstanceType < T > ,
91+ T extends new ( ...args : infer P ) => any ? P : never
92+ > & {
93+ prototype : T extends { prototype : any } ? Mocked < T [ 'prototype' ] > : never ;
94+ } & T ;
7495
7596export interface Constructable {
76- new ( ...args : Array < any > ) : any ;
77- }
97+ new ( ...args : Array < any > ) : any ;
98+ }
7899
79- export interface MockWithArgs < T extends MockableFunction > extends MockInstance < ReturnType < T > , ArgumentsOf < T > > {
80- new ( ...args : ConstructorArgumentsOf < T > ) : T
81- ( ...args : ArgumentsOf < T > ) : ReturnType < T >
100+ export interface MockWithArgs < T extends MockableFunction >
101+ extends MockInstance < ReturnType < T > , ArgumentsOf < T > > {
102+ new ( ...args : ConstructorArgumentsOf < T > ) : T ;
103+ ( ...args : ArgumentsOf < T > ) : ReturnType < T > ;
82104}
83105
84106export interface Mock < T , Y extends Array < unknown > = Array < unknown > >
@@ -1158,21 +1180,19 @@ export class ModuleMocker {
11581180 private _typeOf ( value : any ) : string {
11591181 return value == null ? '' + value : typeof value ;
11601182 }
1161-
1162- // the typings test helper
1163- mocked < T > ( item : T , deep ?: false ) : MaybeMocked < T >
1164-
1165- mocked < T > ( item : T , deep : true ) : MaybeMockedDeep < T >
11661183
1167- mocked < T > ( item : T , _deep = false ) : MaybeMocked < T > | MaybeMockedDeep < T > {
1184+ // the typings test helper
1185+ mocked < T > ( item : T , deep ?: false ) : MaybeMocked < T > ;
11681186
1169- return item as any
1187+ mocked < T > ( item : T , deep : true ) : MaybeMockedDeep < T > ;
11701188
1171- }
1189+ mocked < T > ( item : T , _deep = false ) : MaybeMocked < T > | MaybeMockedDeep < T > {
1190+ return item as any ;
1191+ }
11721192}
11731193
11741194const JestMock = new ModuleMocker ( global as unknown as typeof globalThis ) ;
11751195
11761196export const fn = JestMock . fn . bind ( JestMock ) ;
11771197export const spyOn = JestMock . spyOn . bind ( JestMock ) ;
1178- export const mocked = JestMock . mocked . bind ( JestMock ) ;
1198+ export const mocked = JestMock . mocked . bind ( JestMock ) ;
0 commit comments