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