/
migrate.ts
176 lines (162 loc) · 5.74 KB
/
migrate.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import { createLogger } from 'bs-logger'
import stringifyJson from 'fast-json-stable-stringify'
import { existsSync } from 'fs'
import { stringify as stringifyJson5 } from 'json5'
import { basename, resolve } from 'path'
import { Arguments } from 'yargs'
import { CliCommand } from '..'
import { createJestPreset } from '../../config/create-jest-preset'
import { backportJestConfig } from '../../util/backports'
/**
* @internal
*/
export const run: CliCommand = async (args: Arguments /*, logger: Logger*/) => {
const nullLogger = createLogger({ targets: [] })
const file = args._[0]
const filePath = resolve(process.cwd(), file)
if (!existsSync(filePath)) {
throw new Error(`Configuration file ${file} does not exists.`)
}
const name = basename(file)
const isPackage = name === 'package.json'
if (!/\.(js|json)$/.test(name)) {
throw new TypeError(`Configuration file ${file} must be a JavaScript or JSON file.`)
}
let actualConfig: jest.InitialOptions = require(filePath)
if (isPackage) {
actualConfig = (actualConfig as any).jest
}
if (!actualConfig) actualConfig = {}
// migrate
// first we backport our options
const migratedConfig = backportJestConfig(nullLogger, actualConfig)
// then we check if we can use `preset`
if (!migratedConfig.preset && args.jestPreset) {
migratedConfig.preset = 'ts-jest'
} else if (!args.jestPreset && migratedConfig.preset === 'ts-jest') {
delete migratedConfig.preset
}
const usesPreset = migratedConfig.preset === 'ts-jest'
const presets = createJestPreset({ allowJs: args.allowJs })
// check the extensions
if (migratedConfig.moduleFileExtensions && migratedConfig.moduleFileExtensions.length && usesPreset) {
const presetValue = dedupSort(presets.moduleFileExtensions).join('::')
const migratedValue = dedupSort(migratedConfig.moduleFileExtensions).join('::')
if (presetValue === migratedValue) {
delete migratedConfig.moduleFileExtensions
}
}
// check the testMatch
if (migratedConfig.testMatch && migratedConfig.testMatch.length && usesPreset) {
const presetValue = dedupSort(presets.testMatch).join('::')
const migratedValue = dedupSort(migratedConfig.testMatch).join('::')
if (presetValue === migratedValue) {
delete migratedConfig.testMatch
}
}
// migrate the transform
if (migratedConfig.transform) {
Object.keys(migratedConfig.transform).forEach(key => {
const val = (migratedConfig.transform as any)[key]
if (typeof val === 'string' && /\/?ts-jest(?:\/preprocessor\.js)?$/.test(val)) {
// tslint:disable-next-line:semicolon
;(migratedConfig.transform as any)[key] = 'ts-jest'
}
})
}
// check if it's the same as the preset's one
if (
usesPreset &&
migratedConfig.transform &&
stringifyJson(migratedConfig.transform) === stringifyJson(presets.transform)
) {
delete migratedConfig.transform
}
// cleanup
cleanupConfig(actualConfig)
cleanupConfig(migratedConfig)
const before = stringifyJson(actualConfig)
const after = stringifyJson(migratedConfig)
if (after === before) {
process.stderr.write(`
No migration needed for given Jest configuration
`)
return
}
const stringify = /\.json$/.test(file) ? JSON.stringify : stringifyJson5
const footNotes: string[] = []
// if we are using preset, inform the user that he might be able to remove some section(s)
// we couldn't check for equality
// if (usesPreset && migratedConfig.testMatch) {
// footNotes.push(`
// I couldn't check if your "testMatch" value is the same as mine which is: ${stringify(
// presets.testMatch,
// undefined,
// ' ',
// )}
// If it is the case, you can safely remove the "testMatch" from what I've migrated.
// `)
// }
if (usesPreset && migratedConfig.transform) {
footNotes.push(`
I couldn't check if your "transform" value is the same as mine which is: ${stringify(
presets.transform,
undefined,
' ',
)}
If it is the case, you can safely remove the "transform" from what I've migrated.
`)
}
// output new config
process.stderr.write(`
Migrated Jest configuration:
`)
process.stdout.write(`${stringify(migratedConfig, undefined, ' ')}\n`)
if (footNotes.length) {
process.stderr.write(`
${footNotes.join('\n')}
`)
}
}
function cleanupConfig(config: jest.InitialOptions): void {
if (config.globals) {
if ((config as any).globals['ts-jest'] && Object.keys((config as any).globals['ts-jest']).length === 0) {
delete (config as any).globals['ts-jest']
}
if (Object.keys(config.globals).length === 0) {
delete config.globals
}
}
if (config.transform && Object.keys(config.transform).length === 0) {
delete config.transform
}
if (config.moduleFileExtensions) {
config.moduleFileExtensions = dedupSort(config.moduleFileExtensions)
if (config.moduleFileExtensions.length === 0) delete config.moduleFileExtensions
}
if (config.testMatch) {
config.testMatch = dedupSort(config.testMatch)
if (config.testMatch.length === 0) delete config.testMatch
}
}
function dedupSort(arr: any[]) {
return arr
.filter((s, i, a) => a.findIndex(e => s.toString() === e.toString()) === i)
.sort((a, b) => (a.toString() > b.toString() ? 1 : a.toString() < b.toString() ? -1 : 0))
}
/**
* @internal
*/
export const help: CliCommand = async () => {
process.stdout.write(`
Usage:
ts-jest config:migrate [options] <config-file>
Arguments:
<config-file> Can be a js or json Jest config file. If it is a
package.json file, the configuration will be read from
the "jest" property.
Options:
--allow-js ts-jest will be used to process JS files as well
--no-jest-preset Disable the use of Jest presets
`)
}