UNPKG

11.1 kBTypeScriptView Raw
1/**
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 */
7export declare type ClassLike = {
8 new (...args: any): any;
9};
10
11export declare type ConstructorLikeKeys<T> = keyof {
12 [K in keyof T as Required<T>[K] extends ClassLike ? K : never]: T[K];
13};
14
15export declare const fn: <T extends FunctionLike = UnknownFunction>(
16 implementation?: T | undefined,
17) => Mock<T>;
18
19export declare type FunctionLike = (...args: any) => any;
20
21export declare type MethodLikeKeys<T> = keyof {
22 [K in keyof T as Required<T>[K] extends FunctionLike ? K : never]: T[K];
23};
24
25/**
26 * All what the internal typings need is to be sure that we have any-function.
27 * `FunctionLike` type ensures that and helps to constrain the type as well.
28 * The default of `UnknownFunction` makes sure that `any`s do not leak to the
29 * user side. For instance, calling `fn()` without implementation will return
30 * a mock of `(...args: Array<unknown>) => unknown` type. If implementation
31 * is provided, its typings are inferred correctly.
32 */
33export declare interface Mock<T extends FunctionLike = UnknownFunction>
34 extends Function,
35 MockInstance<T> {
36 new (...args: Parameters<T>): ReturnType<T>;
37 (...args: Parameters<T>): ReturnType<T>;
38}
39
40export declare type Mocked<T> = T extends ClassLike
41 ? MockedClass<T>
42 : T extends FunctionLike
43 ? MockedFunction<T>
44 : T extends object
45 ? MockedObject<T>
46 : T;
47
48export declare const mocked: {
49 <T extends object>(
50 source: T,
51 options?: {
52 shallow: false;
53 },
54 ): Mocked<T>;
55 <T_1 extends object>(
56 source: T_1,
57 options: {
58 shallow: true;
59 },
60 ): MockedShallow<T_1>;
61};
62
63export declare type MockedClass<T extends ClassLike> = MockInstance<
64 (...args: ConstructorParameters<T>) => Mocked<InstanceType<T>>
65> &
66 MockedObject<T>;
67
68export declare type MockedFunction<T extends FunctionLike> = MockInstance<T> &
69 MockedObject<T>;
70
71declare type MockedFunctionShallow<T extends FunctionLike> = MockInstance<T> &
72 T;
73
74export declare type MockedObject<T extends object> = {
75 [K in keyof T]: T[K] extends ClassLike
76 ? MockedClass<T[K]>
77 : T[K] extends FunctionLike
78 ? MockedFunction<T[K]>
79 : T[K] extends object
80 ? MockedObject<T[K]>
81 : T[K];
82} & T;
83
84declare type MockedObjectShallow<T extends object> = {
85 [K in keyof T]: T[K] extends ClassLike
86 ? MockedClass<T[K]>
87 : T[K] extends FunctionLike
88 ? MockedFunctionShallow<T[K]>
89 : T[K];
90} & T;
91
92export declare type MockedShallow<T> = T extends ClassLike
93 ? MockedClass<T>
94 : T extends FunctionLike
95 ? MockedFunctionShallow<T>
96 : T extends object
97 ? MockedObjectShallow<T>
98 : T;
99
100export declare type MockFunctionMetadata<
101 T = unknown,
102 MetadataType = MockMetadataType,
103> = MockMetadata<T, MetadataType>;
104
105export declare type MockFunctionMetadataType = MockMetadataType;
106
107declare type MockFunctionResult<T extends FunctionLike = UnknownFunction> =
108 | MockFunctionResultIncomplete
109 | MockFunctionResultReturn<T>
110 | MockFunctionResultThrow;
111
112declare type MockFunctionResultIncomplete = {
113 type: 'incomplete';
114 /**
115 * Result of a single call to a mock function that has not yet completed.
116 * This occurs if you test the result from within the mock function itself,
117 * or from within a function that was called by the mock.
118 */
119 value: undefined;
120};
121
122declare type MockFunctionResultReturn<
123 T extends FunctionLike = UnknownFunction,
124> = {
125 type: 'return';
126 /**
127 * Result of a single call to a mock function that returned.
128 */
129 value: ReturnType<T>;
130};
131
132declare type MockFunctionResultThrow = {
133 type: 'throw';
134 /**
135 * Result of a single call to a mock function that threw.
136 */
137 value: unknown;
138};
139
140declare type MockFunctionState<T extends FunctionLike = UnknownFunction> = {
141 /**
142 * List of the call arguments of all calls that have been made to the mock.
143 */
144 calls: Array<Parameters<T>>;
145 /**
146 * List of all the object instances that have been instantiated from the mock.
147 */
148 instances: Array<ReturnType<T>>;
149 /**
150 * List of all the function contexts that have been applied to calls to the mock.
151 */
152 contexts: Array<ThisParameterType<T>>;
153 /**
154 * List of the call order indexes of the mock. Jest is indexing the order of
155 * invocations of all mocks in a test file. The index is starting with `1`.
156 */
157 invocationCallOrder: Array<number>;
158 /**
159 * List of the call arguments of the last call that was made to the mock.
160 * If the function was not called, it will return `undefined`.
161 */
162 lastCall?: Parameters<T>;
163 /**
164 * List of the results of all calls that have been made to the mock.
165 */
166 results: Array<MockFunctionResult<T>>;
167};
168
169export declare interface MockInstance<
170 T extends FunctionLike = UnknownFunction,
171> {
172 _isMockFunction: true;
173 _protoImpl: Function;
174 getMockImplementation(): T | undefined;
175 getMockName(): string;
176 mock: MockFunctionState<T>;
177 mockClear(): this;
178 mockReset(): this;
179 mockRestore(): void;
180 mockImplementation(fn: T): this;
181 mockImplementationOnce(fn: T): this;
182 withImplementation(fn: T, callback: () => Promise<unknown>): Promise<void>;
183 withImplementation(fn: T, callback: () => void): void;
184 mockName(name: string): this;
185 mockReturnThis(): this;
186 mockReturnValue(value: ReturnType<T>): this;
187 mockReturnValueOnce(value: ReturnType<T>): this;
188 mockResolvedValue(value: ResolveType<T>): this;
189 mockResolvedValueOnce(value: ResolveType<T>): this;
190 mockRejectedValue(value: RejectType<T>): this;
191 mockRejectedValueOnce(value: RejectType<T>): this;
192}
193
194export declare type MockMetadata<T, MetadataType = MockMetadataType> = {
195 ref?: number;
196 members?: Record<string, MockMetadata<T>>;
197 mockImpl?: T;
198 name?: string;
199 refID?: number;
200 type?: MetadataType;
201 value?: T;
202 length?: number;
203};
204
205export declare type MockMetadataType =
206 | 'object'
207 | 'array'
208 | 'regexp'
209 | 'function'
210 | 'constant'
211 | 'collection'
212 | 'null'
213 | 'undefined';
214
215export declare class ModuleMocker {
216 private readonly _environmentGlobal;
217 private _mockState;
218 private _mockConfigRegistry;
219 private _spyState;
220 private _invocationCallCounter;
221 /**
222 * @see README.md
223 * @param global Global object of the test environment, used to create
224 * mocks
225 */
226 constructor(global: typeof globalThis);
227 private _getSlots;
228 private _ensureMockConfig;
229 private _ensureMockState;
230 private _defaultMockConfig;
231 private _defaultMockState;
232 private _makeComponent;
233 private _createMockFunction;
234 private _generateMock;
235 /**
236 * Check whether the given property of an object has been already replaced.
237 */
238 private _findReplacedProperty;
239 /**
240 * @see README.md
241 * @param metadata Metadata for the mock in the schema returned by the
242 * getMetadata method of this module.
243 */
244 generateFromMetadata<T>(metadata: MockMetadata<T>): Mocked<T>;
245 /**
246 * @see README.md
247 * @param component The component for which to retrieve metadata.
248 */
249 getMetadata<T = unknown>(
250 component: T,
251 _refs?: Map<T, number>,
252 ): MockMetadata<T> | null;
253 isMockFunction<T extends FunctionLike = UnknownFunction>(
254 fn: MockInstance<T>,
255 ): fn is MockInstance<T>;
256 isMockFunction<P extends Array<unknown>, R>(
257 fn: (...args: P) => R,
258 ): fn is Mock<(...args: P) => R>;
259 isMockFunction(fn: unknown): fn is Mock<UnknownFunction>;
260 fn<T extends FunctionLike = UnknownFunction>(implementation?: T): Mock<T>;
261 spyOn<
262 T extends object,
263 K extends PropertyLikeKeys<T>,
264 V extends Required<T>[K],
265 A extends 'get' | 'set',
266 >(
267 object: T,
268 methodKey: K,
269 accessType: A,
270 ): A extends 'get'
271 ? SpiedGetter<V>
272 : A extends 'set'
273 ? SpiedSetter<V>
274 : never;
275 spyOn<
276 T extends object,
277 K extends ConstructorLikeKeys<T> | MethodLikeKeys<T>,
278 V extends Required<T>[K],
279 >(
280 object: T,
281 methodKey: K,
282 ): V extends ClassLike | FunctionLike ? Spied<V> : never;
283 private _spyOnProperty;
284 replaceProperty<T extends object, K extends keyof T>(
285 object: T,
286 propertyKey: K,
287 value: T[K],
288 ): Replaced<T[K]>;
289 clearAllMocks(): void;
290 resetAllMocks(): void;
291 restoreAllMocks(): void;
292 private _typeOf;
293 mocked<T extends object>(
294 source: T,
295 options?: {
296 shallow: false;
297 },
298 ): Mocked<T>;
299 mocked<T extends object>(
300 source: T,
301 options: {
302 shallow: true;
303 },
304 ): MockedShallow<T>;
305}
306
307export declare type PropertyLikeKeys<T> = Exclude<
308 keyof T,
309 ConstructorLikeKeys<T> | MethodLikeKeys<T>
310>;
311
312declare type RejectType<T extends FunctionLike> =
313 ReturnType<T> extends PromiseLike<any> ? unknown : never;
314
315export declare interface Replaced<T = unknown> {
316 /**
317 * Restore property to its original value known at the time of mocking.
318 */
319 restore(): void;
320 /**
321 * Change the value of the property.
322 */
323 replaceValue(value: T): this;
324}
325
326export declare const replaceProperty: <T extends object, K extends keyof T>(
327 object: T,
328 propertyKey: K,
329 value: T[K],
330) => Replaced<T[K]>;
331
332declare type ResolveType<T extends FunctionLike> =
333 ReturnType<T> extends PromiseLike<infer U> ? U : never;
334
335export declare type Spied<T extends ClassLike | FunctionLike> =
336 T extends ClassLike
337 ? SpiedClass<T>
338 : T extends FunctionLike
339 ? SpiedFunction<T>
340 : never;
341
342export declare type SpiedClass<T extends ClassLike = UnknownClass> =
343 MockInstance<(...args: ConstructorParameters<T>) => InstanceType<T>>;
344
345export declare type SpiedFunction<T extends FunctionLike = UnknownFunction> =
346 MockInstance<(...args: Parameters<T>) => ReturnType<T>>;
347
348export declare type SpiedGetter<T> = MockInstance<() => T>;
349
350export declare type SpiedSetter<T> = MockInstance<(arg: T) => void>;
351
352export declare interface SpyInstance<T extends FunctionLike = UnknownFunction>
353 extends MockInstance<T> {}
354
355export declare const spyOn: {
356 <
357 T extends object,
358 K_2 extends Exclude<
359 keyof T,
360 | keyof {
361 [K in keyof T as Required<T>[K] extends ClassLike ? K : never]: T[K];
362 }
363 | keyof {
364 [K_1 in keyof T as Required<T>[K_1] extends FunctionLike
365 ? K_1
366 : never]: T[K_1];
367 }
368 >,
369 V extends Required<T>[K_2],
370 A extends 'set' | 'get',
371 >(
372 object: T,
373 methodKey: K_2,
374 accessType: A,
375 ): A extends 'get'
376 ? SpiedGetter<V>
377 : A extends 'set'
378 ? SpiedSetter<V>
379 : never;
380 <
381 T_1 extends object,
382 K_5 extends
383 | keyof {
384 [K_3 in keyof T_1 as Required<T_1>[K_3] extends ClassLike
385 ? K_3
386 : never]: T_1[K_3];
387 }
388 | keyof {
389 [K_4 in keyof T_1 as Required<T_1>[K_4] extends FunctionLike
390 ? K_4
391 : never]: T_1[K_4];
392 },
393 V_1 extends Required<T_1>[K_5],
394 >(
395 object: T_1,
396 methodKey: K_5,
397 ): V_1 extends ClassLike | FunctionLike ? Spied<V_1> : never;
398};
399
400export declare type UnknownClass = {
401 new (...args: Array<unknown>): unknown;
402};
403
404export declare type UnknownFunction = (...args: Array<unknown>) => unknown;
405
406export {};