From 5251354d2c1068acdb198578a56b2e0a81b17840 Mon Sep 17 00:00:00 2001 From: ahnpnl Date: Fri, 5 Jul 2024 17:00:17 +0200 Subject: [PATCH] refactor: `JsWithBabelPreset` not compat with original `transform` type --- e2e/presets/__tests__/presets.spec.ts | 6 +++ e2e/presets/extended-jest-config.ts | 48 +++++++++++++++++++ src/cli/__snapshots__/cli.spec.ts.snap | 12 ++--- .../create-jest-preset.spec.ts.snap | 18 ++----- src/presets/create-jest-preset.ts | 20 ++------ src/types.ts | 2 +- 6 files changed, 68 insertions(+), 38 deletions(-) create mode 100644 e2e/presets/extended-jest-config.ts diff --git a/e2e/presets/__tests__/presets.spec.ts b/e2e/presets/__tests__/presets.spec.ts index 392589bf21..e0e2905fd9 100644 --- a/e2e/presets/__tests__/presets.spec.ts +++ b/e2e/presets/__tests__/presets.spec.ts @@ -1,6 +1,8 @@ import { JS_EXT_TO_TREAT_AS_ESM, TS_EXT_TO_TREAT_AS_ESM } from 'ts-jest' import presets from 'ts-jest/presets' +import extendedJestConfig from '../extended-jest-config' + const defaultPreset = require('ts-jest/jest-preset') test('presets', () => { @@ -75,3 +77,7 @@ test('presets', () => { }, }) }) + +test('should allow extending jest config without type issues when using create preset util functions', () => { + expect(extendedJestConfig).toBeTruthy() +}) diff --git a/e2e/presets/extended-jest-config.ts b/e2e/presets/extended-jest-config.ts new file mode 100644 index 0000000000..1e22b8fe83 --- /dev/null +++ b/e2e/presets/extended-jest-config.ts @@ -0,0 +1,48 @@ +import { + createDefaultPreset, + createDefaultEsmPreset, + createJsWithTsPreset, + createJsWithTsEsmPreset, + createJsWithBabelPreset, + createJsWithBabelEsmPreset, + type JestConfigWithTsJest, +} from 'ts-jest' + +const defaultPreset: JestConfigWithTsJest = { + ...createDefaultPreset(), + testEnvironment: 'jsdom', +} + +const defaultEsmPreset: JestConfigWithTsJest = { + ...createDefaultEsmPreset(), + testEnvironment: 'jsdom', +} + +const jsWithTsPreset: JestConfigWithTsJest = { + ...createJsWithTsPreset(), + testEnvironment: 'jsdom', +} + +const jsWithTsEsmPreset: JestConfigWithTsJest = { + ...createJsWithTsEsmPreset(), + testEnvironment: 'jsdom', +} + +const jsWithBabelPreset: JestConfigWithTsJest = { + ...createJsWithBabelPreset(), + testEnvironment: 'jsdom', +} + +const jsWithBabelEsmPreset: JestConfigWithTsJest = { + ...createJsWithBabelEsmPreset(), + testEnvironment: 'jsdom', +} + +export default { + defaultPreset, + defaultEsmPreset, + jsWithTsPreset, + jsWithTsEsmPreset, + jsWithBabelPreset, + jsWithBabelEsmPreset, +} diff --git a/src/cli/__snapshots__/cli.spec.ts.snap b/src/cli/__snapshots__/cli.spec.ts.snap index 5bb669e967..e4454982bc 100644 --- a/src/cli/__snapshots__/cli.spec.ts.snap +++ b/src/cli/__snapshots__/cli.spec.ts.snap @@ -25,7 +25,7 @@ exports[`config init should create a jest config file with cli options for confi module.exports = { testEnvironment: "jsdom", transform: { - "^.+.jsx?$": ["babel-jest",{}], + "^.+.jsx?$": "babel-jest", }, };" `; @@ -35,7 +35,7 @@ exports[`config init should create a jest config file with cli options for confi export default { testEnvironment: "jsdom", transform: { - "^.+.jsx?$": ["babel-jest",{}], + "^.+.jsx?$": "babel-jest", }, };" `; @@ -81,15 +81,11 @@ exports[`config init should update package.json for config type js-with-babel-fu "version": "0.0.0-mock.0", "jest": { "transform": { - "^.+.jsx?$": [ - "babel-jest", - {} - ], + "^.+.jsx?$": "babel-jest", "^.+.tsx?$": [ "ts-jest", { - "tsconfig": "tsconfig.test.json", - "babelConfig": true + "tsconfig": "tsconfig.test.json" } ] } diff --git a/src/presets/__snapshots__/create-jest-preset.spec.ts.snap b/src/presets/__snapshots__/create-jest-preset.spec.ts.snap index 8d9d014e00..8010dbc00c 100644 --- a/src/presets/__snapshots__/create-jest-preset.spec.ts.snap +++ b/src/presets/__snapshots__/create-jest-preset.spec.ts.snap @@ -30,16 +30,13 @@ exports[`create-jest-preset CJS presets createJsWithTsPreset should return prese exports[`create-jest-preset CJS presets createWithBabelPreset should return preset config 1`] = ` { "transform": { - "^.+.jsx?$": [ - "babel-jest", - { - "babelrc": true, - }, - ], + "^.+.jsx?$": "babel-jest", "^.+.tsx?$": [ "ts-jest", { - "babelConfig": true, + "babelConfig": { + "babelrc": true, + }, "tsconfig": "tsconfig.spec.json", }, ], @@ -97,12 +94,7 @@ exports[`create-jest-preset ESM presets createWithBabelEsmPreset should return p ".mts", ], "transform": { - "^.+\\.m?jsx?$": [ - "babel-jest", - { - "babelrc": true, - }, - ], + "^.+\\.m?jsx?$": "babel-jest", "^.+\\.m?tsx?$": [ "ts-jest", { diff --git a/src/presets/create-jest-preset.ts b/src/presets/create-jest-preset.ts index dd213a26d5..0551d73515 100644 --- a/src/presets/create-jest-preset.ts +++ b/src/presets/create-jest-preset.ts @@ -11,7 +11,6 @@ import { ESM_JS_TRANSFORM_PATTERN, } from '../constants' import type { - BabelConfig, DefaultPreset, JsWithBabelPreset, JsWithTsPreset, @@ -78,18 +77,10 @@ export function createJsWithBabelPreset( ): JsWithBabelPreset { logger.debug('creating JS with Babel CJS Jest preset') - const babelConfig = tsJestTransformOptions.babelConfig - return { transform: { - [JS_TRANSFORM_PATTERN]: ['babel-jest', typeof babelConfig === 'object' ? babelConfig : {}], - [TS_TRANSFORM_PATTERN]: [ - 'ts-jest', - { - ...tsJestTransformOptions, - babelConfig: true, - }, - ], + [JS_TRANSFORM_PATTERN]: 'babel-jest', + [TS_TRANSFORM_PATTERN]: ['ts-jest', tsJestTransformOptions], }, } } @@ -141,24 +132,21 @@ export function createJsWithTsEsmPreset(tsJestTransformOptions: Omit = {}): { extensionsToTreatAsEsm: string[] transform: { - [ESM_JS_TRANSFORM_PATTERN]: ['babel-jest', babelConfig: BabelConfig] + [ESM_JS_TRANSFORM_PATTERN]: 'babel-jest' [ESM_TS_TRANSFORM_PATTERN]: ['ts-jest', { useESM: true } & typeof tsJestTransformOptions] } } { logger.debug('creating JS with Babel ESM Jest preset') - const babelConfig = tsJestTransformOptions.babelConfig - return { extensionsToTreatAsEsm: [...JS_EXT_TO_TREAT_AS_ESM, ...TS_EXT_TO_TREAT_AS_ESM], transform: { - [ESM_JS_TRANSFORM_PATTERN]: ['babel-jest', typeof babelConfig === 'object' ? babelConfig : {}], + [ESM_JS_TRANSFORM_PATTERN]: 'babel-jest', [ESM_TS_TRANSFORM_PATTERN]: [ 'ts-jest', { ...tsJestTransformOptions, useESM: true, - babelConfig, }, ], }, diff --git a/src/types.ts b/src/types.ts index 374726d884..721ff6d9a5 100644 --- a/src/types.ts +++ b/src/types.ts @@ -250,7 +250,7 @@ export type JsWithTsPreset = { } export type JsWithBabelPreset = { transform: { - [JS_TRANSFORM_PATTERN]: ['babel-jest', babelConfig: BabelConfig] + [JS_TRANSFORM_PATTERN]: 'babel-jest' [TS_TRANSFORM_PATTERN]: ['ts-jest', Omit] } }