Skip to content

Commit 214cb8c

Browse files
authored
fix: use transformer config in config:init instead of globals (#3829)
1 parent c53a2a8 commit 214cb8c

File tree

3 files changed

+48
-26
lines changed

3 files changed

+48
-26
lines changed

src/cli/cli.spec.ts

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -162,11 +162,11 @@ Jest configuration written to "${normalize('/foo/bar/jest.config.foo.js')}".
162162
/** @type {import('ts-jest').JestConfigWithTsJest} */
163163
module.exports = {
164164
...tsjPreset,
165-
globals: {
166-
'ts-jest': {
165+
transform: {
166+
'^.+\\\\.[tj]sx?$': ['ts-jest', {
167167
tsconfig: 'tsconfig.test.json',
168168
babelConfig: true,
169-
},
169+
}],
170170
},
171171
};`,
172172
],
@@ -197,6 +197,7 @@ Jest configuration written to "${normalize('/foo/bar/package.json')}".
197197
],
198198
])
199199
})
200+
200201
it('should update package.json (with all options set)', async () => {
201202
expect.assertions(2)
202203
const res = await runCli(...fullOptions, 'package.json')
@@ -216,13 +217,13 @@ Jest configuration written to "${normalize('/foo/bar/package.json')}".
216217
"version": "0.0.0-mock.0",
217218
"jest": {
218219
"transform": {
219-
"^.+\\\\.[tj]sx?$": "ts-jest"
220-
},
221-
"globals": {
222-
"ts-jest": {
223-
"tsconfig": "tsconfig.test.json",
224-
"babelConfig": true
225-
}
220+
"^.+\\\\.[tj]sx?$": [
221+
"ts-jest",
222+
{
223+
"tsconfig": "tsconfig.test.json",
224+
"babelConfig": true
225+
}
226+
]
226227
}
227228
}
228229
}`,

src/cli/config/init.ts

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ import { basename, join } from 'path'
1010
import { stringify as stringifyJson5 } from 'json5'
1111

1212
import type { CliCommand, CliCommandArgs } from '..'
13-
import { TsJestPresetDescriptor, defaults, jsWIthBabel, jsWithTs } from '../helpers/presets'
13+
import type { JestConfigWithTsJest, TsJestTransformerOptions } from '../../types'
14+
import { type TsJestPresetDescriptor, defaults, jsWIthBabel, jsWithTs } from '../helpers/presets'
1415

1516
/**
1617
* @internal
@@ -25,7 +26,8 @@ export const run: CliCommand = async (args: CliCommandArgs /* , logger: Logger *
2526
const hasPackage = isPackage || existsSync(pkgFile)
2627
// read config
2728
const { jestPreset = true, tsconfig: askedTsconfig, force, jsdom } = args
28-
const tsconfig = askedTsconfig === 'tsconfig.json' ? undefined : askedTsconfig
29+
const tsconfig =
30+
askedTsconfig === 'tsconfig.json' ? undefined : (askedTsconfig as TsJestTransformerOptions['tsconfig'])
2931
// read package
3032
const pkgJson = hasPackage ? JSON.parse(readFileSync(pkgFile, 'utf8')) : {}
3133

@@ -72,17 +74,36 @@ export const run: CliCommand = async (args: CliCommandArgs /* , logger: Logger *
7274

7375
if (isPackage) {
7476
// package.json config
75-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
76-
const base: any = jestPreset ? { preset: preset.name } : { ...preset.value }
77-
if (!jsdom) base.testEnvironment = 'node'
78-
if (tsconfig || shouldPostProcessWithBabel) {
79-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
80-
const tsJestConf: any = {}
81-
base.globals = { 'ts-jest': tsJestConf }
82-
if (tsconfig) tsJestConf.tsconfig = tsconfig
83-
if (shouldPostProcessWithBabel) tsJestConf.babelConfig = true
77+
const jestConfig: JestConfigWithTsJest = jestPreset ? { preset: preset.name } : { ...preset.value }
78+
if (!jsdom) jestConfig.testEnvironment = 'node'
79+
const transformerConfig = Object.entries(jestConfig.transform ?? {}).reduce((acc, [fileRegex, transformerName]) => {
80+
if (transformerName === 'ts-jest') {
81+
if (tsconfig || shouldPostProcessWithBabel) {
82+
const tsJestConf: TsJestTransformerOptions = {}
83+
if (tsconfig) tsJestConf.tsconfig = tsconfig
84+
if (shouldPostProcessWithBabel) tsJestConf.babelConfig = true
85+
86+
return {
87+
...acc,
88+
[fileRegex]: [transformerName, tsJestConf],
89+
}
90+
}
91+
92+
return {
93+
...acc,
94+
[fileRegex]: transformerName,
95+
}
96+
}
97+
98+
return acc
99+
}, {})
100+
if (Object.keys(transformerConfig).length) {
101+
jestConfig.transform = {
102+
...jestConfig.transform,
103+
...transformerConfig,
104+
}
84105
}
85-
body = JSON.stringify({ ...pkgJson, jest: base }, undefined, ' ')
106+
body = JSON.stringify({ ...pkgJson, jest: jestConfig }, undefined, ' ')
86107
} else {
87108
// js config
88109
const content = []
@@ -99,11 +120,11 @@ export const run: CliCommand = async (args: CliCommandArgs /* , logger: Logger *
99120
if (!jsdom) content.push(" testEnvironment: 'node',")
100121

101122
if (tsconfig || shouldPostProcessWithBabel) {
102-
content.push(' globals: {')
103-
content.push(" 'ts-jest': {")
123+
content.push(' transform: {')
124+
content.push(" '^.+\\\\.[tj]sx?$': ['ts-jest', {")
104125
if (tsconfig) content.push(` tsconfig: ${stringifyJson5(tsconfig)},`)
105126
if (shouldPostProcessWithBabel) content.push(' babelConfig: true,')
106-
content.push(' },')
127+
content.push(' }],')
107128
content.push(' },')
108129
}
109130
content.push('};')

src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ export interface GlobalConfigTsJest extends Config.ConfigGlobals {
192192
export interface InitialOptionsTsJest extends Config.InitialOptions {
193193
globals?: GlobalConfigTsJest
194194
}
195-
type TsJestTransformerOptions = TsJestGlobalOptions
195+
export type TsJestTransformerOptions = TsJestGlobalOptions
196196
export interface JestConfigWithTsJest extends Omit<Config.InitialOptions, 'transform'> {
197197
transform?: {
198198
[regex: string]: 'ts-jest' | ['ts-jest', TsJestTransformerOptions] | string | [string, Record<string, unknown>]

0 commit comments

Comments
 (0)