@@ -10,7 +10,8 @@ import { basename, join } from 'path'
10
10
import { stringify as stringifyJson5 } from 'json5'
11
11
12
12
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'
14
15
15
16
/**
16
17
* @internal
@@ -25,7 +26,8 @@ export const run: CliCommand = async (args: CliCommandArgs /* , logger: Logger *
25
26
const hasPackage = isPackage || existsSync ( pkgFile )
26
27
// read config
27
28
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' ] )
29
31
// read package
30
32
const pkgJson = hasPackage ? JSON . parse ( readFileSync ( pkgFile , 'utf8' ) ) : { }
31
33
@@ -72,17 +74,36 @@ export const run: CliCommand = async (args: CliCommandArgs /* , logger: Logger *
72
74
73
75
if ( isPackage ) {
74
76
// 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
+ }
84
105
}
85
- body = JSON . stringify ( { ...pkgJson , jest : base } , undefined , ' ' )
106
+ body = JSON . stringify ( { ...pkgJson , jest : jestConfig } , undefined , ' ' )
86
107
} else {
87
108
// js config
88
109
const content = [ ]
@@ -99,11 +120,11 @@ export const run: CliCommand = async (args: CliCommandArgs /* , logger: Logger *
99
120
if ( ! jsdom ) content . push ( " testEnvironment: 'node'," )
100
121
101
122
if ( tsconfig || shouldPostProcessWithBabel ) {
102
- content . push ( ' globals : {' )
103
- content . push ( " 'ts-jest': {" )
123
+ content . push ( ' transform : {' )
124
+ content . push ( " '^.+\\\\.[tj]sx?$': [' ts-jest', {" )
104
125
if ( tsconfig ) content . push ( ` tsconfig: ${ stringifyJson5 ( tsconfig ) } ,` )
105
126
if ( shouldPostProcessWithBabel ) content . push ( ' babelConfig: true,' )
106
- content . push ( ' },' )
127
+ content . push ( ' }] ,' )
107
128
content . push ( ' },' )
108
129
}
109
130
content . push ( '};' )
0 commit comments