Skip to content

Commit c568f49

Browse files
authoredMay 26, 2021
fix(cli): keep testMatch if old jest config contains it (#2618)
Closes #2616
1 parent 0ea025e commit c568f49

File tree

4 files changed

+141
-33
lines changed

4 files changed

+141
-33
lines changed
 

‎CHANGELOG.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ module.exports = {
115115
```
116116
* Remove possibilities to import `mocked`, `createJestPreset`, `pathsToModuleNameMapper` from package entry. One should change to
117117
```
118-
import { mocked, createJestPreset, pathsToModuleNameMapper` } from 'ts-jest/utils'
118+
import { mocked, createJestPreset, pathsToModuleNameMapper } from 'ts-jest/utils'
119119
```
120120
* **config:** One currently uses `tsConfig` should change to `tsconfig` in your `jest.config.js` or `package.json`.
121121

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`config migrate should migrate preset if valid preset value is used 1`] = `
4+
"
5+
No migration needed for given Jest configuration
6+
"
7+
`;
8+
9+
exports[`config migrate should migrate preset if valid preset value is used 2`] = `
10+
"\\"jest\\": {
11+
\\"preset\\": \\"ts-jest\\"
12+
}
13+
"
14+
`;
15+
16+
exports[`config migrate should migrate preset if valid preset value is used 3`] = `
17+
"
18+
No migration needed for given Jest configuration
19+
"
20+
`;
21+
22+
exports[`config migrate should reset testMatch if testRegex is used 1`] = `
23+
"\\"jest\\": {
24+
\\"testRegex\\": \\"foo-pattern\\",
25+
\\"preset\\": \\"ts-jest\\"
26+
}
27+
"
28+
`;
29+
30+
exports[`config migrate should reset testMatch if testRegex is used 2`] = `
31+
"\\"jest\\": {
32+
\\"testRegex\\": [
33+
\\"foo-pattern\\"
34+
],
35+
\\"preset\\": \\"ts-jest\\"
36+
}
37+
"
38+
`;
39+
40+
exports[`config migrate should reset testMatch if testRegex is used 3`] = `
41+
"\\"jest\\": {
42+
\\"testRegex\\": [],
43+
\\"testMatch\\": [
44+
\\"**/__tests__/**/*.(spec|test).[tj]s?(x)\\"
45+
],
46+
\\"preset\\": \\"ts-jest\\"
47+
}
48+
"
49+
`;
50+
51+
exports[`config migrate should reset testMatch if testRegex is used 4`] = `
52+
"\\"jest\\": {
53+
\\"testMatch\\": [
54+
\\"**/__tests__/**/*.(spec|test).[tj]s?(x)\\"
55+
],
56+
\\"preset\\": \\"ts-jest\\"
57+
}
58+
"
59+
`;
60+
61+
exports[`config migrate should reset testMatch if testRegex is used 5`] = `
62+
"\\"jest\\": {
63+
\\"testRegex\\": \\"foo-pattern\\",
64+
\\"preset\\": \\"ts-jest\\"
65+
}
66+
"
67+
`;

‎src/cli/cli.spec.ts

+60-18
Original file line numberDiff line numberDiff line change
@@ -413,27 +413,69 @@ Migrated Jest configuration:
413413
`)
414414
})
415415

416-
it('should reset testMatch if testRegex is used', async () => {
416+
test.each([
417+
{
418+
jest: {
419+
preset: 'ts-jest',
420+
},
421+
},
422+
{
423+
jest: {
424+
preset: 'ts-jest/foo',
425+
},
426+
},
427+
{
428+
jest: {
429+
preset: 'foo-preset',
430+
},
431+
},
432+
])('should migrate preset if valid preset value is used', async (jestCfg) => {
417433
expect.assertions(1)
418434
fs.existsSync.mockImplementation(() => true)
419-
jest.mock(
420-
pkgPaths.next,
421-
() => ({
422-
jest: {
423-
testRegex: 'foo-pattern',
424-
},
425-
}),
426-
{ virtual: true },
427-
)
435+
jest.mock(pkgPaths.next, () => jestCfg, { virtual: true })
436+
428437
const res = await runCli(...noOption, pkgPaths.current)
429-
expect(res.stdout).toMatchInlineSnapshot(`
430-
"\\"jest\\": {
431-
\\"testRegex\\": \\"foo-pattern\\",
432-
\\"preset\\": \\"ts-jest\\",
433-
\\"testMatch\\": null
434-
}
435-
"
436-
`)
438+
439+
expect(res.stdout ? res.stdout : res.stderr).toMatchSnapshot()
440+
})
441+
442+
test.each([
443+
{
444+
jest: {
445+
testRegex: 'foo-pattern',
446+
testMatch: ['**/__tests__/**/*.(spec|test).[tj]s?(x)'],
447+
},
448+
},
449+
{
450+
jest: {
451+
testRegex: ['foo-pattern'],
452+
testMatch: ['**/__tests__/**/*.(spec|test).[tj]s?(x)'],
453+
},
454+
},
455+
{
456+
jest: {
457+
testRegex: [],
458+
testMatch: ['**/__tests__/**/*.(spec|test).[tj]s?(x)'],
459+
},
460+
},
461+
{
462+
jest: {
463+
testMatch: ['**/__tests__/**/*.(spec|test).[tj]s?(x)'],
464+
},
465+
},
466+
{
467+
jest: {
468+
testRegex: 'foo-pattern',
469+
},
470+
},
471+
])('should reset testMatch if testRegex is used', async (jestCfg) => {
472+
expect.assertions(1)
473+
fs.existsSync.mockImplementation(() => true)
474+
jest.mock(pkgPaths.next, () => jestCfg, { virtual: true })
475+
476+
const res = await runCli(...noOption, pkgPaths.current)
477+
478+
expect(res.stdout).toMatchSnapshot()
437479
})
438480

439481
it('should detect best preset', async () => {

‎src/cli/config/migrate.ts

+13-14
Original file line numberDiff line numberDiff line change
@@ -71,41 +71,40 @@ export const run: CliCommand = async (args: Arguments /* , logger: Logger*/) =>
7171
}
7272
}
7373
// ensure we are using a preset
74-
presetName = presetName || JestPresetNames.default
74+
presetName = presetName ?? JestPresetNames.default
7575
preset = allPresets[presetName]
7676
footNotes.push(
7777
`Detected preset '${preset.label}' as the best matching preset for your configuration.
7878
Visit https://kulshekhar.github.io/ts-jest/user/config/#jest-preset for more information about presets.
7979
`,
8080
)
81-
} else if (migratedConfig.preset && migratedConfig.preset.startsWith('ts-jest')) {
81+
} else if (migratedConfig.preset?.startsWith('ts-jest')) {
8282
if (args.jestPreset === false) {
8383
delete migratedConfig.preset
8484
} else {
8585
// eslint-disable-next-line @typescript-eslint/no-explicit-any
86-
preset = (allPresets as any)[migratedConfig.preset] || defaults
86+
preset = (allPresets as any)[migratedConfig.preset] ?? defaults
8787
}
8888
}
8989

9090
// enforce the correct name
9191
if (preset) migratedConfig.preset = preset.name
9292

9393
// check the extensions
94-
if (migratedConfig.moduleFileExtensions && migratedConfig.moduleFileExtensions.length && preset) {
95-
const presetValue = dedupSort(preset.value.moduleFileExtensions || []).join('::')
94+
if (migratedConfig.moduleFileExtensions?.length && preset) {
95+
const presetValue = dedupSort(preset.value.moduleFileExtensions ?? []).join('::')
9696
const migratedValue = dedupSort(migratedConfig.moduleFileExtensions).join('::')
9797
if (presetValue === migratedValue) {
9898
delete migratedConfig.moduleFileExtensions
9999
}
100100
}
101101
// there is a testRegex, remove our testMatch
102-
if (migratedConfig.testRegex && preset) {
103-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
104-
migratedConfig.testMatch = null as any
102+
if ((typeof migratedConfig.testRegex === 'string' || migratedConfig.testRegex?.length) && preset) {
103+
delete migratedConfig.testMatch
105104
}
106105
// check the testMatch
107-
else if (migratedConfig.testMatch && migratedConfig.testMatch.length && preset) {
108-
const presetValue = dedupSort(preset.value.testMatch || []).join('::')
106+
else if (migratedConfig.testMatch?.length && preset) {
107+
const presetValue = dedupSort(preset.value.testMatch ?? []).join('::')
109108
const migratedValue = dedupSort(migratedConfig.testMatch).join('::')
110109
if (presetValue === migratedValue) {
111110
delete migratedConfig.testMatch
@@ -190,20 +189,20 @@ function cleanupConfig(config: Config.InitialOptions): void {
190189
// eslint-disable-next-line @typescript-eslint/no-explicit-any
191190
delete (config as any).globals['ts-jest']
192191
}
193-
if (Object.keys(config.globals).length === 0) {
192+
if (!Object.keys(config.globals).length) {
194193
delete config.globals
195194
}
196195
}
197-
if (config.transform && Object.keys(config.transform).length === 0) {
196+
if (config.transform && !Object.keys(config.transform).length) {
198197
delete config.transform
199198
}
200199
if (config.moduleFileExtensions) {
201200
config.moduleFileExtensions = dedupSort(config.moduleFileExtensions)
202-
if (config.moduleFileExtensions.length === 0) delete config.moduleFileExtensions
201+
if (!config.moduleFileExtensions.length) delete config.moduleFileExtensions
203202
}
204203
if (config.testMatch) {
205204
config.testMatch = dedupSort(config.testMatch)
206-
if (config.testMatch.length === 0) delete config.testMatch
205+
if (!config.testMatch.length) delete config.testMatch
207206
}
208207
if (config.preset === JestPresetNames.default) config.preset = defaults.name
209208
}

0 commit comments

Comments
 (0)
Please sign in to comment.