UNPKG

7.62 kBTypeScriptView Raw
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 */
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 _getCacheKey;
94 private _getCacheKeyAsync;
95 private _createCachedFilename;
96 private _getFileCachePath;
97 private _getFileCachePathAsync;
98 private _getTransformPath;
99 loadTransformers(): Promise<void>;
100 private _getTransformer;
101 private _instrumentFile;
102 private _buildTransformResult;
103 transformSource(
104 filepath: string,
105 content: string,
106 options: ReducedTransformOptions,
107 ): TransformResult;
108 transformSourceAsync(
109 filepath: string,
110 content: string,
111 options: ReducedTransformOptions,
112 ): Promise<TransformResult>;
113 private _transformAndBuildScriptAsync;
114 private _transformAndBuildScript;
115 transformAsync(
116 filename: string,
117 options: TransformationOptions,
118 fileSource?: string,
119 ): Promise<TransformResult>;
120 transform(
121 filename: string,
122 options: TransformationOptions,
123 fileSource?: string,
124 ): TransformResult;
125 transformJson(
126 filename: string,
127 options: TransformationOptions,
128 fileSource: string,
129 ): string;
130 requireAndTranspileModule<ModuleType = unknown>(
131 moduleName: string,
132 callback?: (module: ModuleType) => void | Promise<void>,
133 options?: RequireAndTranspileModuleOptions,
134 ): Promise<ModuleType>;
135 shouldTransform(filename: string): boolean;
136}
137
138export declare function shouldInstrument(
139 filename: string,
140 options: ShouldInstrumentOptions,
141 config: Config.ProjectConfig,
142): boolean;
143
144export declare interface ShouldInstrumentOptions
145 extends Pick<
146 Config.GlobalConfig,
147 'collectCoverage' | 'collectCoverageFrom' | 'coverageProvider'
148 > {
149 changedFiles?: Set<string>;
150 sourcesRelatedToTestsInChangedFiles?: Set<string>;
151}
152
153declare type StringMap = Map<string, string>;
154
155export declare interface SyncTransformer<TransformerConfig = unknown> {
156 /**
157 * Indicates if the transformer is capable of instrumenting the code for code coverage.
158 *
159 * If V8 coverage is _not_ active, and this is `true`, Jest will assume the code is instrumented.
160 * If V8 coverage is _not_ active, and this is `false`. Jest will instrument the code returned by this transformer using Babel.
161 */
162 canInstrument?: boolean;
163 getCacheKey?: (
164 sourceText: string,
165 sourcePath: string,
166 options: TransformOptions<TransformerConfig>,
167 ) => string;
168 getCacheKeyAsync?: (
169 sourceText: string,
170 sourcePath: string,
171 options: TransformOptions<TransformerConfig>,
172 ) => Promise<string>;
173 process: (
174 sourceText: string,
175 sourcePath: string,
176 options: TransformOptions<TransformerConfig>,
177 ) => TransformedSource;
178 processAsync?: (
179 sourceText: string,
180 sourcePath: string,
181 options: TransformOptions<TransformerConfig>,
182 ) => Promise<TransformedSource>;
183}
184
185export declare interface TransformationOptions
186 extends ShouldInstrumentOptions,
187 CallerTransformOptions {
188 isInternalModule?: boolean;
189}
190
191export declare type TransformedSource = {
192 code: string;
193 map?: FixedRawSourceMap | string | null;
194};
195
196/**
197 * We have both sync (`process`) and async (`processAsync`) code transformation, which both can be provided.
198 * `require` will always use `process`, and `import` will use `processAsync` if it exists, otherwise fall back to `process`.
199 * Meaning, if you use `import` exclusively you do not need `process`, but in most cases supplying both makes sense:
200 * Jest transpiles on demand rather than ahead of time, so the sync one needs to exist.
201 *
202 * For more info on the sync vs async model, see https://jestjs.io/docs/code-transformation#writing-custom-transformers
203 */
204declare type Transformer_2<TransformerConfig = unknown> =
205 | SyncTransformer<TransformerConfig>
206 | AsyncTransformer<TransformerConfig>;
207export {Transformer_2 as Transformer};
208
209export declare type TransformerCreator<
210 X extends Transformer_2<TransformerConfig>,
211 TransformerConfig = unknown,
212> = (transformerConfig?: TransformerConfig) => X;
213
214/**
215 * Instead of having your custom transformer implement the Transformer interface
216 * directly, you can choose to export a factory function to dynamically create
217 * transformers. This is to allow having a transformer config in your jest config.
218 */
219export declare type TransformerFactory<X extends Transformer_2> = {
220 createTransformer: TransformerCreator<X>;
221};
222
223export declare interface TransformOptions<TransformerConfig = unknown>
224 extends ReducedTransformOptions {
225 /** Cached file system which is used by `jest-runtime` to improve performance. */
226 cacheFS: StringMap;
227 /** Jest configuration of currently running project. */
228 config: Config.ProjectConfig;
229 /** Stringified version of the `config` - useful in cache busting. */
230 configString: string;
231 /** Transformer configuration passed through `transform` option by the user. */
232 transformerConfig: TransformerConfig;
233}
234
235export declare type TransformResult = TransformTypes.TransformResult;
236
237export {};