Introduction to ts-jest
maints-jest is a Jest transformer with source map support that enables testing of TypeScript projects using Jest. It supports all TypeScript features, including type-checking.repository·main·Indexed 27 days ago
https://github.com/kulshekhar/ts-jestA Jest transformer with source map support that allows developers to test TypeScript projects using Jest, supporting all TypeScript features including type-checking. Version 29.4.12. It provides tools for initializing configuration via `ts-jest config:init`, support for ESM and CommonJS transformations, and the ability to implement custom TypeScript transformer plugins using a specific boilerplate structure.
ts-jest is a Jest transformer with source map support that enables testing of TypeScript projects using Jest. It supports all TypeScript features, including type-checking.Generate static content for the website. The output will be placed in the build directory, which can then be served by any static content hosting service.
yarn buildIf your tsconfig.json uses baseUrl and paths for module resolution, you must configure Jest's moduleNameMapper to match. ts-jest provides the pathsToModuleNameMapper helper to automate this transformation.
Note: The helper requires the .js version of your config file (or an imported object) to access compilerOptions.
To use the helper:
pathsToModuleNameMapper from ts-jest.compilerOptions from your TypeScript configuration file.compilerOptions.paths to the helper.prefix (e.g., <rootDir>/) to ensure paths resolve correctly relative to the project root.import { pathsToModuleNameMapper } from 'ts-jest'
import { compilerOptions } from './tsconfig'
import type { Config } from 'jest'
const jestConfig: Config = {
// [...]
roots: ['<rootDir>'],
modulePaths: [compilerOptions.baseUrl],
moduleNameMapper: pathsToModuleNameMapper(compilerOptions.paths, { prefix: '<rootDir>/' }),
}
export default jestConfigWhen choosing between using @babel/preset-typescript and ts-jest, be aware that Babel transpiles files as isolated modules without a notion of a 'project'. This leads to several functional differences. ts-jest provides full TypeScript support, whereas @babel/preset-typescript has the following limitations:
ts-jest performs type-checking out of the box, providing a more fluent TDD experience by throwing errors when type mismatches occur.namespace support: TypeScript namespaces cannot be used with the Babel preset.const enum support: Constant enums are not supported.enum or namespace will not work.import/export syntax: Syntax such as import lib = require('lib') or export = myVar is not supported.<Type>value syntax for type-casting is unavailable if JSX is enabled in your Babel configuration.When configuring your project with hybrid Node module values such as Node16, Node18, or NodeNext, be aware of the following behaviors in ts-jest:
ts-jest uses the TypeScript API for transpilation. Consequently, the emitted JavaScript code is dependent on the version of TypeScript currently installed in your project.CommonJs transformations, dynamic import statements will not be transformed into Promise and require calls.It is recommended to consult the official TypeScript documentation regarding Node16/Node18/NodeNext module resolution to understand how your code will be emitted.
To use ts-jest in your project, you need to install jest, typescript, ts-jest, and @types/jest. After installation, you can initialize your configuration using the ts-jest config:init command.
Note on TypeScript 7: If your project uses TypeScript 7, do not install typescript directly; instead, follow the supported side-by-side compiler setup guide in the official documentation.
Note on Versioning: ts-jest does not follow Semantic Versioning (SemVer). The major version number follows the version of Jest. If you need to revert to a version before the 23.10 rewrite, install a version <23.10.0.
By default, Jest does not compile .ts files. To enable TypeScript transpilation using ts-jest, you must create a Jest configuration file that uses the ts-jest preset.
You can automatically generate a basic configuration file using the ts-jest config:init command.
npx ts-jest config:init
# or
yarn ts-jest config:initWhen writing a custom TypeScript transformer plugin for ts-jest, you should follow a specific boilerplate structure. This involves exporting a version number (to invalidate Jest's cache when the transformer logic changes), a name for cache key construction, and a factory function that accepts a TsCompilerInstance.
The factory function provides access to the TypeScript compiler module (compilerInstance.configSet.compilerModule) and returns a Transformer<SourceFile> function. Inside the transformer, you use a Visitor to traverse and potentially modify the AST nodes using ts.visitEachChild or ts.visitNode.
import { SourceFile, TransformationContext, Transformer, Visitor } from 'typescript'
import type { TsCompilerInstance } from 'ts-jest/dist/types'
/**
* Remember to increase the version whenever transformer's content is changed. This is to inform Jest to not reuse
* the previous cache which contains old transformer's content
*/
export const version = 1
// Used for constructing cache key
export const name = 'hoist-jest'
export function factory(compilerInstance: TsCompilerInstance) {
const ts = compilerInstance.configSet.compilerModule
function createVisitor(ctx: TransformationContext, sf: SourceFile) {
const visitor: Visitor = (node) => {
// here we can check each node and potentially return
// new nodes if we want to leave the node as is, and
// continue searching through child nodes:
return ts.visitEachChild(node, visitor, ctx)
}
return visitor
}
// we return the factory expected in CustomTransformers
return (ctx: TransformationContext): Transformer<SourceFile> => {
return (sf: SourceFile) => ts.visitNode(sf, createVisitor(ctx, sf))
}
}ts-jest within a monorepo structure, you must utilize the Jest projects configuration. When the projects array is configured in your Jest setup, Jest will execute ts-jest against each individual project defined within that configuration.globalSetup or globalTeardown are not processed directly by Jest and therefore do not respect the moduleNameMapper configuration. To enable TypeScript path mapping in these files, you must manually register tsconfig-paths.When using the TypeScript 7 side-by-side installation with ts-jest, be aware of the following behaviors:
tsc (the native compiler). Jest transform performance remains equivalent to TypeScript 6 because ts-jest uses the compatibility API.npx tsc --noEmit separately in local development and CI to ensure your project passes the authoritative TypeScript 7 type-check.If you are upgrading from ts-jest version 23.10 or older, you can use the config:migrate CLI tool to automatically help migrate your Jest configuration to the current format.
Depending on how your configuration is stored, run the corresponding command below.