index.d.ts 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. import {CallerTransformOptions} from '@jest/transform';
  8. import type {Config} from '@jest/types';
  9. import type {expect} from '@jest/globals';
  10. import type {Global} from '@jest/types';
  11. import {IHasteMap} from 'jest-haste-map';
  12. import {IModuleMap} from 'jest-haste-map';
  13. import type {JestEnvironment} from '@jest/environment';
  14. import Resolver from 'jest-resolve';
  15. import {ScriptTransformer} from '@jest/transform';
  16. import {shouldInstrument} from '@jest/transform';
  17. import {ShouldInstrumentOptions} from '@jest/transform';
  18. import type {SourceMapRegistry} from '@jest/source-map';
  19. import type {TestContext} from '@jest/test-result';
  20. import type {V8CoverageResult} from '@jest/test-result';
  21. declare type HasteMapOptions = {
  22. console?: Console;
  23. maxWorkers: number;
  24. resetCache: boolean;
  25. watch?: boolean;
  26. watchman: boolean;
  27. workerThreads?: boolean;
  28. };
  29. declare interface InternalModuleOptions
  30. extends Required<CallerTransformOptions> {
  31. isInternalModule: boolean;
  32. }
  33. declare interface JestGlobals extends Global.TestFrameworkGlobals {
  34. expect: typeof expect;
  35. }
  36. declare class Runtime {
  37. private readonly _cacheFS;
  38. private readonly _cacheFSBuffer;
  39. private readonly _config;
  40. private readonly _globalConfig?;
  41. private readonly _coverageOptions;
  42. private _currentlyExecutingModulePath;
  43. private readonly _environment;
  44. private readonly _explicitShouldMock;
  45. private readonly _explicitShouldMockModule;
  46. private _fakeTimersImplementation;
  47. private readonly _internalModuleRegistry;
  48. private _isCurrentlyExecutingManualMock;
  49. private _mainModule;
  50. private readonly _mockFactories;
  51. private readonly _mockMetaDataCache;
  52. private _mockRegistry;
  53. private _isolatedMockRegistry;
  54. private readonly _moduleMockRegistry;
  55. private readonly _moduleMockFactories;
  56. private readonly _moduleMocker;
  57. private _isolatedModuleRegistry;
  58. private _moduleRegistry;
  59. private readonly _esmoduleRegistry;
  60. private readonly _cjsNamedExports;
  61. private readonly _esmModuleLinkingMap;
  62. private readonly _testPath;
  63. private readonly _resolver;
  64. private _shouldAutoMock;
  65. private readonly _shouldMockModuleCache;
  66. private readonly _shouldUnmockTransitiveDependenciesCache;
  67. private readonly _sourceMapRegistry;
  68. private readonly _scriptTransformer;
  69. private readonly _fileTransforms;
  70. private readonly _fileTransformsMutex;
  71. private _v8CoverageInstrumenter;
  72. private _v8CoverageResult;
  73. private _v8CoverageSources;
  74. private readonly _transitiveShouldMock;
  75. private _unmockList;
  76. private readonly _virtualMocks;
  77. private readonly _virtualModuleMocks;
  78. private _moduleImplementation?;
  79. private readonly jestObjectCaches;
  80. private jestGlobals?;
  81. private readonly esmConditions;
  82. private readonly cjsConditions;
  83. private isTornDown;
  84. constructor(
  85. config: Config.ProjectConfig,
  86. environment: JestEnvironment,
  87. resolver: Resolver,
  88. transformer: ScriptTransformer,
  89. cacheFS: Map<string, string>,
  90. coverageOptions: ShouldInstrumentOptions,
  91. testPath: string,
  92. globalConfig?: Config.GlobalConfig,
  93. );
  94. static shouldInstrument: typeof shouldInstrument;
  95. static createContext(
  96. config: Config.ProjectConfig,
  97. options: {
  98. console?: Console;
  99. maxWorkers: number;
  100. watch?: boolean;
  101. watchman: boolean;
  102. },
  103. ): Promise<TestContext>;
  104. static createHasteMap(
  105. config: Config.ProjectConfig,
  106. options?: HasteMapOptions,
  107. ): Promise<IHasteMap>;
  108. static createResolver(
  109. config: Config.ProjectConfig,
  110. moduleMap: IModuleMap,
  111. ): Resolver;
  112. static runCLI(): Promise<never>;
  113. static getCLIOptions(): never;
  114. unstable_shouldLoadAsEsm(modulePath: string): boolean;
  115. private loadEsmModule;
  116. private resolveModule;
  117. private linkAndEvaluateModule;
  118. unstable_importModule(
  119. from: string,
  120. moduleName?: string,
  121. ): Promise<unknown | void>;
  122. private loadCjsAsEsm;
  123. private importMock;
  124. private getExportsOfCjs;
  125. requireModule<T = unknown>(
  126. from: string,
  127. moduleName?: string,
  128. options?: InternalModuleOptions,
  129. isRequireActual?: boolean,
  130. ): T;
  131. requireInternalModule<T = unknown>(from: string, to?: string): T;
  132. requireActual<T = unknown>(from: string, moduleName: string): T;
  133. requireMock<T = unknown>(from: string, moduleName: string): T;
  134. private _loadModule;
  135. private _getFullTransformationOptions;
  136. requireModuleOrMock<T = unknown>(from: string, moduleName: string): T;
  137. isolateModules(fn: () => void): void;
  138. isolateModulesAsync(fn: () => Promise<void>): Promise<void>;
  139. resetModules(): void;
  140. collectV8Coverage(): Promise<void>;
  141. stopCollectingV8Coverage(): Promise<void>;
  142. getAllCoverageInfoCopy(): JestEnvironment['global']['__coverage__'];
  143. getAllV8CoverageInfoCopy(): V8CoverageResult;
  144. getSourceMaps(): SourceMapRegistry;
  145. setMock(
  146. from: string,
  147. moduleName: string,
  148. mockFactory: () => unknown,
  149. options?: {
  150. virtual?: boolean;
  151. },
  152. ): void;
  153. private setModuleMock;
  154. restoreAllMocks(): void;
  155. resetAllMocks(): void;
  156. clearAllMocks(): void;
  157. teardown(): void;
  158. private _resolveCjsModule;
  159. private _resolveModule;
  160. private _requireResolve;
  161. private _requireResolvePaths;
  162. private _execModule;
  163. private transformFile;
  164. private transformFileAsync;
  165. private createScriptFromCode;
  166. private _requireCoreModule;
  167. private _importCoreModule;
  168. private _importWasmModule;
  169. private _getMockedNativeModule;
  170. private _generateMock;
  171. private _shouldMockCjs;
  172. private _shouldMockModule;
  173. private _createRequireImplementation;
  174. private _createJestObjectFor;
  175. private _logFormattedReferenceError;
  176. private wrapCodeInModuleWrapper;
  177. private constructModuleWrapperStart;
  178. private constructInjectedModuleParameters;
  179. private handleExecutionError;
  180. private getGlobalsForCjs;
  181. private getGlobalsForEsm;
  182. private getGlobalsFromEnvironment;
  183. private readFileBuffer;
  184. private readFile;
  185. setGlobalsForRuntime(globals: JestGlobals): void;
  186. }
  187. export default Runtime;
  188. export {};