index.d.ts 10 KB

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