Skip to content

Commit 2b6b86e

Browse files
authoredDec 7, 2021
fix: bring back afterProcess hook (#3132)
1 parent 20258de commit 2b6b86e

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed
 

‎src/ts-jest-transformer.spec.ts

+13
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { SOURCE_MAPPING_PREFIX, TsJestCompiler } from './compiler'
1010
import { CACHE_KEY_EL_SEPARATOR, TsJestTransformer } from './ts-jest-transformer'
1111
import type { DepGraphInfo } from './types'
1212
import { stringify } from './utils'
13+
import { importer } from './utils/importer'
1314
import { sha1 } from './utils/sha1'
1415

1516
const logTarget = logTargetMock()
@@ -443,6 +444,18 @@ describe('TsJestTransformer', () => {
443444
expect(logTarget.filteredLines(LogLevels.warn)[0]).toMatchSnapshot()
444445
}
445446
})
447+
448+
test('should use afterHook file', () => {
449+
importer.tryTheseOr = jest.fn().mockReturnValueOnce({ afterProcess: () => 'foo' })
450+
process.env.TS_JEST_HOOKS = __filename
451+
452+
const fileContent = 'type Foo = number'
453+
const filePath = 'foo.d.ts'
454+
455+
expect(tr.process(fileContent, filePath, baseTransformOptions)).toEqual('foo')
456+
457+
delete process.env.TS_JEST_HOOKS
458+
})
446459
})
447460

448461
describe('processAsync', () => {

‎src/ts-jest-transformer.ts

+22
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { ConfigSet } from './config'
1010
import { DECLARATION_TYPE_EXT, JS_JSX_REGEX, TS_TSX_REGEX } from './constants'
1111
import type { CompilerInstance, DepGraphInfo, ProjectConfigTsJest, TransformOptionsTsJest } from './types'
1212
import { parse, stringify, JsonableValue, rootLogger } from './utils'
13+
import { importer } from './utils/importer'
1314
import { Errors, interpolate } from './utils/messages'
1415
import { sha1 } from './utils/sha1'
1516
import { VersionCheckers } from './utils/version-checkers'
@@ -24,6 +25,11 @@ interface CachedConfigSet {
2425
watchMode: boolean
2526
}
2627

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+
2733
/**
2834
* @internal
2935
*/
@@ -144,6 +150,13 @@ export class TsJestTransformer implements SyncTransformer {
144150
const isDefinitionFile = filePath.endsWith(DECLARATION_TYPE_EXT)
145151
const isJsFile = JS_JSX_REGEX.test(filePath)
146152
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+
}
147160
if (shouldStringifyContent) {
148161
// handles here what we should simply stringify
149162
result = `module.exports=${stringify(fileContent)}`
@@ -179,6 +192,15 @@ export class TsJestTransformer implements SyncTransformer {
179192
// do not instrument here, jest will do it anyway afterwards
180193
result = babelJest.process(result, filePath, { ...transformOptions, instrument: false })
181194
}
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+
}
182204

183205
return result
184206
}

0 commit comments

Comments
 (0)
Please sign in to comment.