Skip to content

Latest commit

 

History

History
268 lines (203 loc) · 7.46 KB

index.md

File metadata and controls

268 lines (203 loc) · 7.46 KB
title
Configuration

ts-jest configuration is done within the Jest configuration object. This can be in package.json under the jest property, or in its own jest.config.js file. The latter 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 the 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 files (.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 added 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": {
    "baseUrl": ".",
    "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 ts-jest specific options are located under the 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
      }
    }
  }
}

IDE ts-jest config suggestion

To utilize IDE suggestions, you can use JSDoc comments to provide suggested ts-jest configs for your Jest config:

/** @typedef {import('ts-jest/dist/types')} */
/** @type {import('@jest/types').Config.InitialOptions} */
const config = {
  // [...]
  globals: {
    'ts-jest': {
      // ts-jest configuration goes here and your IDE will suggest which configs when typing
    }
  }
};

module.exports = config;

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 disabled
astTransformers Custom TypeScript AST transformers object auto
diagnostics Diagnostics related configuration. boolean|object enabled
babelConfig Babel(Jest) related configuration. boolean|string|object disabled
stringifyContentPathRegex Files which will become modules returning self content. string|RegExp disabled

Version checking

By default, ts-jest supports a range of versions for jest/typescript. One uses incompatible versions will receive a warning message while running tests. This warning message can be opt-out by setting environment variable TS_JEST_DISABLE_VER_CHECKER:

Linux/MacOS

export TS_JEST_DISABLE_VER_CHECKER=true

Windows

set TS_JEST_DISABLE_VER_CHECKER=true

Note

As long as the environment variable TS_JEST_DISABLE_VER_CHECKER stays, the warning message will no longer show. This can lead to unexpected errors due to the usage of incompatible versions' dependencies. Use this environment variable with precautions.

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.js:

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