1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 | export declare type ClassLike = {
|
8 | new (...args: any): any;
|
9 | };
|
10 |
|
11 | export declare type ConstructorLikeKeys<T> = keyof {
|
12 | [K in keyof T as Required<T>[K] extends ClassLike ? K : never]: T[K];
|
13 | };
|
14 |
|
15 | export declare const fn: <T extends FunctionLike = UnknownFunction>(
|
16 | implementation?: T | undefined,
|
17 | ) => Mock<T>;
|
18 |
|
19 | export declare type FunctionLike = (...args: any) => any;
|
20 |
|
21 | export declare type MethodLikeKeys<T> = keyof {
|
22 | [K in keyof T as Required<T>[K] extends FunctionLike ? K : never]: T[K];
|
23 | };
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 | export 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 |
|
40 | export 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 |
|
48 | export 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 |
|
63 | export declare type MockedClass<T extends ClassLike> = MockInstance<
|
64 | (...args: ConstructorParameters<T>) => Mocked<InstanceType<T>>
|
65 | > &
|
66 | MockedObject<T>;
|
67 |
|
68 | export declare type MockedFunction<T extends FunctionLike> = MockInstance<T> &
|
69 | MockedObject<T>;
|
70 |
|
71 | declare type MockedFunctionShallow<T extends FunctionLike> = MockInstance<T> &
|
72 | T;
|
73 |
|
74 | export 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 |
|
84 | declare 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 |
|
92 | export 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 |
|
100 | export declare type MockFunctionMetadata<
|
101 | T = unknown,
|
102 | MetadataType = MockMetadataType,
|
103 | > = MockMetadata<T, MetadataType>;
|
104 |
|
105 | export declare type MockFunctionMetadataType = MockMetadataType;
|
106 |
|
107 | declare type MockFunctionResult<T extends FunctionLike = UnknownFunction> =
|
108 | | MockFunctionResultIncomplete
|
109 | | MockFunctionResultReturn<T>
|
110 | | MockFunctionResultThrow;
|
111 |
|
112 | declare type MockFunctionResultIncomplete = {
|
113 | type: 'incomplete';
|
114 | |
115 |
|
116 |
|
117 |
|
118 |
|
119 | value: undefined;
|
120 | };
|
121 |
|
122 | declare type MockFunctionResultReturn<
|
123 | T extends FunctionLike = UnknownFunction,
|
124 | > = {
|
125 | type: 'return';
|
126 | |
127 |
|
128 |
|
129 | value: ReturnType<T>;
|
130 | };
|
131 |
|
132 | declare type MockFunctionResultThrow = {
|
133 | type: 'throw';
|
134 | |
135 |
|
136 |
|
137 | value: unknown;
|
138 | };
|
139 |
|
140 | declare type MockFunctionState<T extends FunctionLike = UnknownFunction> = {
|
141 | |
142 |
|
143 |
|
144 | calls: Array<Parameters<T>>;
|
145 | |
146 |
|
147 |
|
148 | instances: Array<ReturnType<T>>;
|
149 | |
150 |
|
151 |
|
152 | contexts: Array<ThisParameterType<T>>;
|
153 | |
154 |
|
155 |
|
156 |
|
157 | invocationCallOrder: Array<number>;
|
158 | |
159 |
|
160 |
|
161 |
|
162 | lastCall?: Parameters<T>;
|
163 | |
164 |
|
165 |
|
166 | results: Array<MockFunctionResult<T>>;
|
167 | };
|
168 |
|
169 | export 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 |
|
194 | export 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 |
|
205 | export declare type MockMetadataType =
|
206 | | 'object'
|
207 | | 'array'
|
208 | | 'regexp'
|
209 | | 'function'
|
210 | | 'constant'
|
211 | | 'collection'
|
212 | | 'null'
|
213 | | 'undefined';
|
214 |
|
215 | export declare class ModuleMocker {
|
216 | private readonly _environmentGlobal;
|
217 | private _mockState;
|
218 | private _mockConfigRegistry;
|
219 | private _spyState;
|
220 | private _invocationCallCounter;
|
221 | |
222 |
|
223 |
|
224 |
|
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 |
|
307 | export declare type PropertyLikeKeys<T> = Exclude<
|
308 | keyof T,
|
309 | ConstructorLikeKeys<T> | MethodLikeKeys<T>
|
310 | >;
|
311 |
|
312 | declare type RejectType<T extends FunctionLike> =
|
313 | ReturnType<T> extends PromiseLike<any> ? unknown : never;
|
314 |
|
315 | export declare interface Replaced<T = unknown> {
|
316 | |
317 |
|
318 |
|
319 | restore(): void;
|
320 | |
321 |
|
322 |
|
323 | replaceValue(value: T): this;
|
324 | }
|
325 |
|
326 | export 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 |
|
332 | declare type ResolveType<T extends FunctionLike> =
|
333 | ReturnType<T> extends PromiseLike<infer U> ? U : never;
|
334 |
|
335 | export declare type Spied<T extends ClassLike | FunctionLike> =
|
336 | T extends ClassLike
|
337 | ? SpiedClass<T>
|
338 | : T extends FunctionLike
|
339 | ? SpiedFunction<T>
|
340 | : never;
|
341 |
|
342 | export declare type SpiedClass<T extends ClassLike = UnknownClass> =
|
343 | MockInstance<(...args: ConstructorParameters<T>) => InstanceType<T>>;
|
344 |
|
345 | export declare type SpiedFunction<T extends FunctionLike = UnknownFunction> =
|
346 | MockInstance<(...args: Parameters<T>) => ReturnType<T>>;
|
347 |
|
348 | export declare type SpiedGetter<T> = MockInstance<() => T>;
|
349 |
|
350 | export declare type SpiedSetter<T> = MockInstance<(arg: T) => void>;
|
351 |
|
352 | export declare interface SpyInstance<T extends FunctionLike = UnknownFunction>
|
353 | extends MockInstance<T> {}
|
354 |
|
355 | export 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 |
|
400 | export declare type UnknownClass = {
|
401 | new (...args: Array<unknown>): unknown;
|
402 | };
|
403 |
|
404 | export declare type UnknownFunction = (...args: Array<unknown>) => unknown;
|
405 |
|
406 | export {};
|