From 395d3f2798e4c992635b823f9e305292bb1a1e0f Mon Sep 17 00:00:00 2001 From: Ahn Date: Wed, 26 May 2021 00:16:30 +0200 Subject: [PATCH] fix(cli): keep `testMatch` if old jest config contains it Closes #2616 --- CHANGELOG.md | 2 +- src/cli/__snapshots__/cli.spec.ts.snap | 67 ++++++++++++++++++++++ src/cli/cli.spec.ts | 78 ++++++++++++++++++++------ src/cli/config/migrate.ts | 27 +++++---- 4 files changed, 141 insertions(+), 33 deletions(-) create mode 100644 src/cli/__snapshots__/cli.spec.ts.snap diff --git a/CHANGELOG.md b/CHANGELOG.md index be5fcdcc27..baddfc3653 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -115,7 +115,7 @@ module.exports = { ``` * Remove possibilities to import `mocked`, `createJestPreset`, `pathsToModuleNameMapper` from package entry. One should change to ``` -import { mocked, createJestPreset, pathsToModuleNameMapper` } from 'ts-jest/utils' +import { mocked, createJestPreset, pathsToModuleNameMapper } from 'ts-jest/utils' ``` * **config:** One currently uses `tsConfig` should change to `tsconfig` in your `jest.config.js` or `package.json`. diff --git a/src/cli/__snapshots__/cli.spec.ts.snap b/src/cli/__snapshots__/cli.spec.ts.snap new file mode 100644 index 0000000000..7c084d5607 --- /dev/null +++ b/src/cli/__snapshots__/cli.spec.ts.snap @@ -0,0 +1,67 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`config migrate should migrate preset if valid preset value is used 1`] = ` +" +No migration needed for given Jest configuration + " +`; + +exports[`config migrate should migrate preset if valid preset value is used 2`] = ` +"\\"jest\\": { + \\"preset\\": \\"ts-jest\\" +} +" +`; + +exports[`config migrate should migrate preset if valid preset value is used 3`] = ` +" +No migration needed for given Jest configuration + " +`; + +exports[`config migrate should reset testMatch if testRegex is used 1`] = ` +"\\"jest\\": { + \\"testRegex\\": \\"foo-pattern\\", + \\"preset\\": \\"ts-jest\\" +} +" +`; + +exports[`config migrate should reset testMatch if testRegex is used 2`] = ` +"\\"jest\\": { + \\"testRegex\\": [ + \\"foo-pattern\\" + ], + \\"preset\\": \\"ts-jest\\" +} +" +`; + +exports[`config migrate should reset testMatch if testRegex is used 3`] = ` +"\\"jest\\": { + \\"testRegex\\": [], + \\"testMatch\\": [ + \\"**/__tests__/**/*.(spec|test).[tj]s?(x)\\" + ], + \\"preset\\": \\"ts-jest\\" +} +" +`; + +exports[`config migrate should reset testMatch if testRegex is used 4`] = ` +"\\"jest\\": { + \\"testMatch\\": [ + \\"**/__tests__/**/*.(spec|test).[tj]s?(x)\\" + ], + \\"preset\\": \\"ts-jest\\" +} +" +`; + +exports[`config migrate should reset testMatch if testRegex is used 5`] = ` +"\\"jest\\": { + \\"testRegex\\": \\"foo-pattern\\", + \\"preset\\": \\"ts-jest\\" +} +" +`; diff --git a/src/cli/cli.spec.ts b/src/cli/cli.spec.ts index 8d73ec6ee2..816475a07f 100644 --- a/src/cli/cli.spec.ts +++ b/src/cli/cli.spec.ts @@ -413,27 +413,69 @@ Migrated Jest configuration: `) }) - it('should reset testMatch if testRegex is used', async () => { + test.each([ + { + jest: { + preset: 'ts-jest', + }, + }, + { + jest: { + preset: 'ts-jest/foo', + }, + }, + { + jest: { + preset: 'foo-preset', + }, + }, + ])('should migrate preset if valid preset value is used', async (jestCfg) => { expect.assertions(1) fs.existsSync.mockImplementation(() => true) - jest.mock( - pkgPaths.next, - () => ({ - jest: { - testRegex: 'foo-pattern', - }, - }), - { virtual: true }, - ) + jest.mock(pkgPaths.next, () => jestCfg, { virtual: true }) + const res = await runCli(...noOption, pkgPaths.current) - expect(res.stdout).toMatchInlineSnapshot(` -"\\"jest\\": { - \\"testRegex\\": \\"foo-pattern\\", - \\"preset\\": \\"ts-jest\\", - \\"testMatch\\": null -} -" -`) + + expect(res.stdout ? res.stdout : res.stderr).toMatchSnapshot() + }) + + test.each([ + { + jest: { + testRegex: 'foo-pattern', + testMatch: ['**/__tests__/**/*.(spec|test).[tj]s?(x)'], + }, + }, + { + jest: { + testRegex: ['foo-pattern'], + testMatch: ['**/__tests__/**/*.(spec|test).[tj]s?(x)'], + }, + }, + { + jest: { + testRegex: [], + testMatch: ['**/__tests__/**/*.(spec|test).[tj]s?(x)'], + }, + }, + { + jest: { + testMatch: ['**/__tests__/**/*.(spec|test).[tj]s?(x)'], + }, + }, + { + jest: { + testRegex: 'foo-pattern', + }, + }, + ])('should reset testMatch if testRegex is used', async (jestCfg) => { + expect.assertions(1) + fs.existsSync.mockImplementation(() => true) + jest.mock(pkgPaths.next, () => jestCfg, { virtual: true }) + + const res = await runCli(...noOption, pkgPaths.current) + + expect(res.stdout).toMatchSnapshot() }) it('should detect best preset', async () => { diff --git a/src/cli/config/migrate.ts b/src/cli/config/migrate.ts index 532532c55b..8c99d1f73e 100644 --- a/src/cli/config/migrate.ts +++ b/src/cli/config/migrate.ts @@ -71,19 +71,19 @@ export const run: CliCommand = async (args: Arguments /* , logger: Logger*/) => } } // ensure we are using a preset - presetName = presetName || JestPresetNames.default + presetName = presetName ?? JestPresetNames.default preset = allPresets[presetName] footNotes.push( `Detected preset '${preset.label}' as the best matching preset for your configuration. Visit https://kulshekhar.github.io/ts-jest/user/config/#jest-preset for more information about presets. `, ) - } else if (migratedConfig.preset && migratedConfig.preset.startsWith('ts-jest')) { + } else if (migratedConfig.preset?.startsWith('ts-jest')) { if (args.jestPreset === false) { delete migratedConfig.preset } else { // eslint-disable-next-line @typescript-eslint/no-explicit-any - preset = (allPresets as any)[migratedConfig.preset] || defaults + preset = (allPresets as any)[migratedConfig.preset] ?? defaults } } @@ -91,21 +91,20 @@ Visit https://kulshekhar.github.io/ts-jest/user/config/#jest-preset for more inf if (preset) migratedConfig.preset = preset.name // check the extensions - if (migratedConfig.moduleFileExtensions && migratedConfig.moduleFileExtensions.length && preset) { - const presetValue = dedupSort(preset.value.moduleFileExtensions || []).join('::') + if (migratedConfig.moduleFileExtensions?.length && preset) { + const presetValue = dedupSort(preset.value.moduleFileExtensions ?? []).join('::') const migratedValue = dedupSort(migratedConfig.moduleFileExtensions).join('::') if (presetValue === migratedValue) { delete migratedConfig.moduleFileExtensions } } // there is a testRegex, remove our testMatch - if (migratedConfig.testRegex && preset) { - // eslint-disable-next-line @typescript-eslint/no-explicit-any - migratedConfig.testMatch = null as any + if ((typeof migratedConfig.testRegex === 'string' || migratedConfig.testRegex?.length) && preset) { + delete migratedConfig.testMatch } // check the testMatch - else if (migratedConfig.testMatch && migratedConfig.testMatch.length && preset) { - const presetValue = dedupSort(preset.value.testMatch || []).join('::') + else if (migratedConfig.testMatch?.length && preset) { + const presetValue = dedupSort(preset.value.testMatch ?? []).join('::') const migratedValue = dedupSort(migratedConfig.testMatch).join('::') if (presetValue === migratedValue) { delete migratedConfig.testMatch @@ -190,20 +189,20 @@ function cleanupConfig(config: Config.InitialOptions): void { // eslint-disable-next-line @typescript-eslint/no-explicit-any delete (config as any).globals['ts-jest'] } - if (Object.keys(config.globals).length === 0) { + if (!Object.keys(config.globals).length) { delete config.globals } } - if (config.transform && Object.keys(config.transform).length === 0) { + if (config.transform && !Object.keys(config.transform).length) { delete config.transform } if (config.moduleFileExtensions) { config.moduleFileExtensions = dedupSort(config.moduleFileExtensions) - if (config.moduleFileExtensions.length === 0) delete config.moduleFileExtensions + if (!config.moduleFileExtensions.length) delete config.moduleFileExtensions } if (config.testMatch) { config.testMatch = dedupSort(config.testMatch) - if (config.testMatch.length === 0) delete config.testMatch + if (!config.testMatch.length) delete config.testMatch } if (config.preset === JestPresetNames.default) config.preset = defaults.name }