index.d.ts 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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 type {Config} from '@jest/types';
  8. import type {EncodedSourceMap} from '@jridgewell/trace-mapping';
  9. import type {TransformTypes} from '@jest/types';
  10. export declare interface AsyncTransformer<TransformerConfig = unknown> {
  11. /**
  12. * Indicates if the transformer is capable of instrumenting the code for code coverage.
  13. *
  14. * If V8 coverage is _not_ active, and this is `true`, Jest will assume the code is instrumented.
  15. * If V8 coverage is _not_ active, and this is `false`. Jest will instrument the code returned by this transformer using Babel.
  16. */
  17. canInstrument?: boolean;
  18. getCacheKey?: (
  19. sourceText: string,
  20. sourcePath: string,
  21. options: TransformOptions<TransformerConfig>,
  22. ) => string;
  23. getCacheKeyAsync?: (
  24. sourceText: string,
  25. sourcePath: string,
  26. options: TransformOptions<TransformerConfig>,
  27. ) => Promise<string>;
  28. process?: (
  29. sourceText: string,
  30. sourcePath: string,
  31. options: TransformOptions<TransformerConfig>,
  32. ) => TransformedSource;
  33. processAsync: (
  34. sourceText: string,
  35. sourcePath: string,
  36. options: TransformOptions<TransformerConfig>,
  37. ) => Promise<TransformedSource>;
  38. }
  39. export declare interface CallerTransformOptions {
  40. supportsDynamicImport: boolean;
  41. supportsExportNamespaceFrom: boolean;
  42. supportsStaticESM: boolean;
  43. supportsTopLevelAwait: boolean;
  44. }
  45. export declare function createScriptTransformer(
  46. config: Config.ProjectConfig,
  47. cacheFS?: StringMap,
  48. ): Promise<ScriptTransformer>;
  49. export declare function createTranspilingRequire(
  50. config: Config.ProjectConfig,
  51. ): Promise<
  52. <TModuleType = unknown>(
  53. resolverPath: string,
  54. applyInteropRequireDefault?: boolean,
  55. ) => Promise<TModuleType>
  56. >;
  57. declare interface ErrorWithCodeFrame extends Error {
  58. codeFrame?: string;
  59. }
  60. declare interface FixedRawSourceMap extends Omit<EncodedSourceMap, 'version'> {
  61. version: number;
  62. }
  63. export declare function handlePotentialSyntaxError(
  64. e: ErrorWithCodeFrame,
  65. ): ErrorWithCodeFrame;
  66. declare interface ReducedTransformOptions extends CallerTransformOptions {
  67. instrument: boolean;
  68. }
  69. declare interface RequireAndTranspileModuleOptions
  70. extends ReducedTransformOptions {
  71. applyInteropRequireDefault: boolean;
  72. }
  73. export declare type ScriptTransformer = ScriptTransformer_2;
  74. declare class ScriptTransformer_2 {
  75. private readonly _config;
  76. private readonly _cacheFS;
  77. private readonly _cache;
  78. private readonly _transformCache;
  79. private _transformsAreLoaded;
  80. constructor(_config: Config.ProjectConfig, _cacheFS: StringMap);
  81. private _buildCacheKeyFromFileInfo;
  82. private _buildTransformCacheKey;
  83. private _getCacheKey;
  84. private _getCacheKeyAsync;
  85. private _createCachedFilename;
  86. private _getFileCachePath;
  87. private _getFileCachePathAsync;
  88. private _getTransformPatternAndPath;
  89. private _getTransformPath;
  90. loadTransformers(): Promise<void>;
  91. private _getTransformer;
  92. private _instrumentFile;
  93. private _buildTransformResult;
  94. transformSource(
  95. filepath: string,
  96. content: string,
  97. options: ReducedTransformOptions,
  98. ): TransformResult;
  99. transformSourceAsync(
  100. filepath: string,
  101. content: string,
  102. options: ReducedTransformOptions,
  103. ): Promise<TransformResult>;
  104. private _transformAndBuildScriptAsync;
  105. private _transformAndBuildScript;
  106. transformAsync(
  107. filename: string,
  108. options: TransformationOptions,
  109. fileSource?: string,
  110. ): Promise<TransformResult>;
  111. transform(
  112. filename: string,
  113. options: TransformationOptions,
  114. fileSource?: string,
  115. ): TransformResult;
  116. transformJson(
  117. filename: string,
  118. options: TransformationOptions,
  119. fileSource: string,
  120. ): string;
  121. requireAndTranspileModule<ModuleType = unknown>(
  122. moduleName: string,
  123. callback?: (module: ModuleType) => void | Promise<void>,
  124. options?: RequireAndTranspileModuleOptions,
  125. ): Promise<ModuleType>;
  126. shouldTransform(filename: string): boolean;
  127. }
  128. export declare function shouldInstrument(
  129. filename: string,
  130. options: ShouldInstrumentOptions,
  131. config: Config.ProjectConfig,
  132. loadedFilenames?: Array<string>,
  133. ): boolean;
  134. export declare interface ShouldInstrumentOptions
  135. extends Pick<
  136. Config.GlobalConfig,
  137. 'collectCoverage' | 'collectCoverageFrom' | 'coverageProvider'
  138. > {
  139. changedFiles?: Set<string>;
  140. sourcesRelatedToTestsInChangedFiles?: Set<string>;
  141. }
  142. declare type StringMap = Map<string, string>;
  143. export declare interface SyncTransformer<TransformerConfig = unknown> {
  144. /**
  145. * Indicates if the transformer is capable of instrumenting the code for code coverage.
  146. *
  147. * If V8 coverage is _not_ active, and this is `true`, Jest will assume the code is instrumented.
  148. * If V8 coverage is _not_ active, and this is `false`. Jest will instrument the code returned by this transformer using Babel.
  149. */
  150. canInstrument?: boolean;
  151. getCacheKey?: (
  152. sourceText: string,
  153. sourcePath: string,
  154. options: TransformOptions<TransformerConfig>,
  155. ) => string;
  156. getCacheKeyAsync?: (
  157. sourceText: string,
  158. sourcePath: string,
  159. options: TransformOptions<TransformerConfig>,
  160. ) => Promise<string>;
  161. process: (
  162. sourceText: string,
  163. sourcePath: string,
  164. options: TransformOptions<TransformerConfig>,
  165. ) => TransformedSource;
  166. processAsync?: (
  167. sourceText: string,
  168. sourcePath: string,
  169. options: TransformOptions<TransformerConfig>,
  170. ) => Promise<TransformedSource>;
  171. }
  172. export declare interface TransformationOptions
  173. extends ShouldInstrumentOptions,
  174. CallerTransformOptions {
  175. isInternalModule?: boolean;
  176. }
  177. export declare type TransformedSource = {
  178. code: string;
  179. map?: FixedRawSourceMap | string | null;
  180. };
  181. /**
  182. * We have both sync (`process`) and async (`processAsync`) code transformation, which both can be provided.
  183. * `require` will always use `process`, and `import` will use `processAsync` if it exists, otherwise fall back to `process`.
  184. * Meaning, if you use `import` exclusively you do not need `process`, but in most cases supplying both makes sense:
  185. * Jest transpiles on demand rather than ahead of time, so the sync one needs to exist.
  186. *
  187. * For more info on the sync vs async model, see https://jestjs.io/docs/code-transformation#writing-custom-transformers
  188. */
  189. declare type Transformer_2<TransformerConfig = unknown> =
  190. | SyncTransformer<TransformerConfig>
  191. | AsyncTransformer<TransformerConfig>;
  192. export {Transformer_2 as Transformer};
  193. export declare type TransformerCreator<
  194. X extends Transformer_2<TransformerConfig>,
  195. TransformerConfig = unknown,
  196. > = (transformerConfig?: TransformerConfig) => X | Promise<X>;
  197. /**
  198. * Instead of having your custom transformer implement the Transformer interface
  199. * directly, you can choose to export a factory function to dynamically create
  200. * transformers. This is to allow having a transformer config in your jest config.
  201. */
  202. export declare type TransformerFactory<X extends Transformer_2> = {
  203. createTransformer: TransformerCreator<X>;
  204. };
  205. export declare interface TransformOptions<TransformerConfig = unknown>
  206. extends ReducedTransformOptions {
  207. /** Cached file system which is used by `jest-runtime` to improve performance. */
  208. cacheFS: StringMap;
  209. /** Jest configuration of currently running project. */
  210. config: Config.ProjectConfig;
  211. /** Stringified version of the `config` - useful in cache busting. */
  212. configString: string;
  213. /** Transformer configuration passed through `transform` option by the user. */
  214. transformerConfig: TransformerConfig;
  215. }
  216. export declare type TransformResult = TransformTypes.TransformResult;
  217. export {};