UNPKG

8.24 kBTypeScriptView Raw
1/**
2 * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
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 MockFunctionMetadataType = 'object' | 'array' | 'regexp' | 'function' | 'constant' | 'collection' | 'null' | 'undefined';
8export declare type MockFunctionMetadata<T, Y extends Array<unknown>, Type = MockFunctionMetadataType> = {
9 ref?: number;
10 members?: Record<string, MockFunctionMetadata<T, Y>>;
11 mockImpl?: (...args: Y) => T;
12 name?: string;
13 refID?: number;
14 type?: Type;
15 value?: T;
16 length?: number;
17};
18export declare type MockableFunction = (...args: Array<any>) => any;
19export declare type MethodKeysOf<T> = {
20 [K in keyof T]: T[K] extends MockableFunction ? K : never;
21}[keyof T];
22export declare type PropertyKeysOf<T> = {
23 [K in keyof T]: T[K] extends MockableFunction ? never : K;
24}[keyof T];
25export declare type ArgumentsOf<T> = T extends (...args: infer A) => any ? A : never;
26export declare type ConstructorArgumentsOf<T> = T extends new (...args: infer A) => any ? A : never;
27export declare type MaybeMockedConstructor<T> = T extends new (...args: Array<any>) => infer R ? MockInstance<R, ConstructorArgumentsOf<T>> : T;
28export declare type MockedFunction<T extends MockableFunction> = MockWithArgs<T> & {
29 [K in keyof T]: T[K];
30};
31export declare type MockedFunctionDeep<T extends MockableFunction> = MockWithArgs<T> & MockedObjectDeep<T>;
32export declare type MockedObject<T> = MaybeMockedConstructor<T> & {
33 [K in MethodKeysOf<T>]: T[K] extends MockableFunction ? MockedFunction<T[K]> : T[K];
34} & {
35 [K in PropertyKeysOf<T>]: T[K];
36};
37export declare type MockedObjectDeep<T> = MaybeMockedConstructor<T> & {
38 [K in MethodKeysOf<T>]: T[K] extends MockableFunction ? MockedFunctionDeep<T[K]> : T[K];
39} & {
40 [K in PropertyKeysOf<T>]: MaybeMockedDeep<T[K]>;
41};
42export declare type MaybeMockedDeep<T> = T extends MockableFunction ? MockedFunctionDeep<T> : T extends object ? MockedObjectDeep<T> : T;
43export declare type MaybeMocked<T> = T extends MockableFunction ? MockedFunction<T> : T extends object ? MockedObject<T> : T;
44export declare type ArgsType<T> = T extends (...args: infer A) => any ? A : never;
45export declare type Mocked<T> = {
46 [P in keyof T]: T[P] extends (...args: Array<any>) => any ? MockInstance<ReturnType<T[P]>, ArgsType<T[P]>> : T[P] extends Constructable ? MockedClass<T[P]> : T[P];
47} & T;
48export declare type MockedClass<T extends Constructable> = MockInstance<InstanceType<T>, T extends new (...args: infer P) => any ? P : never> & {
49 prototype: T extends {
50 prototype: any;
51 } ? Mocked<T['prototype']> : never;
52} & T;
53export interface Constructable {
54 new (...args: Array<any>): any;
55}
56export interface MockWithArgs<T extends MockableFunction> extends MockInstance<ReturnType<T>, ArgumentsOf<T>> {
57 new (...args: ConstructorArgumentsOf<T>): T;
58 (...args: ArgumentsOf<T>): ReturnType<T>;
59}
60export interface Mock<T, Y extends Array<unknown> = Array<unknown>> extends Function, MockInstance<T, Y> {
61 new (...args: Y): T;
62 (...args: Y): T;
63}
64export interface SpyInstance<T, Y extends Array<unknown>> extends MockInstance<T, Y> {
65}
66export interface MockInstance<T, Y extends Array<unknown>> {
67 _isMockFunction: true;
68 _protoImpl: Function;
69 getMockName(): string;
70 getMockImplementation(): Function | undefined;
71 mock: MockFunctionState<T, Y>;
72 mockClear(): this;
73 mockReset(): this;
74 mockRestore(): void;
75 mockImplementation(fn: (...args: Y) => T): this;
76 mockImplementation(fn: () => Promise<T>): this;
77 mockImplementationOnce(fn: (...args: Y) => T): this;
78 mockImplementationOnce(fn: () => Promise<T>): this;
79 mockName(name: string): this;
80 mockReturnThis(): this;
81 mockReturnValue(value: T): this;
82 mockReturnValueOnce(value: T): this;
83 mockResolvedValue(value: Unpromisify<T>): this;
84 mockResolvedValueOnce(value: Unpromisify<T>): this;
85 mockRejectedValue(value: unknown): this;
86 mockRejectedValueOnce(value: unknown): this;
87}
88declare type Unpromisify<T> = T extends Promise<infer R> ? R : never;
89/**
90 * Possible types of a MockFunctionResult.
91 * 'return': The call completed by returning normally.
92 * 'throw': The call completed by throwing a value.
93 * 'incomplete': The call has not completed yet. This is possible if you read
94 * the mock function result from within the mock function itself
95 * (or a function called by the mock function).
96 */
97declare type MockFunctionResultType = 'return' | 'throw' | 'incomplete';
98/**
99 * Represents the result of a single call to a mock function.
100 */
101declare type MockFunctionResult = {
102 /**
103 * Indicates how the call completed.
104 */
105 type: MockFunctionResultType;
106 /**
107 * The value that was either thrown or returned by the function.
108 * Undefined when type === 'incomplete'.
109 */
110 value: unknown;
111};
112declare type MockFunctionState<T, Y extends Array<unknown>> = {
113 calls: Array<Y>;
114 instances: Array<T>;
115 invocationCallOrder: Array<number>;
116 /**
117 * List of results of calls to the mock function.
118 */
119 results: Array<MockFunctionResult>;
120};
121declare type NonFunctionPropertyNames<T> = {
122 [K in keyof T]: T[K] extends (...args: Array<any>) => any ? never : K;
123}[keyof T] & string;
124declare type FunctionPropertyNames<T> = {
125 [K in keyof T]: T[K] extends (...args: Array<any>) => any ? K : never;
126}[keyof T] & string;
127export declare class ModuleMocker {
128 private _environmentGlobal;
129 private _mockState;
130 private _mockConfigRegistry;
131 private _spyState;
132 private _invocationCallCounter;
133 /**
134 * @see README.md
135 * @param global Global object of the test environment, used to create
136 * mocks
137 */
138 constructor(global: typeof globalThis);
139 private _getSlots;
140 private _ensureMockConfig;
141 private _ensureMockState;
142 private _defaultMockConfig;
143 private _defaultMockState;
144 private _makeComponent;
145 private _createMockFunction;
146 private _generateMock;
147 /**
148 * @see README.md
149 * @param _metadata Metadata for the mock in the schema returned by the
150 * getMetadata method of this module.
151 */
152 generateFromMetadata<T, Y extends Array<unknown>>(_metadata: MockFunctionMetadata<T, Y>): Mock<T, Y>;
153 /**
154 * @see README.md
155 * @param component The component for which to retrieve metadata.
156 */
157 getMetadata<T, Y extends Array<unknown>>(component: T, _refs?: Map<T, number>): MockFunctionMetadata<T, Y> | null;
158 isMockFunction<T>(fn: unknown): fn is Mock<T>;
159 fn<T, Y extends Array<unknown>>(implementation?: (...args: Y) => T): Mock<T, Y>;
160 spyOn<T extends {}, M extends NonFunctionPropertyNames<T>>(object: T, methodName: M, accessType: 'get'): SpyInstance<T[M], []>;
161 spyOn<T extends {}, M extends NonFunctionPropertyNames<T>>(object: T, methodName: M, accessType: 'set'): SpyInstance<void, [T[M]]>;
162 spyOn<T extends {}, M extends FunctionPropertyNames<T>>(object: T, methodName: M): T[M] extends (...args: Array<any>) => any ? SpyInstance<ReturnType<T[M]>, Parameters<T[M]>> : never;
163 private _spyOnProperty;
164 clearAllMocks(): void;
165 resetAllMocks(): void;
166 restoreAllMocks(): void;
167 private _typeOf;
168 mocked<T>(item: T, deep?: false): MaybeMocked<T>;
169 mocked<T>(item: T, deep: true): MaybeMockedDeep<T>;
170}
171export declare const fn: <T, Y extends unknown[]>(implementation?: ((...args: Y) => T) | undefined) => Mock<T, Y>;
172export declare const spyOn: {
173 <T extends {}, M extends NonFunctionPropertyNames<T>>(object: T, methodName: M, accessType: 'get'): SpyInstance<T[M], []>;
174 <T_2 extends {}, M_2 extends NonFunctionPropertyNames<T_2>>(object: T_2, methodName: M_2, accessType: 'set'): SpyInstance<void, [T_2[M_2]]>;
175 <T_4 extends {}, M_4 extends FunctionPropertyNames<T_4>>(object: T_4, methodName: M_4): T_4[M_4] extends (...args: Array<any>) => any ? SpyInstance<ReturnType<T_4[M_4]>, Parameters<T_4[M_4]>> : never;
176};
177export declare const mocked: {
178 <T>(item: T, deep?: false | undefined): MaybeMocked<T>;
179 <T_2>(item: T_2, deep: true): MaybeMockedDeep<T_2>;
180};
181export {};