Skip to content

Commit

Permalink
refactor: use TsJestTransformerOptions everywhere possibly
Browse files Browse the repository at this point in the history
  • Loading branch information
ahnpnl committed Jul 5, 2024
1 parent 9e99c24 commit 7d001be
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 29 deletions.
10 changes: 5 additions & 5 deletions src/__helpers__/fakers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type { Logger } from 'bs-logger'

import { TsCompiler } from '../legacy/compiler'
import { ConfigSet } from '../legacy/config/config-set'
import type { StringMap, TsJestGlobalOptions } from '../types'
import type { StringMap, TsJestTransformerOptions } from '../types'
import type { ImportReasons } from '../utils/messages'

export function filePath(relPath: string): string {
Expand All @@ -19,7 +19,7 @@ const defaultTestMatch = ['**/__tests__/**/*.[jt]s?(x)', '**/?(*.)+(spec|test).[

function getJestConfig<T extends Config.ProjectConfig>(
options?: Partial<Config.InitialOptions | Config.ProjectConfig>,
tsJestOptions?: TsJestGlobalOptions,
tsJestOptions?: TsJestTransformerOptions,
): T {
const res = {
globals: {},
Expand Down Expand Up @@ -47,7 +47,7 @@ export function createConfigSet({
...others
}: {
jestConfig?: Partial<Config.ProjectConfig>
tsJestConfig?: TsJestGlobalOptions
tsJestConfig?: TsJestTransformerOptions
logger?: Logger
resolve?: ((path: string) => string) | null
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand Down Expand Up @@ -81,8 +81,8 @@ export function makeCompiler(
parentConfig,
}: {
jestConfig?: Partial<Config.ProjectConfig>
tsJestConfig?: TsJestGlobalOptions
parentConfig?: TsJestGlobalOptions
tsJestConfig?: TsJestTransformerOptions
parentConfig?: TsJestTransformerOptions
} = {},
jestCacheFS: StringMap = new Map<string, string>(),
): TsCompiler {
Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { TsJestTransformer } from './legacy/ts-jest-transformer'
import type { TsJestGlobalOptions } from './types'
import type { TsJestTransformerOptions } from './types'

export * from './config'
export * from './constants'
Expand All @@ -12,7 +12,7 @@ export * from './utils'
export * from './types'

export default {
createTransformer(tsJestConfig?: TsJestGlobalOptions) {
createTransformer(tsJestConfig?: TsJestTransformerOptions) {
return new TsJestTransformer(tsJestConfig)
},
}
4 changes: 2 additions & 2 deletions src/legacy/config/config-set.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import * as ts from 'typescript'
import { createConfigSet } from '../../__helpers__/fakers'
import { logTargetMock } from '../../__helpers__/mocks'
import type { RawCompilerOptions } from '../../raw-compiler-options'
import type { AstTransformerDesc, TsJestGlobalOptions } from '../../types'
import type { AstTransformerDesc, TsJestTransformerOptions } from '../../types'
import { stringify } from '../../utils'
import * as _backports from '../../utils/backports'
import { getPackageVersion } from '../../utils/get-package-version'
Expand Down Expand Up @@ -47,7 +47,7 @@ test('should create a default fallback jest config when jest config is undefined
})

describe('parsedTsConfig', () => {
const get = (tsJest?: TsJestGlobalOptions) => createConfigSet({ tsJestConfig: tsJest }).parsedTsConfig
const get = (tsJest?: TsJestTransformerOptions) => createConfigSet({ tsJestConfig: tsJest }).parsedTsConfig

it('should read file list from default tsconfig', () => {
// since the default is to lookup for tsconfig,
Expand Down
5 changes: 3 additions & 2 deletions src/legacy/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import type { TsJestGlobalOptions } from '../types'
import type { TsJestTransformerOptions } from '../types'

import { TsJestTransformer } from './ts-jest-transformer'

export default {
createTransformer: (tsJestConfig?: TsJestGlobalOptions): TsJestTransformer => new TsJestTransformer(tsJestConfig),
createTransformer: (tsJestConfig?: TsJestTransformerOptions): TsJestTransformer =>
new TsJestTransformer(tsJestConfig),
}
6 changes: 3 additions & 3 deletions src/legacy/ts-jest-transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export class TsJestTransformer implements SyncTransformer<TsJestTransformerOptio
private _depGraphs: Map<string, DepGraphInfo> = new Map<string, DepGraphInfo>()
private _watchMode = false

constructor(private readonly tsJestConfig?: TsJestTransformOptions['transformerConfig']) {
constructor(private readonly transformerOptions?: TsJestTransformerOptions) {
this._logger = rootLogger.child({ namespace: 'ts-jest-transformer' })
VersionCheckers.jest.warn()
/**
Expand Down Expand Up @@ -106,14 +106,14 @@ export class TsJestTransformer implements SyncTransformer<TsJestTransformerOptio
}
const jestGlobalsConfig = config.globals ?? {}
const tsJestGlobalsConfig = jestGlobalsConfig['ts-jest'] ?? {}
const migratedConfig = this.tsJestConfig
const migratedConfig = this.transformerOptions
? {
...config,
globals: {
...jestGlobalsConfig,
'ts-jest': {
...tsJestGlobalsConfig,
...this.tsJestConfig,
...this.transformerOptions,
},
},
}
Expand Down
30 changes: 15 additions & 15 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,6 @@ import { JS_TRANSFORM_PATTERN, TS_JS_TRANSFORM_PATTERN, TS_TRANSFORM_PATTERN } f
import type { ConfigSet } from './legacy/config/config-set'
import type { RawCompilerOptions } from './raw-compiler-options'

declare module '@jest/types' {
// eslint-disable-next-line @typescript-eslint/no-namespace
namespace Config {
interface ConfigGlobals {
/**
* strangely `@ts-expect-error` doesn't work in this case when running
* `npm run build` vs `npm run pretest`
*/
// eslint-disable-next-line
// @ts-ignore
'ts-jest'?: TsJestGlobalOptions
}
}
}

/**
* @internal
*/
Expand Down Expand Up @@ -269,3 +254,18 @@ export type JsWithBabelPreset = {
[TS_TRANSFORM_PATTERN]: ['ts-jest', Omit<TsJestTransformerOptions, 'useESM'>]
}
}

declare module '@jest/types' {
// eslint-disable-next-line @typescript-eslint/no-namespace
namespace Config {
interface ConfigGlobals {
/**
* strangely `@ts-expect-error` doesn't work in this case when running
* `npm run build` vs `npm run pretest`
*/
// eslint-disable-next-line
// @ts-ignore
'ts-jest'?: TsJestTransformerOptions
}
}
}

0 comments on commit 7d001be

Please sign in to comment.