• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

kulshekhar / ts-jest / 10047525090

22 Jul 2024 08:00PM UTC coverage: 95.285% (+0.03%) from 95.254%
10047525090

push

github

ahnpnl
build(deps): Update dependency @testing-library/jest-dom to ^6.4.7

794 of 891 branches covered (89.11%)

Branch coverage included in aggregate %.

4763 of 4941 relevant lines covered (96.4%)

1243.71 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

87.92
/src/cli/config/migrate.ts
1
import { existsSync } from 'fs'
6✔
2
import { basename, resolve } from 'path'
6✔
3

6✔
4
import type { Config } from '@jest/types'
6✔
5
import { createLogger } from 'bs-logger'
6✔
6
import stableStringify from 'fast-json-stable-stringify'
6✔
7
import { stringify as stringifyJson5 } from 'json5'
6✔
8

6✔
9
import type { CliCommand, CliCommandArgs } from '..'
6✔
10
import { createDefaultPreset, createJsWithBabelPreset, createJsWithTsPreset } from '../../presets/create-jest-preset'
6✔
11
import type { TsJestTransformerOptions } from '../../types'
6✔
12
import { backportJestConfig } from '../../utils/backports'
6✔
13
import { JestPresetNames, type TsJestPresetDescriptor, allPresets } from '../helpers/presets'
6✔
14

6✔
15
const migrateGlobalConfigToTransformConfig = (
6✔
16
  transformConfig: Config.InitialOptions['transform'],
102✔
17
  globalsTsJestConfig: TsJestTransformerOptions | undefined,
102✔
18
) => {
102✔
19
  if (transformConfig) {
102✔
20
    return Object.entries(transformConfig).reduce((previousValue, currentValue) => {
24✔
21
      const [key, transformOptions] = currentValue
48✔
22
      if (typeof transformOptions === 'string' && transformOptions.includes('ts-jest')) {
48✔
23
        return {
36✔
24
          ...previousValue,
36✔
25
          [key]: globalsTsJestConfig ? ['ts-jest', globalsTsJestConfig] : 'ts-jest',
36!
26
        }
36✔
27
      }
36✔
28

12✔
29
      return {
12✔
30
        ...previousValue,
12✔
31
        [key]: transformOptions,
12✔
32
      }
12✔
33
    }, {})
24✔
34
  }
24✔
35

78✔
36
  return {}
78✔
37
}
78✔
38

6✔
39
const migratePresetToTransformConfig = (
6✔
40
  transformConfig: Config.InitialOptions['transform'],
102✔
41
  preset: TsJestPresetDescriptor | undefined,
102✔
42
  globalsTsJestConfig: TsJestTransformerOptions | undefined,
102✔
43
) => {
102✔
44
  if (preset) {
102✔
45
    const transformConfigFromPreset =
102✔
46
      preset.name === JestPresetNames.jsWithTs
102✔
47
        ? createJsWithTsPreset(globalsTsJestConfig)
102✔
48
        : preset.name === JestPresetNames.jsWIthBabel
102✔
49
        ? createJsWithBabelPreset(globalsTsJestConfig)
90!
50
        : createDefaultPreset(globalsTsJestConfig)
90✔
51

102✔
52
    return {
102✔
53
      ...transformConfig,
102✔
54
      ...transformConfigFromPreset.transform,
102✔
55
    }
102✔
56
  }
102✔
57

×
58
  return transformConfig
×
59
}
×
60

6✔
61
/**
6✔
62
 * @internal
6✔
63
 */
6✔
64
export const run: CliCommand = async (args: CliCommandArgs /* , logger: Logger*/) => {
6✔
65
  const nullLogger = createLogger({ targets: [] })
114✔
66
  const file = args._[0]?.toString()
114!
67
  const filePath = resolve(process.cwd(), file)
114✔
68
  if (!existsSync(filePath)) {
114✔
69
    throw new Error(`Configuration file ${file} does not exists.`)
6✔
70
  }
6✔
71
  const name = basename(file)
108✔
72
  const isPackage = name === 'package.json'
108✔
73
  if (!/\.(js|json)$/.test(name)) {
114✔
74
    throw new TypeError(`Configuration file ${file} must be a JavaScript or JSON file.`)
6✔
75
  }
6✔
76
  let actualConfig: Config.InitialOptions = require(filePath)
102✔
77
  if (isPackage) {
114✔
78
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
72✔
79
    actualConfig = (actualConfig as any).jest
72✔
80
  }
72✔
81
  if (!actualConfig) actualConfig = {}
114!
82

102✔
83
  // migrate
102✔
84
  // first we backport our options
102✔
85
  const migratedConfig = backportJestConfig(nullLogger, actualConfig)
102✔
86
  let preset: TsJestPresetDescriptor | undefined
102✔
87
  if (migratedConfig.preset) {
114✔
88
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
18✔
89
    preset = (allPresets as any)[migratedConfig.preset] ?? allPresets[JestPresetNames.default]
18!
90
  } else {
114✔
91
    if (args.js) {
84✔
92
      preset = args.js === 'babel' ? allPresets[JestPresetNames.jsWIthBabel] : allPresets[JestPresetNames.jsWithTs]
12!
93
    } else {
84✔
94
      preset = allPresets[JestPresetNames.default]
72✔
95
    }
72✔
96
  }
84✔
97

102✔
98
  // check the extensions
102✔
99
  if (migratedConfig.moduleFileExtensions?.length && preset) {
114✔
100
    const presetValue = dedupSort(preset.value.moduleFileExtensions ?? []).join('::')
6!
101
    const migratedValue = dedupSort(migratedConfig.moduleFileExtensions).join('::')
6✔
102
    if (presetValue === migratedValue) {
6!
103
      delete migratedConfig.moduleFileExtensions
×
104
    }
×
105
  }
6✔
106
  // there is a testRegex, remove our testMatch
102✔
107
  if (typeof migratedConfig.testRegex === 'string' || migratedConfig.testRegex?.length) {
114✔
108
    delete migratedConfig.testMatch
18✔
109
  }
18✔
110
  // check the testMatch
84✔
111
  else if (migratedConfig.testMatch?.length && preset) {
84✔
112
    const presetValue = dedupSort(preset.value.testMatch ?? []).join('::')
18!
113
    const migratedValue = dedupSort(migratedConfig.testMatch).join('::')
18✔
114
    if (presetValue === migratedValue) {
18!
115
      delete migratedConfig.testMatch
×
116
    }
×
117
  }
18✔
118

102✔
119
  const globalsTsJestConfig = migratedConfig.globals?.['ts-jest']
114!
120
  migratedConfig.transform = migrateGlobalConfigToTransformConfig(migratedConfig.transform, globalsTsJestConfig)
114✔
121
  migratedConfig.transform = migratePresetToTransformConfig(migratedConfig.transform, preset, globalsTsJestConfig)
114✔
122

114✔
123
  cleanupConfig(actualConfig)
114✔
124
  cleanupConfig(migratedConfig)
114✔
125
  const before = stableStringify(actualConfig)
114✔
126
  const after = stableStringify(migratedConfig)
114✔
127
  if (after === before) {
114!
128
    process.stderr.write(`
×
129
No migration needed for given Jest configuration
×
130
    `)
×
131

×
132
    return
×
133
  }
×
134

102✔
135
  const stringify = file.endsWith('.json') ? JSON.stringify : stringifyJson5
114✔
136
  const prefix = file.endsWith('.json') ? '"jest": ' : 'module.exports = '
114✔
137

114✔
138
  // output new config
114✔
139
  process.stderr.write(`
114✔
140
Migrated Jest configuration:
114✔
141
`)
114✔
142
  process.stdout.write(`${prefix}${stringify(migratedConfig, undefined, '  ')}\n`)
114✔
143
}
114✔
144

6✔
145
function cleanupConfig(config: Config.InitialOptions): void {
204✔
146
  if (config.globals) {
204✔
147
    delete config.globals['ts-jest']
120✔
148
    if (!Object.keys(config.globals).length) {
120✔
149
      delete config.globals
120✔
150
    }
120✔
151
  }
120✔
152
  if (config.transform && !Object.keys(config.transform).length) {
204!
153
    delete config.transform
×
154
  }
×
155
  if (config.moduleFileExtensions) {
204✔
156
    config.moduleFileExtensions = dedupSort(config.moduleFileExtensions)
12✔
157
    if (!config.moduleFileExtensions.length) delete config.moduleFileExtensions
12!
158
  }
12✔
159
  if (config.testMatch) {
204✔
160
    config.testMatch = dedupSort(config.testMatch)
48✔
161
    if (!config.testMatch.length) delete config.testMatch
48!
162
  }
48✔
163
  delete config.preset
204✔
164
}
204✔
165

6✔
166
// eslint-disable-next-line @typescript-eslint/no-explicit-any
6✔
167
function dedupSort(arr: any[]) {
108✔
168
  return arr
108✔
169
    .filter((s, i, a) => a.findIndex((e) => s.toString() === e.toString()) === i)
108✔
170
    .sort((a, b) => (a.toString() > b.toString() ? 1 : a.toString() < b.toString() ? -1 : 0))
108!
171
}
108✔
172

6✔
173
/**
6✔
174
 * @internal
6✔
175
 */
6✔
176
export const help: CliCommand = async () => {
6✔
177
  process.stdout.write(`
6✔
178
Usage:
6✔
179
  ts-jest config:migrate [options] <config-file>
6✔
180

6✔
181
Arguments:
6✔
182
  <config-file>         Can be a js or json Jest config file. If it is a
6✔
183
                        package.json file, the configuration will be read from
6✔
184
                        the "jest" property.
6✔
185

6✔
186
Options:
6✔
187
  --js ts|babel         Process .js files with ts-jest if 'ts' or with
6✔
188
                        babel-jest if 'babel'
6✔
189
  --no-jest-preset      Disable the use of Jest presets
6✔
190
`)
6✔
191
}
6✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc