index.d.ts 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. */
  7. export declare type MockFunctionMetadataType = 'object' | 'array' | 'regexp' | 'function' | 'constant' | 'collection' | 'null' | 'undefined';
  8. export 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. };
  18. export declare type MockableFunction = (...args: Array<any>) => any;
  19. export declare type MethodKeysOf<T> = {
  20. [K in keyof T]: T[K] extends MockableFunction ? K : never;
  21. }[keyof T];
  22. export declare type PropertyKeysOf<T> = {
  23. [K in keyof T]: T[K] extends MockableFunction ? never : K;
  24. }[keyof T];
  25. export declare type ArgumentsOf<T> = T extends (...args: infer A) => any ? A : never;
  26. export declare type ConstructorArgumentsOf<T> = T extends new (...args: infer A) => any ? A : never;
  27. export declare type MaybeMockedConstructor<T> = T extends new (...args: Array<any>) => infer R ? MockInstance<R, ConstructorArgumentsOf<T>> : T;
  28. export declare type MockedFunction<T extends MockableFunction> = MockWithArgs<T> & {
  29. [K in keyof T]: T[K];
  30. };
  31. export declare type MockedFunctionDeep<T extends MockableFunction> = MockWithArgs<T> & MockedObjectDeep<T>;
  32. export 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. };
  37. export 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. };
  42. export declare type MaybeMockedDeep<T> = T extends MockableFunction ? MockedFunctionDeep<T> : T extends object ? MockedObjectDeep<T> : T;
  43. export declare type MaybeMocked<T> = T extends MockableFunction ? MockedFunction<T> : T extends object ? MockedObject<T> : T;
  44. export declare type ArgsType<T> = T extends (...args: infer A) => any ? A : never;
  45. export 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;
  48. export 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;
  53. export interface Constructable {
  54. new (...args: Array<any>): any;
  55. }
  56. export interface MockWithArgs<T extends MockableFunction> extends MockInstance<ReturnType<T>, ArgumentsOf<T>> {
  57. new (...args: ConstructorArgumentsOf<T>): T;
  58. (...args: ArgumentsOf<T>): ReturnType<T>;
  59. }
  60. export interface Mock<T, Y extends Array<unknown> = Array<unknown>> extends Function, MockInstance<T, Y> {
  61. new (...args: Y): T;
  62. (...args: Y): T;
  63. }
  64. export interface SpyInstance<T, Y extends Array<unknown>> extends MockInstance<T, Y> {
  65. }
  66. export 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. }
  88. declare 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. */
  97. declare type MockFunctionResultType = 'return' | 'throw' | 'incomplete';
  98. /**
  99. * Represents the result of a single call to a mock function.
  100. */
  101. declare 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. };
  112. declare type MockFunctionState<T, Y extends Array<unknown>> = {
  113. calls: Array<Y>;
  114. instances: Array<T>;
  115. invocationCallOrder: Array<number>;
  116. /**
  117. * Getter for retrieving the last call arguments
  118. */
  119. lastCall?: Y;
  120. /**
  121. * List of results of calls to the mock function.
  122. */
  123. results: Array<MockFunctionResult>;
  124. };
  125. declare type NonFunctionPropertyNames<T> = {
  126. [K in keyof T]: T[K] extends (...args: Array<any>) => any ? never : K;
  127. }[keyof T] & string;
  128. declare type FunctionPropertyNames<T> = {
  129. [K in keyof T]: T[K] extends (...args: Array<any>) => any ? K : never;
  130. }[keyof T] & string;
  131. export declare class ModuleMocker {
  132. private _environmentGlobal;
  133. private _mockState;
  134. private _mockConfigRegistry;
  135. private _spyState;
  136. private _invocationCallCounter;
  137. /**
  138. * @see README.md
  139. * @param global Global object of the test environment, used to create
  140. * mocks
  141. */
  142. constructor(global: typeof globalThis);
  143. private _getSlots;
  144. private _ensureMockConfig;
  145. private _ensureMockState;
  146. private _defaultMockConfig;
  147. private _defaultMockState;
  148. private _makeComponent;
  149. private _createMockFunction;
  150. private _generateMock;
  151. /**
  152. * @see README.md
  153. * @param _metadata Metadata for the mock in the schema returned by the
  154. * getMetadata method of this module.
  155. */
  156. generateFromMetadata<T, Y extends Array<unknown>>(_metadata: MockFunctionMetadata<T, Y>): Mock<T, Y>;
  157. /**
  158. * @see README.md
  159. * @param component The component for which to retrieve metadata.
  160. */
  161. getMetadata<T, Y extends Array<unknown>>(component: T, _refs?: Map<T, number>): MockFunctionMetadata<T, Y> | null;
  162. isMockFunction<T>(fn: unknown): fn is Mock<T>;
  163. fn<T, Y extends Array<unknown>>(implementation?: (...args: Y) => T): Mock<T, Y>;
  164. spyOn<T extends {}, M extends NonFunctionPropertyNames<T>>(object: T, methodName: M, accessType: 'get'): SpyInstance<T[M], []>;
  165. spyOn<T extends {}, M extends NonFunctionPropertyNames<T>>(object: T, methodName: M, accessType: 'set'): SpyInstance<void, [T[M]]>;
  166. 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;
  167. private _spyOnProperty;
  168. clearAllMocks(): void;
  169. resetAllMocks(): void;
  170. restoreAllMocks(): void;
  171. private _typeOf;
  172. mocked<T>(item: T, deep?: false): MaybeMocked<T>;
  173. mocked<T>(item: T, deep: true): MaybeMockedDeep<T>;
  174. }
  175. export declare const fn: <T, Y extends unknown[]>(implementation?: ((...args: Y) => T) | undefined) => Mock<T, Y>;
  176. export declare const spyOn: {
  177. <T extends {}, M extends NonFunctionPropertyNames<T>>(object: T, methodName: M, accessType: 'get'): SpyInstance<T[M], []>;
  178. <T_2 extends {}, M_2 extends NonFunctionPropertyNames<T_2>>(object: T_2, methodName: M_2, accessType: 'set'): SpyInstance<void, [T_2[M_2]]>;
  179. <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;
  180. };
  181. export declare const mocked: {
  182. <T>(item: T, deep?: false | undefined): MaybeMocked<T>;
  183. <T_2>(item: T_2, deep: true): MaybeMockedDeep<T_2>;
  184. };
  185. export {};