Skip to content

Commit 06f78ed

Browse files
committedJul 7, 2024·
feat(presets): add util functions to create ESM presets
1 parent 922d6d0 commit 06f78ed

File tree

6 files changed

+408
-98
lines changed

6 files changed

+408
-98
lines changed
 

‎e2e/presets/__tests__/presets.spec.ts

+12-37
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
import {
2-
JS_EXT_TO_TREAT_AS_ESM,
3-
TS_EXT_TO_TREAT_AS_ESM,
42
createDefaultPreset,
3+
createDefaultLegacyPreset,
54
createDefaultEsmPreset,
5+
createDefaultEsmLegacyPreset,
66
createJsWithTsPreset,
7+
createJsWithTsLegacyPreset,
78
createJsWithTsEsmPreset,
9+
createJsWithTsEsmLegacyPreset,
810
createJsWithBabelPreset,
11+
createJsWithBabelLegacyPreset,
912
createJsWithBabelEsmPreset,
13+
createJsWithBabelEsmLegacyPreset,
1014
} from 'ts-jest'
1115
// @ts-expect-error we have this file but we don't have type definition for it
1216
import defaultPreset from 'ts-jest/jest-preset'
@@ -16,46 +20,17 @@ import extendedJestConfig from '../extended-jest-config'
1620

1721
test('presets', () => {
1822
expect(defaultPreset).toEqual(createDefaultPreset())
19-
expect(presets.defaultsLegacy).toEqual({
20-
transform: {
21-
'^.+\\.tsx?$': ['ts-jest/legacy', {}],
22-
},
23-
})
23+
expect(presets.defaultsLegacy).toEqual(createDefaultLegacyPreset())
2424
expect(presets.defaultsESM).toEqual(createDefaultEsmPreset())
25-
expect(presets.defaultsESMLegacy).toEqual({
26-
extensionsToTreatAsEsm: [...TS_EXT_TO_TREAT_AS_ESM],
27-
transform: {
28-
'^.+\\.tsx?$': ['ts-jest/legacy', { useESM: true }],
29-
},
30-
})
25+
expect(presets.defaultsESMLegacy).toEqual(createDefaultEsmLegacyPreset())
3126
expect(presets.jsWithTs).toEqual(createJsWithTsPreset())
32-
expect(presets.jsWithTsLegacy).toEqual({
33-
transform: {
34-
'^.+\\.[tj]sx?$': ['ts-jest/legacy', {}],
35-
},
36-
})
27+
expect(presets.jsWithTsLegacy).toEqual(createJsWithTsLegacyPreset())
3728
expect(presets.jsWithTsESM).toEqual(createJsWithTsEsmPreset())
38-
expect(presets.jsWithTsESMLegacy).toEqual({
39-
extensionsToTreatAsEsm: [...JS_EXT_TO_TREAT_AS_ESM, ...TS_EXT_TO_TREAT_AS_ESM],
40-
transform: {
41-
'^.+\\.m?[tj]sx?$': ['ts-jest/legacy', { useESM: true }],
42-
},
43-
})
29+
expect(presets.jsWithTsESMLegacy).toEqual(createJsWithTsEsmLegacyPreset())
4430
expect(presets.jsWithBabel).toEqual(createJsWithBabelPreset())
45-
expect(presets.jsWithBabelLegacy).toEqual({
46-
transform: {
47-
'^.+\\.tsx?$': ['ts-jest/legacy', {}],
48-
'^.+\\.jsx?$': 'babel-jest',
49-
},
50-
})
31+
expect(presets.jsWithBabelLegacy).toEqual(createJsWithBabelLegacyPreset())
5132
expect(presets.jsWithBabelESM).toEqual(createJsWithBabelEsmPreset())
52-
expect(presets.jsWithBabelESMLegacy).toEqual({
53-
extensionsToTreatAsEsm: [...JS_EXT_TO_TREAT_AS_ESM, ...TS_EXT_TO_TREAT_AS_ESM],
54-
transform: {
55-
'^.+\\.tsx?$': ['ts-jest/legacy', { useESM: true }],
56-
'^.+\\.m?[j]sx?$': 'babel-jest',
57-
},
58-
})
33+
expect(presets.jsWithBabelESMLegacy).toEqual(createJsWithBabelEsmLegacyPreset())
5934
})
6035

6136
test('should allow extending jest config without type issues when using create preset util functions', () => {

‎presets/index.js

+12-19
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,53 @@
1-
const { JS_EXT_TO_TREAT_AS_ESM, TS_EXT_TO_TREAT_AS_ESM } = require('../dist/constants')
21
const {
3-
createJestPreset,
42
createDefaultPreset,
53
createDefaultEsmPreset,
64
createJsWithTsPreset,
75
createJsWithTsEsmPreset,
86
createJsWithBabelPreset,
97
createJsWithBabelEsmPreset,
8+
createDefaultLegacyPreset,
9+
createDefaultEsmLegacyPreset,
10+
createJsWithTsLegacyPreset,
11+
createJsWithTsEsmLegacyPreset,
12+
createJsWithBabelLegacyPreset,
13+
createJsWithBabelEsmLegacyPreset,
1014
} = require('../dist/presets/create-jest-preset')
1115

1216
module.exports = {
1317
get defaults() {
1418
return createDefaultPreset()
1519
},
1620
get defaultsLegacy() {
17-
return createJestPreset(true, false)
21+
return createDefaultLegacyPreset()
1822
},
1923
get defaultsESM() {
2024
return createDefaultEsmPreset()
2125
},
2226
get defaultsESMLegacy() {
23-
return createJestPreset(true, false, { extensionsToTreatAsEsm: TS_EXT_TO_TREAT_AS_ESM })
27+
return createDefaultEsmLegacyPreset()
2428
},
2529
get jsWithTs() {
2630
return createJsWithTsPreset()
2731
},
2832
get jsWithTsLegacy() {
29-
return createJestPreset(true, true)
33+
return createJsWithTsLegacyPreset()
3034
},
3135
get jsWithTsESM() {
3236
return createJsWithTsEsmPreset()
3337
},
3438
get jsWithTsESMLegacy() {
35-
return createJestPreset(true, true, {
36-
extensionsToTreatAsEsm: [...JS_EXT_TO_TREAT_AS_ESM, ...TS_EXT_TO_TREAT_AS_ESM],
37-
})
39+
return createJsWithTsEsmLegacyPreset()
3840
},
3941
get jsWithBabel() {
4042
return createJsWithBabelPreset()
4143
},
4244
get jsWithBabelLegacy() {
43-
return createJestPreset(true, false, {
44-
transform: {
45-
'^.+\\.jsx?$': 'babel-jest',
46-
},
47-
})
45+
return createJsWithBabelLegacyPreset()
4846
},
4947
get jsWithBabelESM() {
5048
return createJsWithBabelEsmPreset()
5149
},
5250
get jsWithBabelESMLegacy() {
53-
return createJestPreset(true, false, {
54-
extensionsToTreatAsEsm: [...JS_EXT_TO_TREAT_AS_ESM, ...TS_EXT_TO_TREAT_AS_ESM],
55-
transform: {
56-
'^.+\\.m?[j]sx?$': 'babel-jest',
57-
},
58-
})
51+
return createJsWithBabelEsmLegacyPreset()
5952
},
6053
}

‎src/presets/__snapshots__/create-jest-preset.spec.ts.snap

+119-10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3-
exports[`create-jest-preset CJS presets createDefaultPreset should return preset config 1`] = `
3+
exports[`create-jest-preset CJS presets createDefaultLegacyPreset should return preset config 1`] = `
44
{
55
"transform": {
66
"^.+.tsx?$": [
@@ -13,21 +13,37 @@ exports[`create-jest-preset CJS presets createDefaultPreset should return preset
1313
}
1414
`;
1515

16-
exports[`create-jest-preset CJS presets createJsWithTsPreset should return preset config 1`] = `
16+
exports[`create-jest-preset CJS presets createDefaultPreset should return preset config 1`] = `
1717
{
1818
"transform": {
19-
"^.+.[tj]sx?$": [
19+
"^.+.tsx?$": [
2020
"ts-jest",
2121
{
22-
"isolatedModules": true,
2322
"tsconfig": "tsconfig.spec.json",
2423
},
2524
],
2625
},
2726
}
2827
`;
2928

30-
exports[`create-jest-preset CJS presets createWithBabelPreset should return preset config 1`] = `
29+
exports[`create-jest-preset CJS presets createJsWithBabelLegacyPreset should return preset config 1`] = `
30+
{
31+
"transform": {
32+
"^.+.jsx?$": "babel-jest",
33+
"^.+.tsx?$": [
34+
"ts-jest/legacy",
35+
{
36+
"babelConfig": {
37+
"babelrc": true,
38+
},
39+
"tsconfig": "tsconfig.spec.json",
40+
},
41+
],
42+
},
43+
}
44+
`;
45+
46+
exports[`create-jest-preset CJS presets createJsWithBabelPreset should return preset config 1`] = `
3147
{
3248
"transform": {
3349
"^.+.jsx?$": "babel-jest",
@@ -44,6 +60,54 @@ exports[`create-jest-preset CJS presets createWithBabelPreset should return pres
4460
}
4561
`;
4662

63+
exports[`create-jest-preset CJS presets createJsWithTsLegacyPreset should return preset config 1`] = `
64+
{
65+
"transform": {
66+
"^.+.[tj]sx?$": [
67+
"ts-jest/legacy",
68+
{
69+
"isolatedModules": true,
70+
"tsconfig": "tsconfig.spec.json",
71+
},
72+
],
73+
},
74+
}
75+
`;
76+
77+
exports[`create-jest-preset CJS presets createJsWithTsPreset should return preset config 1`] = `
78+
{
79+
"transform": {
80+
"^.+.[tj]sx?$": [
81+
"ts-jest",
82+
{
83+
"isolatedModules": true,
84+
"tsconfig": "tsconfig.spec.json",
85+
},
86+
],
87+
},
88+
}
89+
`;
90+
91+
exports[`create-jest-preset ESM presets createDefaultEsmLegacyPreset should return preset config 1`] = `
92+
{
93+
"extensionsToTreatAsEsm": [
94+
".jsx",
95+
".ts",
96+
".tsx",
97+
".mts",
98+
],
99+
"transform": {
100+
"^.+\\.m?tsx?$": [
101+
"ts-jest/legacy",
102+
{
103+
"tsconfig": "tsconfig.spec.json",
104+
"useESM": true,
105+
},
106+
],
107+
},
108+
}
109+
`;
110+
47111
exports[`create-jest-preset ESM presets createDefaultEsmPreset should return preset config 1`] = `
48112
{
49113
"extensionsToTreatAsEsm": [
@@ -64,7 +128,7 @@ exports[`create-jest-preset ESM presets createDefaultEsmPreset should return pre
64128
}
65129
`;
66130

67-
exports[`create-jest-preset ESM presets createJsWithTsEsmPreset should return preset config 1`] = `
131+
exports[`create-jest-preset ESM presets createJsWithBabelEsmLegacyPreset should return preset config 1`] = `
68132
{
69133
"extensionsToTreatAsEsm": [
70134
".jsx",
@@ -73,10 +137,13 @@ exports[`create-jest-preset ESM presets createJsWithTsEsmPreset should return pr
73137
".mts",
74138
],
75139
"transform": {
76-
"^.+\\.m?[tj]sx?$": [
77-
"ts-jest",
140+
"^.+\\.m?jsx?$": "babel-jest",
141+
"^.+\\.m?tsx?$": [
142+
"ts-jest/legacy",
78143
{
79-
"isolatedModules": true,
144+
"babelConfig": {
145+
"babelrc": true,
146+
},
80147
"tsconfig": "tsconfig.spec.json",
81148
"useESM": true,
82149
},
@@ -85,7 +152,7 @@ exports[`create-jest-preset ESM presets createJsWithTsEsmPreset should return pr
85152
}
86153
`;
87154

88-
exports[`create-jest-preset ESM presets createWithBabelEsmPreset should return preset config 1`] = `
155+
exports[`create-jest-preset ESM presets createJsWithBabelEsmPreset should return preset config 1`] = `
89156
{
90157
"extensionsToTreatAsEsm": [
91158
".jsx",
@@ -109,6 +176,48 @@ exports[`create-jest-preset ESM presets createWithBabelEsmPreset should return p
109176
}
110177
`;
111178

179+
exports[`create-jest-preset ESM presets createJsWithTsEsmLegacyPreset should return preset config 1`] = `
180+
{
181+
"extensionsToTreatAsEsm": [
182+
".jsx",
183+
".ts",
184+
".tsx",
185+
".mts",
186+
],
187+
"transform": {
188+
"^.+\\.m?[tj]sx?$": [
189+
"ts-jest/legacy",
190+
{
191+
"isolatedModules": true,
192+
"tsconfig": "tsconfig.spec.json",
193+
"useESM": true,
194+
},
195+
],
196+
},
197+
}
198+
`;
199+
200+
exports[`create-jest-preset ESM presets createJsWithTsEsmPreset should return preset config 1`] = `
201+
{
202+
"extensionsToTreatAsEsm": [
203+
".jsx",
204+
".ts",
205+
".tsx",
206+
".mts",
207+
],
208+
"transform": {
209+
"^.+\\.m?[tj]sx?$": [
210+
"ts-jest",
211+
{
212+
"isolatedModules": true,
213+
"tsconfig": "tsconfig.spec.json",
214+
"useESM": true,
215+
},
216+
],
217+
},
218+
}
219+
`;
220+
112221
exports[`create-jest-preset createJestPreset should return correct preset 1`] = `
113222
{
114223
"transform": {

‎src/presets/create-jest-preset.spec.ts

+75-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ import {
88
createDefaultEsmPreset,
99
createJsWithTsEsmPreset,
1010
createJsWithBabelEsmPreset,
11+
createJsWithTsLegacyPreset,
12+
createJsWithBabelLegacyPreset,
13+
createDefaultEsmLegacyPreset,
14+
createJsWithTsEsmLegacyPreset,
15+
createJsWithBabelEsmLegacyPreset,
1116
} from './create-jest-preset'
1217

1318
describe('create-jest-preset', () => {
@@ -73,6 +78,16 @@ describe('create-jest-preset', () => {
7378
})
7479
})
7580

81+
describe('createDefaultLegacyPreset', () => {
82+
it('should return preset config', () => {
83+
expect(
84+
createDefaultPreset({
85+
tsconfig: 'tsconfig.spec.json',
86+
}),
87+
).toMatchSnapshot()
88+
})
89+
})
90+
7691
describe('createJsWithTsPreset', () => {
7792
it('should return preset config', () => {
7893
expect(
@@ -84,7 +99,18 @@ describe('create-jest-preset', () => {
8499
})
85100
})
86101

87-
describe('createWithBabelPreset', () => {
102+
describe('createJsWithTsLegacyPreset', () => {
103+
it('should return preset config', () => {
104+
expect(
105+
createJsWithTsLegacyPreset({
106+
tsconfig: 'tsconfig.spec.json',
107+
isolatedModules: true,
108+
}),
109+
).toMatchSnapshot()
110+
})
111+
})
112+
113+
describe('createJsWithBabelPreset', () => {
88114
it('should return preset config', () => {
89115
expect(
90116
createJsWithBabelPreset({
@@ -96,6 +122,19 @@ describe('create-jest-preset', () => {
96122
).toMatchSnapshot()
97123
})
98124
})
125+
126+
describe('createJsWithBabelLegacyPreset', () => {
127+
it('should return preset config', () => {
128+
expect(
129+
createJsWithBabelLegacyPreset({
130+
tsconfig: 'tsconfig.spec.json',
131+
babelConfig: {
132+
babelrc: true,
133+
},
134+
}),
135+
).toMatchSnapshot()
136+
})
137+
})
99138
})
100139

101140
describe('ESM presets', () => {
@@ -109,6 +148,16 @@ describe('create-jest-preset', () => {
109148
})
110149
})
111150

151+
describe('createDefaultEsmLegacyPreset', () => {
152+
it('should return preset config', () => {
153+
expect(
154+
createDefaultEsmLegacyPreset({
155+
tsconfig: 'tsconfig.spec.json',
156+
}),
157+
).toMatchSnapshot()
158+
})
159+
})
160+
112161
describe('createJsWithTsEsmPreset', () => {
113162
it('should return preset config', () => {
114163
expect(
@@ -120,7 +169,18 @@ describe('create-jest-preset', () => {
120169
})
121170
})
122171

123-
describe('createWithBabelEsmPreset', () => {
172+
describe('createJsWithTsEsmLegacyPreset', () => {
173+
it('should return preset config', () => {
174+
expect(
175+
createJsWithTsEsmLegacyPreset({
176+
tsconfig: 'tsconfig.spec.json',
177+
isolatedModules: true,
178+
}),
179+
).toMatchSnapshot()
180+
})
181+
})
182+
183+
describe('createJsWithBabelEsmPreset', () => {
124184
it('should return preset config', () => {
125185
expect(
126186
createJsWithBabelEsmPreset({
@@ -132,5 +192,18 @@ describe('create-jest-preset', () => {
132192
).toMatchSnapshot()
133193
})
134194
})
195+
196+
describe('createJsWithBabelEsmLegacyPreset', () => {
197+
it('should return preset config', () => {
198+
expect(
199+
createJsWithBabelEsmLegacyPreset({
200+
tsconfig: 'tsconfig.spec.json',
201+
babelConfig: {
202+
babelrc: true,
203+
},
204+
}),
205+
).toMatchSnapshot()
206+
})
207+
})
135208
})
136209
})

‎src/presets/create-jest-preset.ts

+114-26
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,25 @@ import {
1010
ESM_TS_JS_TRANSFORM_PATTERN,
1111
ESM_JS_TRANSFORM_PATTERN,
1212
} from '../constants'
13-
import type {
13+
import {
14+
DefaultEsmLegacyPreset,
15+
DefaultEsmPreset,
16+
DefaultEsmTransformOptions,
17+
DefaultLegacyPreset,
1418
DefaultPreset,
19+
DefaultTransformOptions,
20+
JsWithBabelEsmLegacyPreset,
21+
JsWithBabelEsmPreset,
22+
JsWithBabelEsmTransformOptions,
23+
JsWithBabelLegacyPreset,
1524
JsWithBabelPreset,
25+
JsWithBabelTransformerOptions,
26+
JsWithTsEsmLegacyPreset,
27+
JsWithTsEsmPreset,
28+
JsWithTsEsmTransformOptions,
29+
JsWithTsLegacyPreset,
1630
JsWithTsPreset,
31+
JsWithTsTransformOptions,
1732
TsJestPresets,
1833
TsJestTransformerOptions,
1934
} from '../types'
@@ -48,9 +63,7 @@ export function createJestPreset(
4863
}
4964
}
5065

51-
export function createDefaultPreset(
52-
tsJestTransformOptions: Omit<TsJestTransformerOptions, 'useESM' | 'babelConfig'> = {},
53-
): DefaultPreset {
66+
export function createDefaultPreset(tsJestTransformOptions: DefaultTransformOptions = {}): DefaultPreset {
5467
logger.debug('creating default CJS Jest preset')
5568

5669
return {
@@ -60,9 +73,17 @@ export function createDefaultPreset(
6073
}
6174
}
6275

63-
export function createJsWithTsPreset(
64-
tsJestTransformOptions: Omit<TsJestTransformerOptions, 'useESM' | 'babelConfig'> = {},
65-
): JsWithTsPreset {
76+
export function createDefaultLegacyPreset(tsJestTransformOptions: DefaultTransformOptions = {}): DefaultLegacyPreset {
77+
logger.debug('creating default CJS Jest preset')
78+
79+
return {
80+
transform: {
81+
[TS_TRANSFORM_PATTERN]: ['ts-jest/legacy', tsJestTransformOptions],
82+
},
83+
}
84+
}
85+
86+
export function createJsWithTsPreset(tsJestTransformOptions: JsWithTsTransformOptions = {}): JsWithTsPreset {
6687
logger.debug('creating Js with Ts CJS Jest preset')
6788

6889
return {
@@ -72,9 +93,19 @@ export function createJsWithTsPreset(
7293
}
7394
}
7495

75-
export function createJsWithBabelPreset(
76-
tsJestTransformOptions: Omit<TsJestTransformerOptions, 'useESM'> = {},
77-
): JsWithBabelPreset {
96+
export function createJsWithTsLegacyPreset(
97+
tsJestTransformOptions: JsWithTsTransformOptions = {},
98+
): JsWithTsLegacyPreset {
99+
logger.debug('creating Js with Ts CJS Jest preset')
100+
101+
return {
102+
transform: {
103+
[TS_JS_TRANSFORM_PATTERN]: ['ts-jest/legacy', tsJestTransformOptions],
104+
},
105+
}
106+
}
107+
108+
export function createJsWithBabelPreset(tsJestTransformOptions: JsWithBabelTransformerOptions = {}): JsWithBabelPreset {
78109
logger.debug('creating JS with Babel CJS Jest preset')
79110

80111
return {
@@ -85,12 +116,20 @@ export function createJsWithBabelPreset(
85116
}
86117
}
87118

88-
export function createDefaultEsmPreset(tsJestTransformOptions: Omit<TsJestTransformerOptions, 'useESM'> = {}): {
89-
extensionsToTreatAsEsm: string[]
90-
transform: {
91-
[ESM_TS_TRANSFORM_PATTERN]: ['ts-jest', { useESM: true } & typeof tsJestTransformOptions]
119+
export function createJsWithBabelLegacyPreset(
120+
tsJestTransformOptions: JsWithBabelTransformerOptions = {},
121+
): JsWithBabelLegacyPreset {
122+
logger.debug('creating JS with Babel CJS Jest preset')
123+
124+
return {
125+
transform: {
126+
[JS_TRANSFORM_PATTERN]: 'babel-jest',
127+
[TS_TRANSFORM_PATTERN]: ['ts-jest/legacy', tsJestTransformOptions],
128+
},
92129
}
93-
} {
130+
}
131+
132+
export function createDefaultEsmPreset(tsJestTransformOptions: DefaultEsmTransformOptions = {}): DefaultEsmPreset {
94133
logger.debug('creating default ESM Jest preset')
95134

96135
return {
@@ -107,12 +146,26 @@ export function createDefaultEsmPreset(tsJestTransformOptions: Omit<TsJestTransf
107146
}
108147
}
109148

110-
export function createJsWithTsEsmPreset(tsJestTransformOptions: Omit<TsJestTransformerOptions, 'useESM'> = {}): {
111-
extensionsToTreatAsEsm: string[]
112-
transform: {
113-
[ESM_TS_JS_TRANSFORM_PATTERN]: ['ts-jest', { useESM: true } & typeof tsJestTransformOptions]
149+
export function createDefaultEsmLegacyPreset(
150+
tsJestTransformOptions: DefaultEsmTransformOptions = {},
151+
): DefaultEsmLegacyPreset {
152+
logger.debug('creating default ESM Jest preset')
153+
154+
return {
155+
extensionsToTreatAsEsm: [...JS_EXT_TO_TREAT_AS_ESM, ...TS_EXT_TO_TREAT_AS_ESM],
156+
transform: {
157+
[ESM_TS_TRANSFORM_PATTERN]: [
158+
'ts-jest/legacy',
159+
{
160+
...tsJestTransformOptions,
161+
useESM: true,
162+
},
163+
],
164+
},
114165
}
115-
} {
166+
}
167+
168+
export function createJsWithTsEsmPreset(tsJestTransformOptions: JsWithTsEsmTransformOptions = {}): JsWithTsEsmPreset {
116169
logger.debug('creating Js with Ts ESM Jest preset')
117170

118171
return {
@@ -129,13 +182,28 @@ export function createJsWithTsEsmPreset(tsJestTransformOptions: Omit<TsJestTrans
129182
}
130183
}
131184

132-
export function createJsWithBabelEsmPreset(tsJestTransformOptions: Omit<TsJestTransformerOptions, 'useESM'> = {}): {
133-
extensionsToTreatAsEsm: string[]
134-
transform: {
135-
[ESM_JS_TRANSFORM_PATTERN]: 'babel-jest'
136-
[ESM_TS_TRANSFORM_PATTERN]: ['ts-jest', { useESM: true } & typeof tsJestTransformOptions]
185+
export function createJsWithTsEsmLegacyPreset(
186+
tsJestTransformOptions: JsWithTsEsmTransformOptions = {},
187+
): JsWithTsEsmLegacyPreset {
188+
logger.debug('creating Js with Ts ESM Jest preset')
189+
190+
return {
191+
extensionsToTreatAsEsm: [...JS_EXT_TO_TREAT_AS_ESM, ...TS_EXT_TO_TREAT_AS_ESM],
192+
transform: {
193+
[ESM_TS_JS_TRANSFORM_PATTERN]: [
194+
'ts-jest/legacy',
195+
{
196+
...tsJestTransformOptions,
197+
useESM: true,
198+
},
199+
],
200+
},
137201
}
138-
} {
202+
}
203+
204+
export function createJsWithBabelEsmPreset(
205+
tsJestTransformOptions: JsWithBabelEsmTransformOptions = {},
206+
): JsWithBabelEsmPreset {
139207
logger.debug('creating JS with Babel ESM Jest preset')
140208

141209
return {
@@ -152,3 +220,23 @@ export function createJsWithBabelEsmPreset(tsJestTransformOptions: Omit<TsJestTr
152220
},
153221
}
154222
}
223+
224+
export function createJsWithBabelEsmLegacyPreset(
225+
tsJestTransformOptions: JsWithBabelEsmTransformOptions = {},
226+
): JsWithBabelEsmLegacyPreset {
227+
logger.debug('creating JS with Babel ESM Jest preset')
228+
229+
return {
230+
extensionsToTreatAsEsm: [...JS_EXT_TO_TREAT_AS_ESM, ...TS_EXT_TO_TREAT_AS_ESM],
231+
transform: {
232+
[ESM_JS_TRANSFORM_PATTERN]: 'babel-jest',
233+
[ESM_TS_TRANSFORM_PATTERN]: [
234+
'ts-jest/legacy',
235+
{
236+
...tsJestTransformOptions,
237+
useESM: true,
238+
},
239+
],
240+
},
241+
}
242+
}

‎src/types.ts

+76-4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,14 @@ import type * as babelJest from 'babel-jest'
44
import type * as _babel from 'babel__core'
55
import type * as _ts from 'typescript'
66

7-
import { JS_TRANSFORM_PATTERN, TS_JS_TRANSFORM_PATTERN, TS_TRANSFORM_PATTERN } from './constants'
7+
import {
8+
ESM_JS_TRANSFORM_PATTERN,
9+
ESM_TS_JS_TRANSFORM_PATTERN,
10+
ESM_TS_TRANSFORM_PATTERN,
11+
JS_TRANSFORM_PATTERN,
12+
TS_JS_TRANSFORM_PATTERN,
13+
TS_TRANSFORM_PATTERN,
14+
} from './constants'
815
import type { ConfigSet } from './legacy/config/config-set'
916
import type { RawCompilerOptions } from './raw-compiler-options'
1017

@@ -238,20 +245,85 @@ export interface TsJestAstTransformer {
238245
afterDeclarations: AstTransformerDesc[]
239246
}
240247

248+
export type DefaultTransformOptions = Omit<TsJestTransformerOptions, 'useESM' | 'babelConfig'>
241249
export type DefaultPreset = {
242250
transform: {
243-
[TS_TRANSFORM_PATTERN]: ['ts-jest', Omit<TsJestTransformerOptions, 'useESM' | 'babelConfig'>]
251+
[TS_TRANSFORM_PATTERN]: ['ts-jest', DefaultTransformOptions]
244252
}
245253
}
254+
export type DefaultLegacyPreset = {
255+
transform: {
256+
[TS_TRANSFORM_PATTERN]: ['ts-jest/legacy', DefaultTransformOptions]
257+
}
258+
}
259+
260+
export type DefaultEsmTransformOptions = Omit<TsJestTransformerOptions, 'useESM'>
261+
export type DefaultEsmPreset = {
262+
extensionsToTreatAsEsm: string[]
263+
transform: {
264+
[ESM_TS_TRANSFORM_PATTERN]: ['ts-jest', { useESM: true } & DefaultEsmTransformOptions]
265+
}
266+
}
267+
export type DefaultEsmLegacyPreset = {
268+
extensionsToTreatAsEsm: string[]
269+
transform: {
270+
[ESM_TS_TRANSFORM_PATTERN]: ['ts-jest/legacy', { useESM: true } & DefaultEsmTransformOptions]
271+
}
272+
}
273+
274+
export type JsWithTsTransformOptions = Omit<TsJestTransformerOptions, 'useESM' | 'babelConfig'>
246275
export type JsWithTsPreset = {
247276
transform: {
248-
[TS_JS_TRANSFORM_PATTERN]: ['ts-jest', Omit<TsJestTransformerOptions, 'useESM' | 'babelConfig'>]
277+
[TS_JS_TRANSFORM_PATTERN]: ['ts-jest', JsWithTsTransformOptions]
278+
}
279+
}
280+
export type JsWithTsLegacyPreset = {
281+
transform: {
282+
[TS_JS_TRANSFORM_PATTERN]: ['ts-jest/legacy', JsWithTsTransformOptions]
249283
}
250284
}
285+
286+
export type JsWithTsEsmTransformOptions = Omit<TsJestTransformerOptions, 'useESM'>
287+
export type JsWithTsEsmPreset = {
288+
extensionsToTreatAsEsm: string[]
289+
transform: {
290+
[ESM_TS_JS_TRANSFORM_PATTERN]: ['ts-jest', { useESM: true } & JsWithTsEsmTransformOptions]
291+
}
292+
}
293+
export type JsWithTsEsmLegacyPreset = {
294+
extensionsToTreatAsEsm: string[]
295+
transform: {
296+
[ESM_TS_JS_TRANSFORM_PATTERN]: ['ts-jest/legacy', { useESM: true } & JsWithTsEsmTransformOptions]
297+
}
298+
}
299+
300+
export type JsWithBabelTransformerOptions = Omit<TsJestTransformerOptions, 'useESM'>
251301
export type JsWithBabelPreset = {
252302
transform: {
253303
[JS_TRANSFORM_PATTERN]: 'babel-jest'
254-
[TS_TRANSFORM_PATTERN]: ['ts-jest', Omit<TsJestTransformerOptions, 'useESM'>]
304+
[TS_TRANSFORM_PATTERN]: ['ts-jest', JsWithBabelTransformerOptions]
305+
}
306+
}
307+
export type JsWithBabelLegacyPreset = {
308+
transform: {
309+
[JS_TRANSFORM_PATTERN]: 'babel-jest'
310+
[TS_TRANSFORM_PATTERN]: ['ts-jest/legacy', JsWithBabelTransformerOptions]
311+
}
312+
}
313+
314+
export type JsWithBabelEsmTransformOptions = Omit<TsJestTransformerOptions, 'useESM'>
315+
export type JsWithBabelEsmPreset = {
316+
extensionsToTreatAsEsm: string[]
317+
transform: {
318+
[ESM_JS_TRANSFORM_PATTERN]: 'babel-jest'
319+
[ESM_TS_TRANSFORM_PATTERN]: ['ts-jest', { useESM: true } & JsWithBabelEsmTransformOptions]
320+
}
321+
}
322+
export type JsWithBabelEsmLegacyPreset = {
323+
extensionsToTreatAsEsm: string[]
324+
transform: {
325+
[ESM_JS_TRANSFORM_PATTERN]: 'babel-jest'
326+
[ESM_TS_TRANSFORM_PATTERN]: ['ts-jest/legacy', { useESM: true } & JsWithBabelEsmTransformOptions]
255327
}
256328
}
257329

0 commit comments

Comments
 (0)
Please sign in to comment.