index.d.ts 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. /// <reference types="node" />
  8. declare type Global = NodeJS.Global;
  9. declare namespace JestMock {
  10. type ModuleMocker = ModuleMockerClass;
  11. type MockFunctionMetadataType = 'object' | 'array' | 'regexp' | 'function' | 'constant' | 'collection' | 'null' | 'undefined';
  12. type MockFunctionMetadata<T, Y extends Array<unknown>, Type = MockFunctionMetadataType> = {
  13. ref?: number;
  14. members?: Record<string, MockFunctionMetadata<T, Y>>;
  15. mockImpl?: (...args: Y) => T;
  16. name?: string;
  17. refID?: number;
  18. type?: Type;
  19. value?: T;
  20. length?: number;
  21. };
  22. interface Mock<T, Y extends Array<unknown> = Array<unknown>> extends Function, MockInstance<T, Y> {
  23. new (...args: Y): T;
  24. (...args: Y): T;
  25. }
  26. interface SpyInstance<T, Y extends Array<unknown>> extends MockInstance<T, Y> {
  27. }
  28. interface MockInstance<T, Y extends Array<unknown>> {
  29. _isMockFunction: true;
  30. _protoImpl: Function;
  31. getMockName(): string;
  32. getMockImplementation(): Function | undefined;
  33. mock: MockFunctionState<T, Y>;
  34. mockClear(): this;
  35. mockReset(): this;
  36. mockRestore(): void;
  37. mockImplementation(fn: (...args: Y) => T): this;
  38. mockImplementation(fn: () => Promise<T>): this;
  39. mockImplementationOnce(fn: (...args: Y) => T): this;
  40. mockImplementationOnce(fn: () => Promise<T>): this;
  41. mockName(name: string): this;
  42. mockReturnThis(): this;
  43. mockReturnValue(value: T): this;
  44. mockReturnValueOnce(value: T): this;
  45. mockResolvedValue(value: Unpromisify<T>): this;
  46. mockResolvedValueOnce(value: Unpromisify<T>): this;
  47. mockRejectedValue(value: unknown): this;
  48. mockRejectedValueOnce(value: unknown): this;
  49. }
  50. }
  51. declare type Unpromisify<T> = T extends Promise<infer R> ? R : never;
  52. /**
  53. * Possible types of a MockFunctionResult.
  54. * 'return': The call completed by returning normally.
  55. * 'throw': The call completed by throwing a value.
  56. * 'incomplete': The call has not completed yet. This is possible if you read
  57. * the mock function result from within the mock function itself
  58. * (or a function called by the mock function).
  59. */
  60. declare type MockFunctionResultType = 'return' | 'throw' | 'incomplete';
  61. /**
  62. * Represents the result of a single call to a mock function.
  63. */
  64. declare type MockFunctionResult = {
  65. /**
  66. * Indicates how the call completed.
  67. */
  68. type: MockFunctionResultType;
  69. /**
  70. * The value that was either thrown or returned by the function.
  71. * Undefined when type === 'incomplete'.
  72. */
  73. value: unknown;
  74. };
  75. declare type MockFunctionState<T, Y extends Array<unknown>> = {
  76. calls: Array<Y>;
  77. instances: Array<T>;
  78. invocationCallOrder: Array<number>;
  79. /**
  80. * List of results of calls to the mock function.
  81. */
  82. results: Array<MockFunctionResult>;
  83. };
  84. declare type NonFunctionPropertyNames<T> = {
  85. [K in keyof T]: T[K] extends (...args: Array<any>) => any ? never : K;
  86. }[keyof T] & string;
  87. declare type FunctionPropertyNames<T> = {
  88. [K in keyof T]: T[K] extends (...args: Array<any>) => any ? K : never;
  89. }[keyof T] & string;
  90. declare class ModuleMockerClass {
  91. private _environmentGlobal;
  92. private _mockState;
  93. private _mockConfigRegistry;
  94. private _spyState;
  95. private _invocationCallCounter;
  96. ModuleMocker: typeof ModuleMockerClass;
  97. /**
  98. * @see README.md
  99. * @param global Global object of the test environment, used to create
  100. * mocks
  101. */
  102. constructor(global: Global);
  103. private _getSlots;
  104. private _ensureMockConfig;
  105. private _ensureMockState;
  106. private _defaultMockConfig;
  107. private _defaultMockState;
  108. private _makeComponent;
  109. private _createMockFunction;
  110. private _generateMock;
  111. /**
  112. * @see README.md
  113. * @param _metadata Metadata for the mock in the schema returned by the
  114. * getMetadata method of this module.
  115. */
  116. generateFromMetadata<T, Y extends Array<unknown>>(_metadata: JestMock.MockFunctionMetadata<T, Y>): JestMock.Mock<T, Y>;
  117. /**
  118. * @see README.md
  119. * @param component The component for which to retrieve metadata.
  120. */
  121. getMetadata<T, Y extends Array<unknown>>(component: T, _refs?: Map<T, number>): JestMock.MockFunctionMetadata<T, Y> | null;
  122. isMockFunction<T>(fn: any): fn is JestMock.Mock<T>;
  123. fn<T, Y extends Array<unknown>>(implementation?: (...args: Y) => T): JestMock.Mock<T, Y>;
  124. spyOn<T extends {}, M extends NonFunctionPropertyNames<T>>(object: T, methodName: M, accessType: 'get'): JestMock.SpyInstance<T[M], []>;
  125. spyOn<T extends {}, M extends NonFunctionPropertyNames<T>>(object: T, methodName: M, accessType: 'set'): JestMock.SpyInstance<void, [T[M]]>;
  126. spyOn<T extends {}, M extends FunctionPropertyNames<T>>(object: T, methodName: M): T[M] extends (...args: Array<any>) => any ? JestMock.SpyInstance<ReturnType<T[M]>, Parameters<T[M]>> : never;
  127. private _spyOnProperty;
  128. clearAllMocks(): void;
  129. resetAllMocks(): void;
  130. restoreAllMocks(): void;
  131. private _typeOf;
  132. }
  133. declare const JestMock: ModuleMockerClass;
  134. export = JestMock;