Presets
The presets
Starting from v28.0.0, ts-jest
will gradually opt in adoption of esbuild
/swc
more to improve the performance. To make the transition smoothly, we introduce legacy
presets as a fallback when the new codes don't work yet.
The list of preset
below is now deprecated in favor of util functions. If one is using preset
in Jest config, please run npx ts-jest config:migrate
or look into Advanced section below for alternative solutions.
ts-jest
comes with several presets, covering most of the project's base configuration:
Preset name | Description |
---|---|
ts-jest/presets/default or ts-jest | TypeScript files (.ts , .tsx ) will be transformed by ts-jest to CommonJS syntax, leaving JavaScript files (.js , jsx ) as-is. |
ts-jest/presets/default-legacy or ts-jest/legacy (LEGACY) | TypeScript files (.ts , .tsx ) will be transformed by ts-jest to CommonJS syntax, leaving JavaScript files (.js , jsx ) as-is. |
ts-jest/presets/default-esm | TypeScript files (.ts , .tsx ) will be transformed by ts-jest to ESM syntax, leaving JavaScript files (.js , jsx ) as-is. |
ts-jest/presets/default-esm-legacy (LEGACY) | TypeScript files (.ts , .tsx ) will be transformed by ts-jest to ESM syntax, leaving JavaScript files (.js , jsx ) as-is. |
ts-jest/presets/js-with-ts | TypeScript and JavaScript files (.ts , .tsx , .js , .jsx ) will be transformed by ts-jest to CommonJS syntax.You'll need to set allowJs to true in your tsconfig.json file. |
ts-jest/presets/js-with-ts-legacy (LEGACY) | TypeScript and JavaScript files (.ts , .tsx , .js , .jsx ) will be transformed by ts-jest to CommonJS syntax.You'll need to set allowJs to true in your tsconfig.json file. |
ts-jest/presets/js-with-ts-esm | TypeScript and JavaScript files (.ts , .tsx , .js , .jsx , .mjs ) will be transformed by ts-jest to ESM syntax.You'll need to set allowJs to true in your tsconfig.json file. |
ts-jest/presets/js-with-ts-esm-legacy (LEGACY) | TypeScript and JavaScript files (.ts , .tsx , .js , .jsx , .mjs ) will be transformed by ts-jest to ESM syntax.You'll need to set allowJs to true in your tsconfig.json file. |
ts-jest/presets/js-with-babel | TypeScript files (.ts , .tsx ) will be transformed by ts-jest to CommonJS syntax, and JavaScript files (.js , jsx ) will be transformed by babel-jest . |
ts-jest/presets/js-with-babel-legacy (LEGACY) | TypeScript files (.ts , .tsx ) will be transformed by ts-jest to CommonJS syntax, and JavaScript files (.js , jsx ) will be transformed by babel-jest . |
ts-jest/presets/js-with-babel-esm | TypeScript files (.ts , .tsx ) will be transformed by ts-jest to ESM syntax, and JavaScript files (.js , jsx , .mjs ) will be transformed by babel-jest . |
ts-jest/presets/js-with-babel-esm-legacy (LEGACY) | TypeScript files (.ts , .tsx ) will be transformed by ts-jest to ESM syntax, and JavaScript files (.js , jsx , .mjs ) will be transformed by babel-jest . |
Basic usage
In most cases, simply setting the preset
key to the desired preset name in your Jest config should be enough to start
using TypeScript with Jest (assuming you added ts-jest
to your devDependencies
of course):
- JavaScript
- TypeScript
// jest.config.js
const { createDefaultPreset } = require('ts-jest')
const defaultPreset = createDefaultPreset()
/** @type {import('ts-jest').JestConfigWithTsJest} */
module.exports = {
// [...]
// Replace `ts-jest` with the preset you want to use
// from the above list
...defaultPreset,
}
// jest.config.ts
import { type JestConfigWithTsJest, createDefaultPreset } from 'ts-jest'
const defaultPreset = createDefaultPreset()
const jestConfig: JestConfigWithTsJest = {
// [...]
// Replace `ts-jest` with the preset you want to use
// from the above list
...defaultPreset,
}
export default jestConfig
Note: presets use testMatch
, like Jest does in its defaults. If you want to use testRegex
instead in your configuration, you MUST set testMatch
to null
or Jest will bail.
Advanced
There are several util functions to create and extend the existing presets:
createDefaultPreset
: for default presetcreateDefaultLegacyPreset
: for default preset in legacy modecreateDefaultEsmPreset
: for default ESM presetcreateDefaultEsmLegacyPreset
: for default ESM preset in legacy modecreateJsWithTsPreset
: forjs-with-ts
presetcreateJsWithTsLegacyPreset
: forjs-with-ts
preset in legacy modecreateJsWithTsEsmPreset
: forjs-with-ts
ESM presetcreateJsWithTsEsmLegacyPreset
: forjs-with-ts
ESM preset in legacy modecreateJsWithBabelPreset
: forjs-with-babel
presetcreateJsWithBabelLegacyPreset
: forjs-with-babel
preset in legacy modecreateJsWithBabelEsmPreset
: forjs-with-babel
ESM presetcreateJsWithBabelEsmLegacyPreset
: forjs-with-babel
ESM preset in legacy mode
Example:
- JavaScript
- TypeScript
// jest.config.js
const { createDefaultPreset } = require('ts-jest')
/** @type {import('ts-jest').JestConfigWithTsJest} */
module.exports = {
// [...]
transform: {
...createDefaultPreset().transform,
// [...]
},
}
// jest.config.ts
import { createDefaultPreset, type JestConfigWithTsJest } from 'ts-jest'
const jestConfig: JestConfigWithTsJest = {
// [...]
transform: {
...createDefaultPreset().transform,
// [...]
},
}
export default jestConfig