Skip to content

Commit

Permalink
build: reference transform options via jest TransformOptions type
Browse files Browse the repository at this point in the history
  • Loading branch information
Anh Pham authored and ahnpnl committed Jan 22, 2024
1 parent a5d6f2d commit 19778bd
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 20 deletions.
11 changes: 5 additions & 6 deletions src/legacy/config/config-set.ts
Expand Up @@ -25,9 +25,8 @@ import type {
AstTransformerDesc,
BabelConfig,
BabelJestTransformer,
ProjectConfigTsJest,
TsJestAstTransformer,
TsJestGlobalOptions,
TsJestTransformOptions,
TTypeScript,
} from '../../types'
import { TsCompilerInstance } from '../../types'
Expand Down Expand Up @@ -145,7 +144,7 @@ export class ConfigSet {
/**
* @internal
*/
private _jestCfg!: ProjectConfigTsJest
private _jestCfg!: TsJestTransformOptions['config']
/**
* @internal
*/
Expand Down Expand Up @@ -187,7 +186,7 @@ export class ConfigSet {
tsBuildInfoFile: undefined,
}

constructor(jestConfig: ProjectConfigTsJest | undefined, readonly parentLogger?: Logger) {
constructor(jestConfig: TsJestTransformOptions['config'] | undefined, readonly parentLogger?: Logger) {
this.logger = this.parentLogger
? this.parentLogger.child({ [LogContexts.namespace]: 'config' })
: rootLogger.child({ namespace: 'config' })
Expand Down Expand Up @@ -224,7 +223,7 @@ export class ConfigSet {
/**
* @internal
*/
private _backportJestCfg(jestCfg: ProjectConfigTsJest): void {
private _backportJestCfg(jestCfg: TsJestTransformOptions['config']): void {
const config = backportJestConfig(this.logger, jestCfg)

this.logger.debug({ jestConfig: config }, 'normalized jest config')
Expand All @@ -239,7 +238,7 @@ export class ConfigSet {
/**
* @internal
*/
private _setupConfigSet(options: TsJestGlobalOptions): void {
private _setupConfigSet(options: TsJestTransformOptions['transformerConfig']): void {
// useESM
this.useESM = options.useESM ?? false

Expand Down
27 changes: 13 additions & 14 deletions src/legacy/ts-jest-transformer.ts
Expand Up @@ -9,9 +9,8 @@ import type {
CompiledOutput,
CompilerInstance,
DepGraphInfo,
ProjectConfigTsJest,
TransformOptionsTsJest,
TsJestGlobalOptions,
TsJestTransformerOptions,
TsJestTransformOptions,
} from '../types'
import { parse, stringify, JsonableValue, rootLogger } from '../utils'
import { importer } from '../utils/importer'
Expand All @@ -24,7 +23,7 @@ import { ConfigSet } from './config/config-set'

interface CachedConfigSet {
configSet: ConfigSet
jestConfig: JsonableValue<ProjectConfigTsJest>
jestConfig: JsonableValue<TsJestTransformOptions['config']>
transformerCfgStr: string
compiler: CompilerInstance
depGraphs: Map<string, DepGraphInfo>
Expand All @@ -42,7 +41,7 @@ interface TsJestHooksMap {
*/
export const CACHE_KEY_EL_SEPARATOR = '\x00'

export class TsJestTransformer implements SyncTransformer {
export class TsJestTransformer implements SyncTransformer<TsJestTransformerOptions> {
/**
* cache ConfigSet between test runs
*
Expand All @@ -56,7 +55,7 @@ export class TsJestTransformer implements SyncTransformer {
private _depGraphs: Map<string, DepGraphInfo> = new Map<string, DepGraphInfo>()
private _watchMode = false

constructor(private readonly tsJestConfig?: TsJestGlobalOptions) {
constructor(private readonly tsJestConfig?: TsJestTransformOptions['transformerConfig']) {
this._logger = rootLogger.child({ namespace: 'ts-jest-transformer' })
VersionCheckers.jest.warn()
/**
Expand All @@ -72,7 +71,7 @@ export class TsJestTransformer implements SyncTransformer {
process.env.TS_JEST = '1'
}

private _configsFor(transformOptions: TransformOptionsTsJest): ConfigSet {
private _configsFor(transformOptions: TsJestTransformOptions): ConfigSet {
const { config, cacheFS } = transformOptions
const ccs: CachedConfigSet | undefined = TsJestTransformer._cachedConfigSets.find(
(cs) => cs.jestConfig.value === config,
Expand Down Expand Up @@ -148,7 +147,7 @@ export class TsJestTransformer implements SyncTransformer {
}

// eslint-disable-next-line class-methods-use-this
protected _createConfigSet(config: ProjectConfigTsJest | undefined): ConfigSet {
protected _createConfigSet(config: TsJestTransformOptions['config'] | undefined): ConfigSet {
return new ConfigSet(config)
}

Expand All @@ -159,7 +158,7 @@ export class TsJestTransformer implements SyncTransformer {
/**
* @public
*/
process(sourceText: string, sourcePath: string, transformOptions: TransformOptionsTsJest): TransformedSource {
process(sourceText: string, sourcePath: string, transformOptions: TsJestTransformOptions): TransformedSource {
this._logger.debug({ fileName: sourcePath, transformOptions }, 'processing', sourcePath)

const configs = this._configsFor(transformOptions)
Expand All @@ -185,7 +184,7 @@ export class TsJestTransformer implements SyncTransformer {
async processAsync(
sourceText: string,
sourcePath: string,
transformOptions: TransformOptionsTsJest,
transformOptions: TsJestTransformOptions,
): Promise<TransformedSource> {
this._logger.debug({ fileName: sourcePath, transformOptions }, 'processing', sourcePath)

Expand Down Expand Up @@ -217,7 +216,7 @@ export class TsJestTransformer implements SyncTransformer {
private processWithTs(
sourceText: string,
sourcePath: string,
transformOptions: TransformOptionsTsJest,
transformOptions: TsJestTransformOptions,
): CompiledOutput {
let result: TransformedSource
const configs = this._configsFor(transformOptions)
Expand Down Expand Up @@ -269,7 +268,7 @@ export class TsJestTransformer implements SyncTransformer {
private runTsJestHook(
sourcePath: string,
sourceText: string,
transformOptions: TransformOptionsTsJest,
transformOptions: TsJestTransformOptions,
compiledOutput: TransformedSource,
) {
let hooksFile = process.env.TS_JEST_HOOKS
Expand Down Expand Up @@ -302,7 +301,7 @@ export class TsJestTransformer implements SyncTransformer {
*
* @public
*/
getCacheKey(fileContent: string, filePath: string, transformOptions: TransformOptionsTsJest): string {
getCacheKey(fileContent: string, filePath: string, transformOptions: TsJestTransformOptions): string {
const configs = this._configsFor(transformOptions)

this._logger.debug({ fileName: filePath, transformOptions }, 'computing cache key for', filePath)
Expand Down Expand Up @@ -365,7 +364,7 @@ export class TsJestTransformer implements SyncTransformer {
async getCacheKeyAsync(
sourceText: string,
sourcePath: string,
transformOptions: TransformOptionsTsJest,
transformOptions: TsJestTransformOptions,
): Promise<string> {
return Promise.resolve(this.getCacheKey(sourceText, sourcePath, transformOptions))
}
Expand Down
3 changes: 3 additions & 0 deletions src/types.ts
Expand Up @@ -188,6 +188,9 @@ export interface InitialOptionsTsJest extends Config.InitialOptions {
globals?: GlobalConfigTsJest
}
export type TsJestTransformerOptions = TsJestGlobalOptions

export type TsJestTransformOptions = TransformOptions<TsJestTransformerOptions>

export interface JestConfigWithTsJest extends Omit<Config.InitialOptions, 'transform'> {
transform?: {
[regex: string]:
Expand Down

0 comments on commit 19778bd

Please sign in to comment.