Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: bring back afterProcess hook #3132

Merged
merged 1 commit into from Dec 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/ts-jest-transformer.spec.ts
Expand Up @@ -10,6 +10,7 @@ import { SOURCE_MAPPING_PREFIX, TsJestCompiler } from './compiler'
import { CACHE_KEY_EL_SEPARATOR, TsJestTransformer } from './ts-jest-transformer'
import type { DepGraphInfo } from './types'
import { stringify } from './utils'
import { importer } from './utils/importer'
import { sha1 } from './utils/sha1'

const logTarget = logTargetMock()
Expand Down Expand Up @@ -443,6 +444,18 @@ describe('TsJestTransformer', () => {
expect(logTarget.filteredLines(LogLevels.warn)[0]).toMatchSnapshot()
}
})

test('should use afterHook file', () => {
importer.tryTheseOr = jest.fn().mockReturnValueOnce({ afterProcess: () => 'foo' })
process.env.TS_JEST_HOOKS = __filename

const fileContent = 'type Foo = number'
const filePath = 'foo.d.ts'

expect(tr.process(fileContent, filePath, baseTransformOptions)).toEqual('foo')

delete process.env.TS_JEST_HOOKS
})
})

describe('processAsync', () => {
Expand Down
22 changes: 22 additions & 0 deletions src/ts-jest-transformer.ts
Expand Up @@ -10,6 +10,7 @@ import { ConfigSet } from './config'
import { DECLARATION_TYPE_EXT, JS_JSX_REGEX, TS_TSX_REGEX } from './constants'
import type { CompilerInstance, DepGraphInfo, ProjectConfigTsJest, TransformOptionsTsJest } from './types'
import { parse, stringify, JsonableValue, rootLogger } from './utils'
import { importer } from './utils/importer'
import { Errors, interpolate } from './utils/messages'
import { sha1 } from './utils/sha1'
import { VersionCheckers } from './utils/version-checkers'
Expand All @@ -24,6 +25,11 @@ interface CachedConfigSet {
watchMode: boolean
}

interface TsJestHooksMap {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
afterProcess?(args: any[], result: string | TransformedSource): string | TransformedSource | void
}

/**
* @internal
*/
Expand Down Expand Up @@ -144,6 +150,13 @@ export class TsJestTransformer implements SyncTransformer {
const isDefinitionFile = filePath.endsWith(DECLARATION_TYPE_EXT)
const isJsFile = JS_JSX_REGEX.test(filePath)
const isTsFile = !isDefinitionFile && TS_TSX_REGEX.test(filePath)
let hooksFile = process.env.TS_JEST_HOOKS
let hooks: TsJestHooksMap | undefined
/* istanbul ignore next (cover by e2e) */
if (hooksFile) {
hooksFile = path.resolve(configs.cwd, hooksFile)
hooks = importer.tryTheseOr(hooksFile, {})
}
if (shouldStringifyContent) {
// handles here what we should simply stringify
result = `module.exports=${stringify(fileContent)}`
Expand Down Expand Up @@ -179,6 +192,15 @@ export class TsJestTransformer implements SyncTransformer {
// do not instrument here, jest will do it anyway afterwards
result = babelJest.process(result, filePath, { ...transformOptions, instrument: false })
}
// This is not supposed to be a public API but we keep it as some people use it
if (hooks?.afterProcess) {
this._logger.debug({ fileName: filePath, hookName: 'afterProcess' }, 'calling afterProcess hook')

const newResult = hooks.afterProcess([fileContent, filePath, transformOptions.config, transformOptions], result)
if (newResult) {
return newResult
}
}

return result
}
Expand Down