UNPKG

9.94 kBTypeScriptView Raw
1import type { TransformedSource, TransformOptions } from '@jest/transform';
2import type { Config } from '@jest/types';
3import type * as _babel from 'babel__core';
4import type * as _ts from 'typescript';
5import { ESM_JS_TRANSFORM_PATTERN, ESM_TS_JS_TRANSFORM_PATTERN, ESM_TS_TRANSFORM_PATTERN, JS_TRANSFORM_PATTERN, TS_JS_TRANSFORM_PATTERN, TS_TRANSFORM_PATTERN } from './constants';
6import type { ConfigSet } from './legacy/config/config-set';
7import type { RawCompilerOptions } from './raw-compiler-options';
8export type TTypeScript = typeof _ts;
9/**
10 * Don't mark as internal because it is used in TsJestGlobalOptions which is an exposed type
11 */
12export type BabelConfig = _babel.TransformOptions;
13export interface AstTransformer<T = Record<string, unknown>> {
14 path: string;
15 options?: T;
16}
17export interface ConfigCustomTransformer {
18 before?: Array<string | AstTransformer>;
19 after?: Array<string | AstTransformer>;
20 afterDeclarations?: Array<string | AstTransformer>;
21}
22/**
23 * @deprecated use `TsJestTransformerOptions` instead
24 */
25export interface TsJestGlobalOptions {
26 /**
27 * Compiler options. It can be:
28 * - `true` (or `undefined`, it's the default): use default tsconfig file
29 * - `false`: do NOT use default config file
30 * - `path/to/tsconfig.json`: path to a specific tsconfig file (<rootDir> can be used)
31 * - `{...}`: an object with inline compiler options
32 *
33 * @default undefined uses the default tsconfig file
34 */
35 tsconfig?: boolean | string | RawCompilerOptions;
36 /**
37 * Compiles files as isolated modules (disables some features and type-checking)
38 *
39 * @default undefined (disabled)
40 */
41 isolatedModules?: boolean;
42 /**
43 * Compiler to use
44 *
45 * @default 'typescript'
46 */
47 compiler?: 'typescript' | 'ttypescript' | string;
48 /**
49 * Custom transformers (mostly used by jest presets)
50 */
51 astTransformers?: ConfigCustomTransformer;
52 /**
53 * TS diagnostics - less to be reported if `isolatedModules` is `true`. It can be:
54 * - `true` (or `undefined`, it's the default): show all diagnostics
55 * - `false`: hide diagnostics of all files (kind of useless)
56 * - `{...}`: an inline object with fine grained settings
57 *
58 * @default undefined shows all diagnostics
59 */
60 diagnostics?: boolean | {
61 /**
62 * Enables colorful and pretty output of errors
63 *
64 * @default undefined (enabled)
65 */
66 pretty?: boolean;
67 /**
68 * List of TypeScript diagnostic error codes to ignore
69 * [here](https://github.com/Microsoft/TypeScript/blob/master/src/compiler/diagnosticMessages.json).
70 *
71 * @see https://github.com/Microsoft/TypeScript/blob/master/src/compiler/diagnosticMessages.json
72 * @default [6059,18002,18003]
73 */
74 ignoreCodes?: number | string | Array<number | string>;
75 /**
76 * If specified, diagnostics of source files which path **matches** will be ignored
77 */
78 exclude?: string[];
79 /**
80 * Logs TypeScript errors to stderr instead of throwing exceptions
81 *
82 * @default undefined (disabled)
83 */
84 warnOnly?: boolean;
85 };
86 /**
87 * Babel config. It can be:
88 * - `false` (or `undefined`, it's the default): do NOT use babel
89 * - `true`: use babel using default babelrc file
90 * - `path/to/.babelrc`: path to a babelrc file (<rootDir> can be used)
91 * - `{...}`: an object with inline babel options
92 *
93 * @default undefined does NOT use babel
94 */
95 babelConfig?: boolean | string | BabelConfig;
96 /**
97 * Kept for backward compatibility to handle __TRANSFORM_HTML__
98 * Any file which will match this regex will be transpiled as a module
99 * exporting the content of the file as a string
100 */
101 stringifyContentPathRegex?: string | RegExp;
102 /**
103 * Tell `ts-jest` to transform codes to ESM format. This only works in combination with `jest-runtime` ESM option
104 * `supportsStaticESM` true which is passed into Jest transformer
105 */
106 useESM?: boolean;
107}
108/**
109 * For transformers which extends `ts-jest`
110 * @deprecated use `JestConfigWithTsJest` instead
111 */
112export interface ProjectConfigTsJest extends Config.ProjectConfig {
113 globals: GlobalConfigTsJest;
114}
115/**
116 * @deprecated use `JestConfigWithTsJest` instead
117 */
118export interface TransformOptionsTsJest<TransformerConfig = unknown> extends TransformOptions<TransformerConfig> {
119 config: Config.ProjectConfig;
120}
121/**
122 * For typings in `jest.config.ts`
123 * @deprecated use `JestConfigWithTsJest` instead
124 */
125export interface GlobalConfigTsJest extends Config.ConfigGlobals {
126 'ts-jest'?: TsJestGlobalOptions;
127}
128/**
129 * @deprecated use `JestConfigWithTsJest` instead
130 */
131export interface InitialOptionsTsJest extends Config.InitialOptions {
132 globals?: GlobalConfigTsJest;
133}
134export type TsJestTransformerOptions = TsJestGlobalOptions;
135export type TsJestTransformOptions = TransformOptions<TsJestTransformerOptions>;
136export interface JestConfigWithTsJest extends Omit<Config.InitialOptions, 'transform'> {
137 transform?: {
138 [regex: string]: 'ts-jest' | 'ts-jest/legacy' | ['ts-jest', TsJestTransformerOptions] | ['ts-jest/legacy', TsJestTransformerOptions] | string | Config.TransformerConfig;
139 };
140}
141export type StringMap = Map<string, string>;
142export interface DepGraphInfo {
143 fileContent: string;
144 resolvedModuleNames: string[];
145}
146export interface TsJestCompileOptions {
147 depGraphs: Map<string, DepGraphInfo>;
148 watchMode: boolean;
149 supportsStaticESM: boolean;
150}
151export interface CompiledOutput extends TransformedSource {
152 diagnostics?: _ts.Diagnostic[];
153}
154export interface CompilerInstance {
155 getResolvedModules(fileContent: string, fileName: string, runtimeCacheFS: StringMap): string[];
156 getCompiledOutput(fileContent: string, fileName: string, options: TsJestCompileOptions): CompiledOutput;
157}
158export interface TsCompilerInstance extends CompilerInstance {
159 configSet: ConfigSet;
160 program: _ts.Program | undefined;
161}
162export interface AstTransformerDesc<T = Record<string, unknown>> {
163 name: string;
164 version: number;
165 factory(tsCompiler: TsCompilerInstance, opts?: T): _ts.TransformerFactory<_ts.SourceFile> | _ts.TransformerFactory<_ts.Bundle | _ts.SourceFile>;
166 options?: T;
167}
168export interface TsJestAstTransformer {
169 before: AstTransformerDesc[];
170 after: AstTransformerDesc[];
171 afterDeclarations: AstTransformerDesc[];
172}
173/**
174 * @deprecated use other preset types below instead
175 */
176export type TsJestPresets = Pick<JestConfigWithTsJest, 'extensionsToTreatAsEsm' | 'moduleFileExtensions' | 'transform' | 'testMatch'>;
177export type DefaultTransformOptions = Omit<TsJestTransformerOptions, 'useESM'>;
178export type DefaultPreset = {
179 transform: {
180 [TS_TRANSFORM_PATTERN]: ['ts-jest', DefaultTransformOptions];
181 };
182};
183export type DefaultLegacyPreset = {
184 transform: {
185 [TS_TRANSFORM_PATTERN]: ['ts-jest/legacy', DefaultTransformOptions];
186 };
187};
188export type DefaultEsmTransformOptions = Omit<TsJestTransformerOptions, 'useESM'>;
189export type DefaultEsmPreset = {
190 extensionsToTreatAsEsm: string[];
191 transform: {
192 [ESM_TS_TRANSFORM_PATTERN]: ['ts-jest', {
193 useESM: true;
194 } & DefaultEsmTransformOptions];
195 };
196};
197export type DefaultEsmLegacyPreset = {
198 extensionsToTreatAsEsm: string[];
199 transform: {
200 [ESM_TS_TRANSFORM_PATTERN]: ['ts-jest/legacy', {
201 useESM: true;
202 } & DefaultEsmTransformOptions];
203 };
204};
205export type JsWithTsTransformOptions = Omit<TsJestTransformerOptions, 'useESM'>;
206export type JsWithTsPreset = {
207 transform: {
208 [TS_JS_TRANSFORM_PATTERN]: ['ts-jest', JsWithTsTransformOptions];
209 };
210};
211export type JsWithTsLegacyPreset = {
212 transform: {
213 [TS_JS_TRANSFORM_PATTERN]: ['ts-jest/legacy', JsWithTsTransformOptions];
214 };
215};
216export type JsWithTsEsmTransformOptions = Omit<TsJestTransformerOptions, 'useESM'>;
217export type JsWithTsEsmPreset = {
218 extensionsToTreatAsEsm: string[];
219 transform: {
220 [ESM_TS_JS_TRANSFORM_PATTERN]: ['ts-jest', {
221 useESM: true;
222 } & JsWithTsEsmTransformOptions];
223 };
224};
225export type JsWithTsEsmLegacyPreset = {
226 extensionsToTreatAsEsm: string[];
227 transform: {
228 [ESM_TS_JS_TRANSFORM_PATTERN]: ['ts-jest/legacy', {
229 useESM: true;
230 } & JsWithTsEsmTransformOptions];
231 };
232};
233export type JsWithBabelTransformerOptions = Omit<TsJestTransformerOptions, 'useESM'>;
234export type JsWithBabelPreset = {
235 transform: {
236 [JS_TRANSFORM_PATTERN]: 'babel-jest';
237 [TS_TRANSFORM_PATTERN]: ['ts-jest', JsWithBabelTransformerOptions];
238 };
239};
240export type JsWithBabelLegacyPreset = {
241 transform: {
242 [JS_TRANSFORM_PATTERN]: 'babel-jest';
243 [TS_TRANSFORM_PATTERN]: ['ts-jest/legacy', JsWithBabelTransformerOptions];
244 };
245};
246export type JsWithBabelEsmTransformOptions = Omit<TsJestTransformerOptions, 'useESM'>;
247export type JsWithBabelEsmPreset = {
248 extensionsToTreatAsEsm: string[];
249 transform: {
250 [ESM_JS_TRANSFORM_PATTERN]: 'babel-jest';
251 [ESM_TS_TRANSFORM_PATTERN]: ['ts-jest', {
252 useESM: true;
253 } & JsWithBabelEsmTransformOptions];
254 };
255};
256export type JsWithBabelEsmLegacyPreset = {
257 extensionsToTreatAsEsm: string[];
258 transform: {
259 [ESM_JS_TRANSFORM_PATTERN]: 'babel-jest';
260 [ESM_TS_TRANSFORM_PATTERN]: ['ts-jest/legacy', {
261 useESM: true;
262 } & JsWithBabelEsmTransformOptions];
263 };
264};
265declare module '@jest/types' {
266 namespace Config {
267 interface ConfigGlobals {
268 /**
269 * strangely `@ts-expect-error` doesn't work in this case when running
270 * `npm run build` vs `npm run pretest`
271 */
272 'ts-jest'?: TsJestTransformerOptions;
273 }
274 }
275}