Configuration

ts-jest configuration is done within Jest configuration object. This latest can be in package.json under the jest property, or in its own jest.config.js file. The later is preferred since it’s more customizable, but it depends on your needs and preference.

Jest preset

The 3 presets

ts-jest comes with 3 presets, covering most of project’s base configuration:

Preset name Description
ts-jest/presets/default
or ts-jest
ts-jest will take care of .ts and .tsx files only, leaving JavaScript files as-is.
ts-jest/presets/js-with-ts TypeScript and JavaScript file (.ts, .tsx, .js and .jsx) will be handled by ts-jest.
You’ll need to set allowJs to true in your tsconfig.json file.
ts-jest/presets/js-with-babel TypeScript files will be handled by ts-jest, and JavaScript files will be handled 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 did add ts-jest to your dev. dependencies of course):

// jest.config.js
module.exports = {
  // [...]
  // Replace `ts-jest` with the preset you want to use
  // from the above list
  preset: 'ts-jest'
};
// OR package.json
{
  // [...]
  "jest": {
    // 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

Any preset can also be used with other options. If you’re already using another preset, you might want only some specific settings from the chosen ts-jest preset. In this case you’ll need to use the JavaScript version of Jest config (comment/uncomment according to your use-case):

// jest.config.js
const { defaults: tsjPreset } = require('ts-jest/presets');
// const { jsWithTs: tsjPreset } = require('ts-jest/presets');
// const { jsWithBabel: tsjPreset } = require('ts-jest/presets');

module.exports = {
  // [...]
  transform: {
    ...tsjPreset.transform,
    // [...]
  }
}

Paths mapping

If you use “baseUrl” and “paths” options in your tsconfig file, you should make sure the “moduleNameMapper” option in your Jest config is setup accordingly.

ts-jest provides a helper to transform the mapping from tsconfig to Jest config format, but it needs the .js version of the config file.

Example

TypeScript config

With the below config in your tsconfig:

// tsconfig.json
{
  "compilerOptions": {
    "paths": {
      "@App/*": ["src/*"],
      "lib/*": ["common/*"]
    }
  }
}

Jest config (without helper)

// jest.config.js
module.exports = {
  // [...]
  moduleNameMapper: {
    '^@App/(.*)$': '<rootDir>/src/$1',
    '^lib/(.*)$': '<rootDir>/common/$1'
  }
};
// OR package.json
{
  // [...]
  "jest": {
    "moduleNameMapper": {
      "^@App/(.*)$": "<rootDir>/src/$1",
      "^lib/(.*)$": "<rootDir>/common/$1"
    }
  }
}

Jest config (with helper)

// jest.config.js
const { pathsToModuleNameMapper } = require('ts-jest/utils');
// In the following statement, replace `./tsconfig` with the path to your `tsconfig` file
// which contains the path mapping (ie the `compilerOptions.paths` option):
const { compilerOptions } = require('./tsconfig');

module.exports = {
  // [...]
  moduleNameMapper: pathsToModuleNameMapper(compilerOptions.paths /*, { prefix: '<rootDir>/' } */ )
};

ts-jest options

Introduction

All configuration of ts-jest specific options are located under globals.ts-jest path of your Jest config:

// jest.config.js
module.exports = {
  // [...]
  globals: {
    'ts-jest': {
      // ts-jest configuration goes here
    }
  }
};
// OR package.json
{
  // [...]
  "jest": {
    "globals": {
      "ts-jest": {
        // ts-jest configuration goes here
      }
    }
  }
}

Options

All options have default values which should fit most of the projects. Click on the option’s name to see details and example(s).

Option Description Type Default
compiler TypeScript module to use as compiler. string "typescript"
tsConfig TypeScript compiler related configuration. string|object|boolean auto
isolatedModules Disable type-checking boolean false
diagnostics Diagnostics related configuration. boolean|object true
babelConfig Babel(Jest) related configuration. boolean|object disabled
stringifyContentPathRegex Files which will become modules returning self content. string|RegExp disabled

Upgrading

You can use the config:migrate tool of ts-jest CLI if you’re coming from an older version to help you migrate your Jest configuration.

If you’re using jest.config.json:

npx ts-jest config:migrate jest.config.js

If you’re using jest config property of package.json:

npx ts-jest config:migrate package.json