diff --git a/e2e/presets/__tests__/presets.spec.ts b/e2e/presets/__tests__/presets.spec.ts index 88d62cbc4d..bfe9cac4e8 100644 --- a/e2e/presets/__tests__/presets.spec.ts +++ b/e2e/presets/__tests__/presets.spec.ts @@ -1,12 +1,16 @@ import { - JS_EXT_TO_TREAT_AS_ESM, - TS_EXT_TO_TREAT_AS_ESM, createDefaultPreset, + createDefaultLegacyPreset, createDefaultEsmPreset, + createDefaultEsmLegacyPreset, createJsWithTsPreset, + createJsWithTsLegacyPreset, createJsWithTsEsmPreset, + createJsWithTsEsmLegacyPreset, createJsWithBabelPreset, + createJsWithBabelLegacyPreset, createJsWithBabelEsmPreset, + createJsWithBabelEsmLegacyPreset, } from 'ts-jest' // @ts-expect-error we have this file but we don't have type definition for it import defaultPreset from 'ts-jest/jest-preset' @@ -16,46 +20,17 @@ import extendedJestConfig from '../extended-jest-config' test('presets', () => { expect(defaultPreset).toEqual(createDefaultPreset()) - expect(presets.defaultsLegacy).toEqual({ - transform: { - '^.+\\.tsx?$': ['ts-jest/legacy', {}], - }, - }) + expect(presets.defaultsLegacy).toEqual(createDefaultLegacyPreset()) expect(presets.defaultsESM).toEqual(createDefaultEsmPreset()) - expect(presets.defaultsESMLegacy).toEqual({ - extensionsToTreatAsEsm: [...TS_EXT_TO_TREAT_AS_ESM], - transform: { - '^.+\\.tsx?$': ['ts-jest/legacy', { useESM: true }], - }, - }) + expect(presets.defaultsESMLegacy).toEqual(createDefaultEsmLegacyPreset()) expect(presets.jsWithTs).toEqual(createJsWithTsPreset()) - expect(presets.jsWithTsLegacy).toEqual({ - transform: { - '^.+\\.[tj]sx?$': ['ts-jest/legacy', {}], - }, - }) + expect(presets.jsWithTsLegacy).toEqual(createJsWithTsLegacyPreset()) expect(presets.jsWithTsESM).toEqual(createJsWithTsEsmPreset()) - expect(presets.jsWithTsESMLegacy).toEqual({ - extensionsToTreatAsEsm: [...JS_EXT_TO_TREAT_AS_ESM, ...TS_EXT_TO_TREAT_AS_ESM], - transform: { - '^.+\\.m?[tj]sx?$': ['ts-jest/legacy', { useESM: true }], - }, - }) + expect(presets.jsWithTsESMLegacy).toEqual(createJsWithTsEsmLegacyPreset()) expect(presets.jsWithBabel).toEqual(createJsWithBabelPreset()) - expect(presets.jsWithBabelLegacy).toEqual({ - transform: { - '^.+\\.tsx?$': ['ts-jest/legacy', {}], - '^.+\\.jsx?$': 'babel-jest', - }, - }) + expect(presets.jsWithBabelLegacy).toEqual(createJsWithBabelLegacyPreset()) expect(presets.jsWithBabelESM).toEqual(createJsWithBabelEsmPreset()) - expect(presets.jsWithBabelESMLegacy).toEqual({ - extensionsToTreatAsEsm: [...JS_EXT_TO_TREAT_AS_ESM, ...TS_EXT_TO_TREAT_AS_ESM], - transform: { - '^.+\\.tsx?$': ['ts-jest/legacy', { useESM: true }], - '^.+\\.m?[j]sx?$': 'babel-jest', - }, - }) + expect(presets.jsWithBabelESMLegacy).toEqual(createJsWithBabelEsmLegacyPreset()) }) test('should allow extending jest config without type issues when using create preset util functions', () => { diff --git a/presets/index.js b/presets/index.js index 4c66274c4a..4620b3b476 100644 --- a/presets/index.js +++ b/presets/index.js @@ -1,12 +1,16 @@ -const { JS_EXT_TO_TREAT_AS_ESM, TS_EXT_TO_TREAT_AS_ESM } = require('../dist/constants') const { - createJestPreset, createDefaultPreset, createDefaultEsmPreset, createJsWithTsPreset, createJsWithTsEsmPreset, createJsWithBabelPreset, createJsWithBabelEsmPreset, + createDefaultLegacyPreset, + createDefaultEsmLegacyPreset, + createJsWithTsLegacyPreset, + createJsWithTsEsmLegacyPreset, + createJsWithBabelLegacyPreset, + createJsWithBabelEsmLegacyPreset, } = require('../dist/presets/create-jest-preset') module.exports = { @@ -14,47 +18,36 @@ module.exports = { return createDefaultPreset() }, get defaultsLegacy() { - return createJestPreset(true, false) + return createDefaultLegacyPreset() }, get defaultsESM() { return createDefaultEsmPreset() }, get defaultsESMLegacy() { - return createJestPreset(true, false, { extensionsToTreatAsEsm: TS_EXT_TO_TREAT_AS_ESM }) + return createDefaultEsmLegacyPreset() }, get jsWithTs() { return createJsWithTsPreset() }, get jsWithTsLegacy() { - return createJestPreset(true, true) + return createJsWithTsLegacyPreset() }, get jsWithTsESM() { return createJsWithTsEsmPreset() }, get jsWithTsESMLegacy() { - return createJestPreset(true, true, { - extensionsToTreatAsEsm: [...JS_EXT_TO_TREAT_AS_ESM, ...TS_EXT_TO_TREAT_AS_ESM], - }) + return createJsWithTsEsmLegacyPreset() }, get jsWithBabel() { return createJsWithBabelPreset() }, get jsWithBabelLegacy() { - return createJestPreset(true, false, { - transform: { - '^.+\\.jsx?$': 'babel-jest', - }, - }) + return createJsWithBabelLegacyPreset() }, get jsWithBabelESM() { return createJsWithBabelEsmPreset() }, get jsWithBabelESMLegacy() { - return createJestPreset(true, false, { - extensionsToTreatAsEsm: [...JS_EXT_TO_TREAT_AS_ESM, ...TS_EXT_TO_TREAT_AS_ESM], - transform: { - '^.+\\.m?[j]sx?$': 'babel-jest', - }, - }) + return createJsWithBabelEsmLegacyPreset() }, } diff --git a/src/presets/__snapshots__/create-jest-preset.spec.ts.snap b/src/presets/__snapshots__/create-jest-preset.spec.ts.snap index 8010dbc00c..527a6e53cf 100644 --- a/src/presets/__snapshots__/create-jest-preset.spec.ts.snap +++ b/src/presets/__snapshots__/create-jest-preset.spec.ts.snap @@ -1,6 +1,6 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`create-jest-preset CJS presets createDefaultPreset should return preset config 1`] = ` +exports[`create-jest-preset CJS presets createDefaultLegacyPreset should return preset config 1`] = ` { "transform": { "^.+.tsx?$": [ @@ -13,13 +13,12 @@ exports[`create-jest-preset CJS presets createDefaultPreset should return preset } `; -exports[`create-jest-preset CJS presets createJsWithTsPreset should return preset config 1`] = ` +exports[`create-jest-preset CJS presets createDefaultPreset should return preset config 1`] = ` { "transform": { - "^.+.[tj]sx?$": [ + "^.+.tsx?$": [ "ts-jest", { - "isolatedModules": true, "tsconfig": "tsconfig.spec.json", }, ], @@ -27,7 +26,24 @@ exports[`create-jest-preset CJS presets createJsWithTsPreset should return prese } `; -exports[`create-jest-preset CJS presets createWithBabelPreset should return preset config 1`] = ` +exports[`create-jest-preset CJS presets createJsWithBabelLegacyPreset should return preset config 1`] = ` +{ + "transform": { + "^.+.jsx?$": "babel-jest", + "^.+.tsx?$": [ + "ts-jest/legacy", + { + "babelConfig": { + "babelrc": true, + }, + "tsconfig": "tsconfig.spec.json", + }, + ], + }, +} +`; + +exports[`create-jest-preset CJS presets createJsWithBabelPreset should return preset config 1`] = ` { "transform": { "^.+.jsx?$": "babel-jest", @@ -44,6 +60,54 @@ exports[`create-jest-preset CJS presets createWithBabelPreset should return pres } `; +exports[`create-jest-preset CJS presets createJsWithTsLegacyPreset should return preset config 1`] = ` +{ + "transform": { + "^.+.[tj]sx?$": [ + "ts-jest/legacy", + { + "isolatedModules": true, + "tsconfig": "tsconfig.spec.json", + }, + ], + }, +} +`; + +exports[`create-jest-preset CJS presets createJsWithTsPreset should return preset config 1`] = ` +{ + "transform": { + "^.+.[tj]sx?$": [ + "ts-jest", + { + "isolatedModules": true, + "tsconfig": "tsconfig.spec.json", + }, + ], + }, +} +`; + +exports[`create-jest-preset ESM presets createDefaultEsmLegacyPreset should return preset config 1`] = ` +{ + "extensionsToTreatAsEsm": [ + ".jsx", + ".ts", + ".tsx", + ".mts", + ], + "transform": { + "^.+\\.m?tsx?$": [ + "ts-jest/legacy", + { + "tsconfig": "tsconfig.spec.json", + "useESM": true, + }, + ], + }, +} +`; + exports[`create-jest-preset ESM presets createDefaultEsmPreset should return preset config 1`] = ` { "extensionsToTreatAsEsm": [ @@ -64,7 +128,7 @@ exports[`create-jest-preset ESM presets createDefaultEsmPreset should return pre } `; -exports[`create-jest-preset ESM presets createJsWithTsEsmPreset should return preset config 1`] = ` +exports[`create-jest-preset ESM presets createJsWithBabelEsmLegacyPreset should return preset config 1`] = ` { "extensionsToTreatAsEsm": [ ".jsx", @@ -73,10 +137,13 @@ exports[`create-jest-preset ESM presets createJsWithTsEsmPreset should return pr ".mts", ], "transform": { - "^.+\\.m?[tj]sx?$": [ - "ts-jest", + "^.+\\.m?jsx?$": "babel-jest", + "^.+\\.m?tsx?$": [ + "ts-jest/legacy", { - "isolatedModules": true, + "babelConfig": { + "babelrc": true, + }, "tsconfig": "tsconfig.spec.json", "useESM": true, }, @@ -85,7 +152,7 @@ exports[`create-jest-preset ESM presets createJsWithTsEsmPreset should return pr } `; -exports[`create-jest-preset ESM presets createWithBabelEsmPreset should return preset config 1`] = ` +exports[`create-jest-preset ESM presets createJsWithBabelEsmPreset should return preset config 1`] = ` { "extensionsToTreatAsEsm": [ ".jsx", @@ -109,6 +176,48 @@ exports[`create-jest-preset ESM presets createWithBabelEsmPreset should return p } `; +exports[`create-jest-preset ESM presets createJsWithTsEsmLegacyPreset should return preset config 1`] = ` +{ + "extensionsToTreatAsEsm": [ + ".jsx", + ".ts", + ".tsx", + ".mts", + ], + "transform": { + "^.+\\.m?[tj]sx?$": [ + "ts-jest/legacy", + { + "isolatedModules": true, + "tsconfig": "tsconfig.spec.json", + "useESM": true, + }, + ], + }, +} +`; + +exports[`create-jest-preset ESM presets createJsWithTsEsmPreset should return preset config 1`] = ` +{ + "extensionsToTreatAsEsm": [ + ".jsx", + ".ts", + ".tsx", + ".mts", + ], + "transform": { + "^.+\\.m?[tj]sx?$": [ + "ts-jest", + { + "isolatedModules": true, + "tsconfig": "tsconfig.spec.json", + "useESM": true, + }, + ], + }, +} +`; + exports[`create-jest-preset createJestPreset should return correct preset 1`] = ` { "transform": { diff --git a/src/presets/create-jest-preset.spec.ts b/src/presets/create-jest-preset.spec.ts index 96af505dbc..85fe051b0c 100644 --- a/src/presets/create-jest-preset.spec.ts +++ b/src/presets/create-jest-preset.spec.ts @@ -8,6 +8,11 @@ import { createDefaultEsmPreset, createJsWithTsEsmPreset, createJsWithBabelEsmPreset, + createJsWithTsLegacyPreset, + createJsWithBabelLegacyPreset, + createDefaultEsmLegacyPreset, + createJsWithTsEsmLegacyPreset, + createJsWithBabelEsmLegacyPreset, } from './create-jest-preset' describe('create-jest-preset', () => { @@ -73,6 +78,16 @@ describe('create-jest-preset', () => { }) }) + describe('createDefaultLegacyPreset', () => { + it('should return preset config', () => { + expect( + createDefaultPreset({ + tsconfig: 'tsconfig.spec.json', + }), + ).toMatchSnapshot() + }) + }) + describe('createJsWithTsPreset', () => { it('should return preset config', () => { expect( @@ -84,7 +99,18 @@ describe('create-jest-preset', () => { }) }) - describe('createWithBabelPreset', () => { + describe('createJsWithTsLegacyPreset', () => { + it('should return preset config', () => { + expect( + createJsWithTsLegacyPreset({ + tsconfig: 'tsconfig.spec.json', + isolatedModules: true, + }), + ).toMatchSnapshot() + }) + }) + + describe('createJsWithBabelPreset', () => { it('should return preset config', () => { expect( createJsWithBabelPreset({ @@ -96,6 +122,19 @@ describe('create-jest-preset', () => { ).toMatchSnapshot() }) }) + + describe('createJsWithBabelLegacyPreset', () => { + it('should return preset config', () => { + expect( + createJsWithBabelLegacyPreset({ + tsconfig: 'tsconfig.spec.json', + babelConfig: { + babelrc: true, + }, + }), + ).toMatchSnapshot() + }) + }) }) describe('ESM presets', () => { @@ -109,6 +148,16 @@ describe('create-jest-preset', () => { }) }) + describe('createDefaultEsmLegacyPreset', () => { + it('should return preset config', () => { + expect( + createDefaultEsmLegacyPreset({ + tsconfig: 'tsconfig.spec.json', + }), + ).toMatchSnapshot() + }) + }) + describe('createJsWithTsEsmPreset', () => { it('should return preset config', () => { expect( @@ -120,7 +169,18 @@ describe('create-jest-preset', () => { }) }) - describe('createWithBabelEsmPreset', () => { + describe('createJsWithTsEsmLegacyPreset', () => { + it('should return preset config', () => { + expect( + createJsWithTsEsmLegacyPreset({ + tsconfig: 'tsconfig.spec.json', + isolatedModules: true, + }), + ).toMatchSnapshot() + }) + }) + + describe('createJsWithBabelEsmPreset', () => { it('should return preset config', () => { expect( createJsWithBabelEsmPreset({ @@ -132,5 +192,18 @@ describe('create-jest-preset', () => { ).toMatchSnapshot() }) }) + + describe('createJsWithBabelEsmLegacyPreset', () => { + it('should return preset config', () => { + expect( + createJsWithBabelEsmLegacyPreset({ + tsconfig: 'tsconfig.spec.json', + babelConfig: { + babelrc: true, + }, + }), + ).toMatchSnapshot() + }) + }) }) }) diff --git a/src/presets/create-jest-preset.ts b/src/presets/create-jest-preset.ts index 0551d73515..f0a5cd755a 100644 --- a/src/presets/create-jest-preset.ts +++ b/src/presets/create-jest-preset.ts @@ -10,10 +10,25 @@ import { ESM_TS_JS_TRANSFORM_PATTERN, ESM_JS_TRANSFORM_PATTERN, } from '../constants' -import type { +import { + DefaultEsmLegacyPreset, + DefaultEsmPreset, + DefaultEsmTransformOptions, + DefaultLegacyPreset, DefaultPreset, + DefaultTransformOptions, + JsWithBabelEsmLegacyPreset, + JsWithBabelEsmPreset, + JsWithBabelEsmTransformOptions, + JsWithBabelLegacyPreset, JsWithBabelPreset, + JsWithBabelTransformerOptions, + JsWithTsEsmLegacyPreset, + JsWithTsEsmPreset, + JsWithTsEsmTransformOptions, + JsWithTsLegacyPreset, JsWithTsPreset, + JsWithTsTransformOptions, TsJestPresets, TsJestTransformerOptions, } from '../types' @@ -48,9 +63,7 @@ export function createJestPreset( } } -export function createDefaultPreset( - tsJestTransformOptions: Omit = {}, -): DefaultPreset { +export function createDefaultPreset(tsJestTransformOptions: DefaultTransformOptions = {}): DefaultPreset { logger.debug('creating default CJS Jest preset') return { @@ -60,9 +73,17 @@ export function createDefaultPreset( } } -export function createJsWithTsPreset( - tsJestTransformOptions: Omit = {}, -): JsWithTsPreset { +export function createDefaultLegacyPreset(tsJestTransformOptions: DefaultTransformOptions = {}): DefaultLegacyPreset { + logger.debug('creating default CJS Jest preset') + + return { + transform: { + [TS_TRANSFORM_PATTERN]: ['ts-jest/legacy', tsJestTransformOptions], + }, + } +} + +export function createJsWithTsPreset(tsJestTransformOptions: JsWithTsTransformOptions = {}): JsWithTsPreset { logger.debug('creating Js with Ts CJS Jest preset') return { @@ -72,9 +93,19 @@ export function createJsWithTsPreset( } } -export function createJsWithBabelPreset( - tsJestTransformOptions: Omit = {}, -): JsWithBabelPreset { +export function createJsWithTsLegacyPreset( + tsJestTransformOptions: JsWithTsTransformOptions = {}, +): JsWithTsLegacyPreset { + logger.debug('creating Js with Ts CJS Jest preset') + + return { + transform: { + [TS_JS_TRANSFORM_PATTERN]: ['ts-jest/legacy', tsJestTransformOptions], + }, + } +} + +export function createJsWithBabelPreset(tsJestTransformOptions: JsWithBabelTransformerOptions = {}): JsWithBabelPreset { logger.debug('creating JS with Babel CJS Jest preset') return { @@ -85,12 +116,20 @@ export function createJsWithBabelPreset( } } -export function createDefaultEsmPreset(tsJestTransformOptions: Omit = {}): { - extensionsToTreatAsEsm: string[] - transform: { - [ESM_TS_TRANSFORM_PATTERN]: ['ts-jest', { useESM: true } & typeof tsJestTransformOptions] +export function createJsWithBabelLegacyPreset( + tsJestTransformOptions: JsWithBabelTransformerOptions = {}, +): JsWithBabelLegacyPreset { + logger.debug('creating JS with Babel CJS Jest preset') + + return { + transform: { + [JS_TRANSFORM_PATTERN]: 'babel-jest', + [TS_TRANSFORM_PATTERN]: ['ts-jest/legacy', tsJestTransformOptions], + }, } -} { +} + +export function createDefaultEsmPreset(tsJestTransformOptions: DefaultEsmTransformOptions = {}): DefaultEsmPreset { logger.debug('creating default ESM Jest preset') return { @@ -107,12 +146,26 @@ export function createDefaultEsmPreset(tsJestTransformOptions: Omit = {}): { - extensionsToTreatAsEsm: string[] - transform: { - [ESM_TS_JS_TRANSFORM_PATTERN]: ['ts-jest', { useESM: true } & typeof tsJestTransformOptions] +export function createDefaultEsmLegacyPreset( + tsJestTransformOptions: DefaultEsmTransformOptions = {}, +): DefaultEsmLegacyPreset { + logger.debug('creating default ESM Jest preset') + + return { + extensionsToTreatAsEsm: [...JS_EXT_TO_TREAT_AS_ESM, ...TS_EXT_TO_TREAT_AS_ESM], + transform: { + [ESM_TS_TRANSFORM_PATTERN]: [ + 'ts-jest/legacy', + { + ...tsJestTransformOptions, + useESM: true, + }, + ], + }, } -} { +} + +export function createJsWithTsEsmPreset(tsJestTransformOptions: JsWithTsEsmTransformOptions = {}): JsWithTsEsmPreset { logger.debug('creating Js with Ts ESM Jest preset') return { @@ -129,13 +182,28 @@ export function createJsWithTsEsmPreset(tsJestTransformOptions: Omit = {}): { - extensionsToTreatAsEsm: string[] - transform: { - [ESM_JS_TRANSFORM_PATTERN]: 'babel-jest' - [ESM_TS_TRANSFORM_PATTERN]: ['ts-jest', { useESM: true } & typeof tsJestTransformOptions] +export function createJsWithTsEsmLegacyPreset( + tsJestTransformOptions: JsWithTsEsmTransformOptions = {}, +): JsWithTsEsmLegacyPreset { + logger.debug('creating Js with Ts ESM Jest preset') + + return { + extensionsToTreatAsEsm: [...JS_EXT_TO_TREAT_AS_ESM, ...TS_EXT_TO_TREAT_AS_ESM], + transform: { + [ESM_TS_JS_TRANSFORM_PATTERN]: [ + 'ts-jest/legacy', + { + ...tsJestTransformOptions, + useESM: true, + }, + ], + }, } -} { +} + +export function createJsWithBabelEsmPreset( + tsJestTransformOptions: JsWithBabelEsmTransformOptions = {}, +): JsWithBabelEsmPreset { logger.debug('creating JS with Babel ESM Jest preset') return { @@ -152,3 +220,23 @@ export function createJsWithBabelEsmPreset(tsJestTransformOptions: Omit export type DefaultPreset = { transform: { - [TS_TRANSFORM_PATTERN]: ['ts-jest', Omit] + [TS_TRANSFORM_PATTERN]: ['ts-jest', DefaultTransformOptions] } } +export type DefaultLegacyPreset = { + transform: { + [TS_TRANSFORM_PATTERN]: ['ts-jest/legacy', DefaultTransformOptions] + } +} + +export type DefaultEsmTransformOptions = Omit +export type DefaultEsmPreset = { + extensionsToTreatAsEsm: string[] + transform: { + [ESM_TS_TRANSFORM_PATTERN]: ['ts-jest', { useESM: true } & DefaultEsmTransformOptions] + } +} +export type DefaultEsmLegacyPreset = { + extensionsToTreatAsEsm: string[] + transform: { + [ESM_TS_TRANSFORM_PATTERN]: ['ts-jest/legacy', { useESM: true } & DefaultEsmTransformOptions] + } +} + +export type JsWithTsTransformOptions = Omit export type JsWithTsPreset = { transform: { - [TS_JS_TRANSFORM_PATTERN]: ['ts-jest', Omit] + [TS_JS_TRANSFORM_PATTERN]: ['ts-jest', JsWithTsTransformOptions] + } +} +export type JsWithTsLegacyPreset = { + transform: { + [TS_JS_TRANSFORM_PATTERN]: ['ts-jest/legacy', JsWithTsTransformOptions] } } + +export type JsWithTsEsmTransformOptions = Omit +export type JsWithTsEsmPreset = { + extensionsToTreatAsEsm: string[] + transform: { + [ESM_TS_JS_TRANSFORM_PATTERN]: ['ts-jest', { useESM: true } & JsWithTsEsmTransformOptions] + } +} +export type JsWithTsEsmLegacyPreset = { + extensionsToTreatAsEsm: string[] + transform: { + [ESM_TS_JS_TRANSFORM_PATTERN]: ['ts-jest/legacy', { useESM: true } & JsWithTsEsmTransformOptions] + } +} + +export type JsWithBabelTransformerOptions = Omit export type JsWithBabelPreset = { transform: { [JS_TRANSFORM_PATTERN]: 'babel-jest' - [TS_TRANSFORM_PATTERN]: ['ts-jest', Omit] + [TS_TRANSFORM_PATTERN]: ['ts-jest', JsWithBabelTransformerOptions] + } +} +export type JsWithBabelLegacyPreset = { + transform: { + [JS_TRANSFORM_PATTERN]: 'babel-jest' + [TS_TRANSFORM_PATTERN]: ['ts-jest/legacy', JsWithBabelTransformerOptions] + } +} + +export type JsWithBabelEsmTransformOptions = Omit +export type JsWithBabelEsmPreset = { + extensionsToTreatAsEsm: string[] + transform: { + [ESM_JS_TRANSFORM_PATTERN]: 'babel-jest' + [ESM_TS_TRANSFORM_PATTERN]: ['ts-jest', { useESM: true } & JsWithBabelEsmTransformOptions] + } +} +export type JsWithBabelEsmLegacyPreset = { + extensionsToTreatAsEsm: string[] + transform: { + [ESM_JS_TRANSFORM_PATTERN]: 'babel-jest' + [ESM_TS_TRANSFORM_PATTERN]: ['ts-jest/legacy', { useESM: true } & JsWithBabelEsmTransformOptions] } }