UNPKG

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