ts-jest
ts-jest is a Jest transformer with source map support that enables testing projects written in TypeScript using Jest. It supports all TypeScript features, including type-checking. The current stable version is 29.4.9. Note that ts-jest does not follow semantic versioning, which means major breaking changes can occur without a corresponding major version increment. Releases are frequent, primarily for patch updates.
Common errors
-
Jest: ts-jest preset was not applied. Ensure that you have configured jest.config.js/ts to use a preset.
cause The Jest configuration file (`jest.config.js` or `jest.config.ts`) does not include `preset: 'ts-jest'`.fixAdd `preset: 'ts-jest'` to your Jest configuration file, for example: `export default { preset: 'ts-jest' };` -
Cannot find module 'typescript' or 'jest' or '@jest/types'
cause A required peer dependency of `ts-jest` (like `typescript`, `jest`, `@jest/types`, `babel-jest`, etc.) is not installed or its version is incompatible.fixInstall all necessary peer dependencies explicitly using `npm install -D jest typescript @types/jest ts-jest` (or `yarn add --dev ...`) and ensure their versions fall within the ranges specified by `ts-jest`. -
Error: Jest: a transformer must at least provide a `process` function.
cause This usually indicates an incompatibility between `ts-jest` and the installed version of Jest or TypeScript, or a corrupted `node_modules`.fixVerify that your `jest` and `typescript` versions satisfy `ts-jest`'s peer dependency requirements. Try clearing your `node_modules` and `npm cache clean --force` (or `yarn cache clean`), then reinstall dependencies. -
TSxxxx: [TypeScript compilation error]
cause TypeScript compiler errors are being reported during the test run, indicating issues with your code's types or `tsconfig.json` configuration.fixAddress the specific TypeScript errors in your source or test files. Ensure your `tsconfig.json` (especially `compilerOptions` like `target`, `module`, `strict`) is correctly configured for your project and `ts-jest`.
Warnings
- breaking ts-jest explicitly states it does not follow semantic versioning. This means breaking changes can occur in minor or even patch releases, making upgrades potentially risky.
- gotcha ts-jest has strict peer dependency requirements for Jest and TypeScript versions. Incompatible versions can lead to unexpected errors or installation failures.
- gotcha There's a fundamental difference between using `ts-jest` for TypeScript compilation and using Babel with `@babel/preset-typescript`. `ts-jest` handles type-checking and Jest integration, while Babel with `preset-typescript` only strips types, potentially missing type errors.
Install
-
npm install ts-jest -
yarn add ts-jest -
pnpm add ts-jest
Quickstart
{
"devDependencies": {
"jest": "^29.4.9",
"typescript": "^5.0.0",
"ts-jest": "^29.4.9",
"@types/jest": "^29.4.9"
},
"scripts": {
"test": "jest"
}
}
// jest.config.ts (created by `npx ts-jest config:init`)
export default {
preset: 'ts-jest',
testEnvironment: 'node',
roots: ['<rootDir>/src'],
};
// src/sum.ts
export function sum(a: number, b: number): number {
return a + b;
}
// src/sum.test.ts
import { sum } from './sum';
describe('sum', () => {
it('adds two numbers', () => {
expect(sum(1, 2)).toBe(3);
});
});