@@ -13,11 +13,13 @@ import { existsSync, readFileSync } from 'fs'
1313import json5 from 'json5'
1414import { dirname , isAbsolute , join , resolve } from 'path'
1515import semver from 'semver'
16- import { Diagnostic , FormatDiagnosticsHost , ParsedCommandLine } from 'typescript'
16+ import { CustomTransformers , Diagnostic , FormatDiagnosticsHost , ParsedCommandLine } from 'typescript'
1717
1818import { version as myVersion } from '..'
1919import { createCompiler } from '../compiler'
20+ import { internals as internalAstTransformers } from '../transformers'
2021import {
22+ AstTransformerDesc ,
2123 BabelConfig ,
2224 BabelJestTransformer ,
2325 TTypeScript ,
@@ -160,6 +162,9 @@ export class ConfigSet {
160162 }
161163 }
162164
165+ // transformers
166+ const transformers = ( options . astTransformers || [ ] ) . map ( mod => this . resolvePath ( mod , { nodeResolve : true } ) )
167+
163168 // babel jest
164169 const { babelConfig : babelConfigOpt } = options
165170 let babelConfig : TsJestConfig [ 'babelConfig' ]
@@ -210,6 +215,7 @@ export class ConfigSet {
210215 diagnostics,
211216 isolatedModules : ! ! options . isolatedModules ,
212217 compiler : options . compiler || 'typescript' ,
218+ transformers,
213219 stringifyContentPathRegex,
214220 }
215221 this . logger . debug ( { tsJestConfig : res } , 'normalized ts-jest config' )
@@ -316,6 +322,18 @@ export class ConfigSet {
316322 return createCompiler ( this )
317323 }
318324
325+ @Memoize ( )
326+ get astTransformers ( ) : AstTransformerDesc [ ] {
327+ return [ ...internalAstTransformers , ...this . tsJest . transformers . map ( m => require ( m ) ) ]
328+ }
329+
330+ @Memoize ( )
331+ get tsCustomTransformers ( ) : CustomTransformers {
332+ return {
333+ before : this . astTransformers . map ( t => t . factory ( this ) ) ,
334+ }
335+ }
336+
319337 @Memoize ( )
320338 get hooks ( ) : TsJestHooksMap {
321339 let hooksFile = process . env . TS_JEST_HOOKS
@@ -487,14 +505,32 @@ export class ConfigSet {
487505 return { input, resolved : result }
488506 }
489507
490- resolvePath ( inputPath : string , noFailIfMissing = false ) : string {
508+ resolvePath (
509+ inputPath : string ,
510+ { throwIfMissing = true , nodeResolve = false } : { throwIfMissing ?: boolean ; nodeResolve ?: boolean } = { } ,
511+ ) : string {
491512 let path : string = inputPath
513+ let nodeResolved = false
492514 if ( path . startsWith ( '<rootDir>' ) ) {
493515 path = resolve ( this . rootDir , path . substr ( 9 ) )
494516 } else if ( ! isAbsolute ( path ) ) {
495- path = resolve ( this . cwd , path )
517+ if ( ! path . startsWith ( '.' ) && nodeResolve ) {
518+ try {
519+ path = require . resolve ( path )
520+ nodeResolved = true
521+ } catch ( _ ) { }
522+ }
523+ if ( ! nodeResolved ) {
524+ path = resolve ( this . cwd , path )
525+ }
526+ }
527+ if ( ! nodeResolved && nodeResolve ) {
528+ try {
529+ path = require . resolve ( path )
530+ nodeResolved = true
531+ } catch ( _ ) { }
496532 }
497- if ( ! noFailIfMissing && ! existsSync ( path ) ) {
533+ if ( throwIfMissing && ! existsSync ( path ) ) {
498534 throw new Error ( interpolate ( Errors . FileNotFound , { inputPath, resolvedPath : path } ) )
499535 }
500536 this . logger . debug ( { fromPath : inputPath , toPath : path } , 'resolved path from' , inputPath , 'to' , path )
@@ -514,6 +550,7 @@ export class ConfigSet {
514550
515551 return new JsonableValue ( {
516552 versions : this . versions ,
553+ transformers : this . astTransformers . map ( t => `${ t . name } @${ t . version } ` ) ,
517554 jest,
518555 tsJest : this . tsJest ,
519556 babel : this . babel ,
0 commit comments