@@ -6,16 +6,19 @@ import { basename, resolve } from 'path'
6
6
import { Arguments } from 'yargs'
7
7
8
8
import { CliCommand } from '..'
9
- import { createJestPreset } from '../../config/create-jest-preset '
9
+ import { TsJestPresets } from '../../types '
10
10
import { backportJestConfig } from '../../util/backports'
11
11
12
+ const DEFAULT_PRESET = 'ts-jest/presets/default'
13
+
12
14
/**
13
15
* @internal
14
16
*/
15
17
export const run : CliCommand = async ( args : Arguments /*, logger: Logger*/ ) => {
16
18
const nullLogger = createLogger ( { targets : [ ] } )
17
19
const file = args . _ [ 0 ]
18
20
const filePath = resolve ( process . cwd ( ) , file )
21
+ const footNotes : string [ ] = [ ]
19
22
if ( ! existsSync ( filePath ) ) {
20
23
throw new Error ( `Configuration file ${ file } does not exists.` )
21
24
}
@@ -33,29 +36,71 @@ export const run: CliCommand = async (args: Arguments /*, logger: Logger*/) => {
33
36
// migrate
34
37
// first we backport our options
35
38
const migratedConfig = backportJestConfig ( nullLogger , actualConfig )
39
+ let presetName : string | undefined
36
40
// then we check if we can use `preset`
37
41
if ( ! migratedConfig . preset && args . jestPreset ) {
38
- migratedConfig . preset = 'ts-jest'
39
- } else if ( ! args . jestPreset && migratedConfig . preset === 'ts-jest' ) {
40
- delete migratedConfig . preset
42
+ // find the best preset
43
+ if ( args . allowJs ) presetName = 'ts-jest/presets/js-with-ts'
44
+ else {
45
+ // try to detect what transformer the js extensions would target
46
+ const jsTransformers = Object . keys ( migratedConfig . transform || { } ) . reduce (
47
+ ( list , pattern ) => {
48
+ if ( RegExp ( pattern . replace ( / ^ < r o o t D i r > \/ ? / , '/dummy-project/' ) ) . test ( '/dummy-project/src/foo.js' ) ) {
49
+ let transformer : string = ( migratedConfig . transform as any ) [ pattern ]
50
+ if ( / \b b a b e l - j e s t \b / . test ( transformer ) ) transformer = 'babel-jest'
51
+ else if ( / \t s - j e s t \b / . test ( transformer ) ) transformer = 'ts-jest'
52
+ return [ ...list , transformer ]
53
+ }
54
+ return list
55
+ } ,
56
+ [ ] as string [ ] ,
57
+ )
58
+ // depending on the transformer found, we use one or the other preset
59
+ const jsWithTs = jsTransformers . includes ( 'ts-jest' )
60
+ const jsWithBabel = jsTransformers . includes ( 'babel-jest' )
61
+ if ( jsWithBabel && ! jsWithTs ) {
62
+ presetName = 'ts-jest/presets/js-with-babel'
63
+ } else if ( jsWithTs && ! jsWithBabel ) {
64
+ presetName = 'ts-jest/presets/js-with-ts'
65
+ } else {
66
+ // sounds like js files are NOT handled, or handled with a unknown transformer, so we do not need to handle it
67
+ presetName = DEFAULT_PRESET
68
+ }
69
+ }
70
+ // ensure we are using a preset
71
+ presetName = presetName || DEFAULT_PRESET
72
+ migratedConfig . preset = presetName
73
+ footNotes . push (
74
+ `Detected preset '${ presetName . replace (
75
+ / ^ t s - j e s t \/ p r e s e t s \/ / ,
76
+ '' ,
77
+ ) } ' as the best matching preset for your configuration.\nVisit https://kulshekhar.github.io/ts-jest/user/config/#jest-preset for more information about presets.\n`,
78
+ )
79
+ } else if ( migratedConfig . preset && migratedConfig . preset . startsWith ( 'ts-jest' ) ) {
80
+ if ( args . jestPreset === false ) {
81
+ delete migratedConfig . preset
82
+ } else {
83
+ presetName = migratedConfig . preset
84
+ }
41
85
}
42
- const usesPreset = migratedConfig . preset === 'ts-jest'
43
- const presets = createJestPreset ( { allowJs : args . allowJs } )
86
+ const presets : TsJestPresets | undefined = presetName
87
+ ? require ( `../../../${ presetName . replace ( / ^ t s - j e s t \/ / , '' ) } /jest-preset` )
88
+ : undefined
44
89
45
90
// check the extensions
46
- if ( migratedConfig . moduleFileExtensions && migratedConfig . moduleFileExtensions . length && usesPreset ) {
91
+ if ( migratedConfig . moduleFileExtensions && migratedConfig . moduleFileExtensions . length && presets ) {
47
92
const presetValue = dedupSort ( presets . moduleFileExtensions ) . join ( '::' )
48
93
const migratedValue = dedupSort ( migratedConfig . moduleFileExtensions ) . join ( '::' )
49
94
if ( presetValue === migratedValue ) {
50
95
delete migratedConfig . moduleFileExtensions
51
96
}
52
97
}
53
98
// there is a testRegex, remove our testMatch
54
- if ( migratedConfig . testRegex && usesPreset ) {
99
+ if ( migratedConfig . testRegex && presets ) {
55
100
migratedConfig . testMatch = null as any
56
101
}
57
102
// check the testMatch
58
- else if ( migratedConfig . testMatch && migratedConfig . testMatch . length && usesPreset ) {
103
+ else if ( migratedConfig . testMatch && migratedConfig . testMatch . length && presets ) {
59
104
const presetValue = dedupSort ( presets . testMatch ) . join ( '::' )
60
105
const migratedValue = dedupSort ( migratedConfig . testMatch ) . join ( '::' )
61
106
if ( presetValue === migratedValue ) {
@@ -75,7 +120,7 @@ export const run: CliCommand = async (args: Arguments /*, logger: Logger*/) => {
75
120
}
76
121
// check if it's the same as the preset's one
77
122
if (
78
- usesPreset &&
123
+ presets &&
79
124
migratedConfig . transform &&
80
125
stringifyJson ( migratedConfig . transform ) === stringifyJson ( presets . transform )
81
126
) {
@@ -95,7 +140,7 @@ No migration needed for given Jest configuration
95
140
}
96
141
97
142
const stringify = / \. j s o n $ / . test ( file ) ? JSON . stringify : stringifyJson5
98
- const footNotes : string [ ] = [ ]
143
+ const prefix = / \. j s o n $ / . test ( file ) ? '"jest": ' : 'module.exports = '
99
144
100
145
// if we are using preset, inform the user that he might be able to remove some section(s)
101
146
// we couldn't check for equality
@@ -109,7 +154,7 @@ No migration needed for given Jest configuration
109
154
// If it is the case, you can safely remove the "testMatch" from what I've migrated.
110
155
// `)
111
156
// }
112
- if ( usesPreset && migratedConfig . transform ) {
157
+ if ( presets && migratedConfig . transform ) {
113
158
footNotes . push ( `
114
159
I couldn't check if your "transform" value is the same as mine which is: ${ stringify (
115
160
presets . transform ,
@@ -124,7 +169,7 @@ If it is the case, you can safely remove the "transform" from what I've migrated
124
169
process . stderr . write ( `
125
170
Migrated Jest configuration:
126
171
` )
127
- process . stdout . write ( `${ stringify ( migratedConfig , undefined , ' ' ) } \n` )
172
+ process . stdout . write ( `${ prefix } ${ stringify ( migratedConfig , undefined , ' ' ) } \n` )
128
173
if ( footNotes . length ) {
129
174
process . stderr . write ( `
130
175
${ footNotes . join ( '\n' ) }
@@ -152,6 +197,7 @@ function cleanupConfig(config: jest.InitialOptions): void {
152
197
config . testMatch = dedupSort ( config . testMatch )
153
198
if ( config . testMatch . length === 0 ) delete config . testMatch
154
199
}
200
+ if ( config . preset === DEFAULT_PRESET ) config . preset = 'ts-jest'
155
201
}
156
202
157
203
function dedupSort ( arr : any [ ] ) {
0 commit comments