diff --git a/docs/CodeTransformation.md b/docs/CodeTransformation.md index b94873c7eb0f..b7ceb9aaeb7a 100644 --- a/docs/CodeTransformation.md +++ b/docs/CodeTransformation.md @@ -33,19 +33,20 @@ Remember to include the default `babel-jest` transformer explicitly, if you wish You can write your own transformer. The API of a transformer is as follows: ```ts -interface TransformOptions { +interface TransformOptions { supportsDynamicImport: boolean; supportsExportNamespaceFrom: boolean; supportsStaticESM: boolean; supportsTopLevelAwait: boolean; instrument: boolean; - /** a cached file system which is used in jest-runtime - useful to improve performance */ + /** Cached file system which is used by `jest-runtime` to improve performance. */ cacheFS: Map; - config: Config.ProjectConfig; - /** A stringified version of the configuration - useful in cache busting */ + /** Jest configuration of currently running project. */ + config: ProjectConfig; + /** Stringified version of the `config` - useful in cache busting. */ configString: string; - /** the options passed through Jest's config by the user */ - transformerConfig: OptionType; + /** Transformer configuration passed through `transform` option by the user. */ + transformerConfig: TransformerConfig; } type TransformedSource = { @@ -53,70 +54,70 @@ type TransformedSource = { map?: RawSourceMap | string | null; }; -interface SyncTransformer { +interface SyncTransformer { canInstrument?: boolean; getCacheKey?: ( sourceText: string, sourcePath: string, - options: TransformOptions, + options: TransformOptions, ) => string; getCacheKeyAsync?: ( sourceText: string, sourcePath: string, - options: TransformOptions, + options: TransformOptions, ) => Promise; process: ( sourceText: string, sourcePath: string, - options: TransformOptions, + options: TransformOptions, ) => TransformedSource; processAsync?: ( sourceText: string, sourcePath: string, - options: TransformOptions, + options: TransformOptions, ) => Promise; } -interface AsyncTransformer { +interface AsyncTransformer { canInstrument?: boolean; getCacheKey?: ( sourceText: string, sourcePath: string, - options: TransformOptions, + options: TransformOptions, ) => string; getCacheKeyAsync?: ( sourceText: string, sourcePath: string, - options: TransformOptions, + options: TransformOptions, ) => Promise; process?: ( sourceText: string, sourcePath: string, - options: TransformOptions, + options: TransformOptions, ) => TransformedSource; processAsync: ( sourceText: string, sourcePath: string, - options: TransformOptions, + options: TransformOptions, ) => Promise; } -type Transformer = - | SyncTransformer - | AsyncTransformer; +type Transformer = + | SyncTransformer + | AsyncTransformer; type TransformerCreator< - X extends Transformer, - OptionType = unknown, -> = (options?: OptionType) => X; + X extends Transformer, + TransformerConfig = unknown, +> = (transformerConfig?: TransformerConfig) => X; type TransformerFactory = { createTransformer: TransformerCreator; diff --git a/packages/jest-transform/src/types.ts b/packages/jest-transform/src/types.ts index 27308b5403f8..a86d56e973d1 100644 --- a/packages/jest-transform/src/types.ts +++ b/packages/jest-transform/src/types.ts @@ -57,18 +57,19 @@ export interface RequireAndTranspileModuleOptions export type StringMap = Map; -export interface TransformOptions +export interface TransformOptions extends ReducedTransformOptions { - /** a cached file system which is used in jest-runtime - useful to improve performance */ + /** Cached file system which is used by `jest-runtime` to improve performance. */ cacheFS: StringMap; + /** Jest configuration of currently running project. */ config: Config.ProjectConfig; - /** A stringified version of the configuration - useful in cache busting */ + /** Stringified version of the `config` - useful in cache busting. */ configString: string; - /** the options passed through Jest's config by the user */ - transformerConfig: OptionType; + /** Transformer configuration passed through `transform` option by the user. */ + transformerConfig: TransformerConfig; } -export interface SyncTransformer { +export interface SyncTransformer { /** * Indicates if the transformer is capable of instrumenting the code for code coverage. * @@ -80,29 +81,29 @@ export interface SyncTransformer { getCacheKey?: ( sourceText: string, sourcePath: string, - options: TransformOptions, + options: TransformOptions, ) => string; getCacheKeyAsync?: ( sourceText: string, sourcePath: string, - options: TransformOptions, + options: TransformOptions, ) => Promise; process: ( sourceText: string, sourcePath: string, - options: TransformOptions, + options: TransformOptions, ) => TransformedSource; processAsync?: ( sourceText: string, sourcePath: string, - options: TransformOptions, + options: TransformOptions, ) => Promise; } -export interface AsyncTransformer { +export interface AsyncTransformer { /** * Indicates if the transformer is capable of instrumenting the code for code coverage. * @@ -114,25 +115,25 @@ export interface AsyncTransformer { getCacheKey?: ( sourceText: string, sourcePath: string, - options: TransformOptions, + options: TransformOptions, ) => string; getCacheKeyAsync?: ( sourceText: string, sourcePath: string, - options: TransformOptions, + options: TransformOptions, ) => Promise; process?: ( sourceText: string, sourcePath: string, - options: TransformOptions, + options: TransformOptions, ) => TransformedSource; processAsync: ( sourceText: string, sourcePath: string, - options: TransformOptions, + options: TransformOptions, ) => Promise; } @@ -144,14 +145,14 @@ export interface AsyncTransformer { * * For more info on the sync vs async model, see https://jestjs.io/docs/code-transformation#writing-custom-transformers */ -export type Transformer = - | SyncTransformer - | AsyncTransformer; +export type Transformer = + | SyncTransformer + | AsyncTransformer; export type TransformerCreator< - X extends Transformer, - OptionType = unknown, -> = (options?: OptionType) => X; + X extends Transformer, + TransformerConfig = unknown, +> = (transformerConfig?: TransformerConfig) => X; /** * Instead of having your custom transformer implement the Transformer interface