Skip to content

Commit

Permalink
feat(presets): add util functions to create ESM presets
Browse files Browse the repository at this point in the history
  • Loading branch information
ahnpnl committed Jul 7, 2024
1 parent 922d6d0 commit 06f78ed
Show file tree
Hide file tree
Showing 6 changed files with 408 additions and 98 deletions.
49 changes: 12 additions & 37 deletions e2e/presets/__tests__/presets.spec.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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', () => {
Expand Down
31 changes: 12 additions & 19 deletions presets/index.js
Original file line number Diff line number Diff line change
@@ -1,60 +1,53 @@
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 = {
get defaults() {
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()
},
}
129 changes: 119 additions & 10 deletions src/presets/__snapshots__/create-jest-preset.spec.ts.snap
Original file line number Diff line number Diff line change
@@ -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?$": [
Expand All @@ -13,21 +13,37 @@ 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",
},
],
},
}
`;

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",
Expand All @@ -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": [
Expand All @@ -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",
Expand All @@ -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,
},
Expand All @@ -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",
Expand All @@ -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": {
Expand Down
Loading

0 comments on commit 06f78ed

Please sign in to comment.