diff --git a/src/cli/cli.spec.ts b/src/cli/cli.spec.ts index 4d32974f7b..ad0dc1b7db 100644 --- a/src/cli/cli.spec.ts +++ b/src/cli/cli.spec.ts @@ -162,11 +162,11 @@ Jest configuration written to "${normalize('/foo/bar/jest.config.foo.js')}". /** @type {import('ts-jest').JestConfigWithTsJest} */ module.exports = { ...tsjPreset, - globals: { - 'ts-jest': { + transform: { + '^.+\\\\.[tj]sx?$': ['ts-jest', { tsconfig: 'tsconfig.test.json', babelConfig: true, - }, + }], }, };`, ], @@ -197,6 +197,7 @@ Jest configuration written to "${normalize('/foo/bar/package.json')}". ], ]) }) + it('should update package.json (with all options set)', async () => { expect.assertions(2) const res = await runCli(...fullOptions, 'package.json') @@ -216,13 +217,13 @@ Jest configuration written to "${normalize('/foo/bar/package.json')}". "version": "0.0.0-mock.0", "jest": { "transform": { - "^.+\\\\.[tj]sx?$": "ts-jest" - }, - "globals": { - "ts-jest": { - "tsconfig": "tsconfig.test.json", - "babelConfig": true - } + "^.+\\\\.[tj]sx?$": [ + "ts-jest", + { + "tsconfig": "tsconfig.test.json", + "babelConfig": true + } + ] } } }`, diff --git a/src/cli/config/init.ts b/src/cli/config/init.ts index 215fd0ab1d..92277bab66 100644 --- a/src/cli/config/init.ts +++ b/src/cli/config/init.ts @@ -10,7 +10,8 @@ import { basename, join } from 'path' import { stringify as stringifyJson5 } from 'json5' import type { CliCommand, CliCommandArgs } from '..' -import { TsJestPresetDescriptor, defaults, jsWIthBabel, jsWithTs } from '../helpers/presets' +import type { JestConfigWithTsJest, TsJestTransformerOptions } from '../../types' +import { type TsJestPresetDescriptor, defaults, jsWIthBabel, jsWithTs } from '../helpers/presets' /** * @internal @@ -25,7 +26,8 @@ export const run: CliCommand = async (args: CliCommandArgs /* , logger: Logger * const hasPackage = isPackage || existsSync(pkgFile) // read config const { jestPreset = true, tsconfig: askedTsconfig, force, jsdom } = args - const tsconfig = askedTsconfig === 'tsconfig.json' ? undefined : askedTsconfig + const tsconfig = + askedTsconfig === 'tsconfig.json' ? undefined : (askedTsconfig as TsJestTransformerOptions['tsconfig']) // read package const pkgJson = hasPackage ? JSON.parse(readFileSync(pkgFile, 'utf8')) : {} @@ -72,17 +74,36 @@ export const run: CliCommand = async (args: CliCommandArgs /* , logger: Logger * if (isPackage) { // package.json config - // eslint-disable-next-line @typescript-eslint/no-explicit-any - const base: any = jestPreset ? { preset: preset.name } : { ...preset.value } - if (!jsdom) base.testEnvironment = 'node' - if (tsconfig || shouldPostProcessWithBabel) { - // eslint-disable-next-line @typescript-eslint/no-explicit-any - const tsJestConf: any = {} - base.globals = { 'ts-jest': tsJestConf } - if (tsconfig) tsJestConf.tsconfig = tsconfig - if (shouldPostProcessWithBabel) tsJestConf.babelConfig = true + const jestConfig: JestConfigWithTsJest = jestPreset ? { preset: preset.name } : { ...preset.value } + if (!jsdom) jestConfig.testEnvironment = 'node' + const transformerConfig = Object.entries(jestConfig.transform ?? {}).reduce((acc, [fileRegex, transformerName]) => { + if (transformerName === 'ts-jest') { + if (tsconfig || shouldPostProcessWithBabel) { + const tsJestConf: TsJestTransformerOptions = {} + if (tsconfig) tsJestConf.tsconfig = tsconfig + if (shouldPostProcessWithBabel) tsJestConf.babelConfig = true + + return { + ...acc, + [fileRegex]: [transformerName, tsJestConf], + } + } + + return { + ...acc, + [fileRegex]: transformerName, + } + } + + return acc + }, {}) + if (Object.keys(transformerConfig).length) { + jestConfig.transform = { + ...jestConfig.transform, + ...transformerConfig, + } } - body = JSON.stringify({ ...pkgJson, jest: base }, undefined, ' ') + body = JSON.stringify({ ...pkgJson, jest: jestConfig }, undefined, ' ') } else { // js config const content = [] @@ -99,11 +120,11 @@ export const run: CliCommand = async (args: CliCommandArgs /* , logger: Logger * if (!jsdom) content.push(" testEnvironment: 'node',") if (tsconfig || shouldPostProcessWithBabel) { - content.push(' globals: {') - content.push(" 'ts-jest': {") + content.push(' transform: {') + content.push(" '^.+\\\\.[tj]sx?$': ['ts-jest', {") if (tsconfig) content.push(` tsconfig: ${stringifyJson5(tsconfig)},`) if (shouldPostProcessWithBabel) content.push(' babelConfig: true,') - content.push(' },') + content.push(' }],') content.push(' },') } content.push('};') diff --git a/src/types.ts b/src/types.ts index b99d434f6f..51e790d647 100644 --- a/src/types.ts +++ b/src/types.ts @@ -192,7 +192,7 @@ export interface GlobalConfigTsJest extends Config.ConfigGlobals { export interface InitialOptionsTsJest extends Config.InitialOptions { globals?: GlobalConfigTsJest } -type TsJestTransformerOptions = TsJestGlobalOptions +export type TsJestTransformerOptions = TsJestGlobalOptions export interface JestConfigWithTsJest extends Omit { transform?: { [regex: string]: 'ts-jest' | ['ts-jest', TsJestTransformerOptions] | string | [string, Record]