Skip to main content
Version: 29.2

Presets

The presets

important

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.

caution

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 nameDescription
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-tsTypeScript 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-esmTypeScript 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-babelTypeScript 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-esmTypeScript 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):

// jest.config.js
/** @type {import('ts-jest').JestConfigWithTsJest} */
module.exports = {
// [...]
// Replace `ts-jest` with the preset you want to use
// from the above list
preset: 'ts-jest',
}

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 preset
  • createDefaultLegacyPreset: for default preset in legacy mode
  • createDefaultEsmPreset: for default ESM preset
  • createDefaultEsmLegacyPreset: for default ESM preset in legacy mode
  • createJsWithTsPreset: for js-with-ts preset
  • createJsWithTsLegacyPreset: for js-with-ts preset in legacy mode
  • createJsWithTsEsmPreset: for js-with-ts ESM preset
  • createJsWithTsEsmLegacyPreset: for js-with-ts ESM preset in legacy mode
  • createJsWithBabelPreset: for js-with-babel preset
  • createJsWithBabelLegacyPreset: for js-with-babel preset in legacy mode
  • createJsWithBabelEsmPreset: for js-with-babel ESM preset
  • createJsWithBabelEsmLegacyPreset: for js-with-babel ESM preset in legacy mode

Example:

// jest.config.js
const { createDefaultPreset } = require('ts-jest')

/** @type {import('ts-jest').JestConfigWithTsJest} */
module.exports = {
// [...]
transform: {
...createDefaultPreset().transform,
// [...]
},
}