@@ -10,6 +10,7 @@ import { ConfigSet } from './config'
10
10
import { DECLARATION_TYPE_EXT , JS_JSX_REGEX , TS_TSX_REGEX } from './constants'
11
11
import type { CompilerInstance , DepGraphInfo , ProjectConfigTsJest , TransformOptionsTsJest } from './types'
12
12
import { parse , stringify , JsonableValue , rootLogger } from './utils'
13
+ import { importer } from './utils/importer'
13
14
import { Errors , interpolate } from './utils/messages'
14
15
import { sha1 } from './utils/sha1'
15
16
import { VersionCheckers } from './utils/version-checkers'
@@ -24,6 +25,11 @@ interface CachedConfigSet {
24
25
watchMode : boolean
25
26
}
26
27
28
+ interface TsJestHooksMap {
29
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
30
+ afterProcess ?( args : any [ ] , result : string | TransformedSource ) : string | TransformedSource | void
31
+ }
32
+
27
33
/**
28
34
* @internal
29
35
*/
@@ -144,6 +150,13 @@ export class TsJestTransformer implements SyncTransformer {
144
150
const isDefinitionFile = filePath . endsWith ( DECLARATION_TYPE_EXT )
145
151
const isJsFile = JS_JSX_REGEX . test ( filePath )
146
152
const isTsFile = ! isDefinitionFile && TS_TSX_REGEX . test ( filePath )
153
+ let hooksFile = process . env . TS_JEST_HOOKS
154
+ let hooks : TsJestHooksMap | undefined
155
+ /* istanbul ignore next (cover by e2e) */
156
+ if ( hooksFile ) {
157
+ hooksFile = path . resolve ( configs . cwd , hooksFile )
158
+ hooks = importer . tryTheseOr ( hooksFile , { } )
159
+ }
147
160
if ( shouldStringifyContent ) {
148
161
// handles here what we should simply stringify
149
162
result = `module.exports=${ stringify ( fileContent ) } `
@@ -179,6 +192,15 @@ export class TsJestTransformer implements SyncTransformer {
179
192
// do not instrument here, jest will do it anyway afterwards
180
193
result = babelJest . process ( result , filePath , { ...transformOptions , instrument : false } )
181
194
}
195
+ // This is not supposed to be a public API but we keep it as some people use it
196
+ if ( hooks ?. afterProcess ) {
197
+ this . _logger . debug ( { fileName : filePath , hookName : 'afterProcess' } , 'calling afterProcess hook' )
198
+
199
+ const newResult = hooks . afterProcess ( [ fileContent , filePath , transformOptions . config , transformOptions ] , result )
200
+ if ( newResult ) {
201
+ return newResult
202
+ }
203
+ }
182
204
183
205
return result
184
206
}
0 commit comments