Skip to content

Commit

Permalink
refactor(presets): use create preset util functions for cjs presets
Browse files Browse the repository at this point in the history
  • Loading branch information
ahnpnl committed Jul 6, 2024
1 parent 22fb027 commit 922d6d0
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 56 deletions.
56 changes: 18 additions & 38 deletions e2e/presets/__tests__/presets.spec.ts
Original file line number Diff line number Diff line change
@@ -1,74 +1,54 @@
import { JS_EXT_TO_TREAT_AS_ESM, TS_EXT_TO_TREAT_AS_ESM } from 'ts-jest'
import {
JS_EXT_TO_TREAT_AS_ESM,
TS_EXT_TO_TREAT_AS_ESM,
createDefaultPreset,
createDefaultEsmPreset,
createJsWithTsPreset,
createJsWithTsEsmPreset,
createJsWithBabelPreset,
createJsWithBabelEsmPreset,
} 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'
import presets from 'ts-jest/presets'

import extendedJestConfig from '../extended-jest-config'

const defaultPreset = require('ts-jest/jest-preset')

test('presets', () => {
expect(defaultPreset).toEqual({
transform: {
'^.+\\.tsx?$': ['ts-jest', {}],
},
})
expect(defaultPreset).toEqual(createDefaultPreset())
expect(presets.defaultsLegacy).toEqual({
transform: {
'^.+\\.tsx?$': ['ts-jest/legacy', {}],
},
})
expect(presets.defaultsESM).toEqual({
extensionsToTreatAsEsm: [...TS_EXT_TO_TREAT_AS_ESM],
transform: {
'^.+\\.tsx?$': ['ts-jest', { useESM: true }],
},
})
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.jsWithTs).toEqual({
transform: {
'^.+\\.[tj]sx?$': ['ts-jest', {}],
},
})
expect(presets.jsWithTs).toEqual(createJsWithTsPreset())
expect(presets.jsWithTsLegacy).toEqual({
transform: {
'^.+\\.[tj]sx?$': ['ts-jest/legacy', {}],
},
})
expect(presets.jsWithTsESM).toEqual({
extensionsToTreatAsEsm: [...JS_EXT_TO_TREAT_AS_ESM, ...TS_EXT_TO_TREAT_AS_ESM],
transform: {
'^.+\\.m?[tj]sx?$': ['ts-jest', { useESM: true }],
},
})
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.jsWithBabel).toEqual({
transform: {
'^.+\\.tsx?$': ['ts-jest', {}],
'^.+\\.jsx?$': 'babel-jest',
},
})
expect(presets.jsWithBabel).toEqual(createJsWithBabelPreset())
expect(presets.jsWithBabelLegacy).toEqual({
transform: {
'^.+\\.tsx?$': ['ts-jest/legacy', {}],
'^.+\\.jsx?$': 'babel-jest',
},
})
expect(presets.jsWithBabelESM).toEqual({
extensionsToTreatAsEsm: [...JS_EXT_TO_TREAT_AS_ESM, ...TS_EXT_TO_TREAT_AS_ESM],
transform: {
'^.+\\.tsx?$': ['ts-jest', { useESM: true }],
'^.+\\.m?[j]sx?$': 'babel-jest',
},
})
expect(presets.jsWithBabelESM).toEqual(createJsWithBabelEsmPreset())
expect(presets.jsWithBabelESMLegacy).toEqual({
extensionsToTreatAsEsm: [...JS_EXT_TO_TREAT_AS_ESM, ...TS_EXT_TO_TREAT_AS_ESM],
transform: {
Expand Down
33 changes: 15 additions & 18 deletions presets/index.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,43 @@
const { JS_EXT_TO_TREAT_AS_ESM, TS_EXT_TO_TREAT_AS_ESM } = require('../dist/constants')
const { createJestPreset } = require('../dist/presets/create-jest-preset')
const {
createJestPreset,
createDefaultPreset,
createDefaultEsmPreset,
createJsWithTsPreset,
createJsWithTsEsmPreset,
createJsWithBabelPreset,
createJsWithBabelEsmPreset,
} = require('../dist/presets/create-jest-preset')

module.exports = {
get defaults() {
return createJestPreset()
return createDefaultPreset()
},
get defaultsLegacy() {
return createJestPreset(true, false)
},
get defaultsESM() {
return createJestPreset(false, false, { extensionsToTreatAsEsm: TS_EXT_TO_TREAT_AS_ESM })
return createDefaultEsmPreset()
},
get defaultsESMLegacy() {
return createJestPreset(true, false, { extensionsToTreatAsEsm: TS_EXT_TO_TREAT_AS_ESM })
},
get jsWithTs() {
return createJestPreset(false, true)
return createJsWithTsPreset()
},
get jsWithTsLegacy() {
return createJestPreset(true, true)
},
get jsWithTsESM() {
return createJestPreset(false, true, {
extensionsToTreatAsEsm: [...JS_EXT_TO_TREAT_AS_ESM, ...TS_EXT_TO_TREAT_AS_ESM],
})
return createJsWithTsEsmPreset()
},
get jsWithTsESMLegacy() {
return createJestPreset(true, true, {
extensionsToTreatAsEsm: [...JS_EXT_TO_TREAT_AS_ESM, ...TS_EXT_TO_TREAT_AS_ESM],
})
},
get jsWithBabel() {
return createJestPreset(false, false, {
transform: {
'^.+\\.jsx?$': 'babel-jest',
},
})
return createJsWithBabelPreset()
},
get jsWithBabelLegacy() {
return createJestPreset(true, false, {
Expand All @@ -45,12 +47,7 @@ module.exports = {
})
},
get jsWithBabelESM() {
return createJestPreset(false, false, {
extensionsToTreatAsEsm: [...JS_EXT_TO_TREAT_AS_ESM, ...TS_EXT_TO_TREAT_AS_ESM],
transform: {
'^.+\\.m?[j]sx?$': 'babel-jest',
},
})
return createJsWithBabelEsmPreset()
},
get jsWithBabelESMLegacy() {
return createJestPreset(true, false, {
Expand Down

0 comments on commit 922d6d0

Please sign in to comment.