What is ts-jest and why is it needed?
ts-jest is a TypeScript preprocessor for the popular testing framework Jest. It allows you to run tests written in TypeScript without the need for preliminary compilation to JavaScript. The library automatically transforms TypeScript code into JavaScript during test execution, using the built-in TypeScript compiler.
The main goal of ts-jest is to provide seamless integration of TypeScript with Jest. Without it, you would have to manually configure TypeScript compilation before each test run or use additional tools like ts-node. ts-jest makes this process transparent and automatic.
Key advantages of using ts-jest:
- Automatic compilation — TypeScript files are compiled "on the fly" during testing
- Full TypeScript support — all language features (decorators, generics, types) work correctly
- Source maps — on errors, you see the original TypeScript code, not the compiled JavaScript
- Performance — ts-jest caches compilation results, speeding up subsequent runs
- Flexibility — you can configure TypeScript compiler options through Jest configuration
Installing ts-jest
To work with ts-jest, you will need Node.js (version 14 or higher) and a package manager npm or yarn. Installation is done with one command:
npm install --save-dev jest typescript ts-jest @types/jestOr via yarn:
yarn add --dev jest typescript ts-jest @types/jestPackage explanation:
jest— the testing framework itselftypescript— the TypeScript compilerts-jest— the preprocessor for integration@types/jest— TypeScript types for Jest (for autocompletion and type checking)
After installation, you need to create or update the jest.config.js file (or jest.config.ts):
// jest.config.jsmodule.exports = { preset: 'ts-jest', testEnvironment: 'node', roots: ['<rootDir>/src'], testMatch: ['**/__tests__/**/*.ts?(x)', '**/?(*.)+(spec|test).ts?(x)'], moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node']};Also, make sure the correct parameters are set in tsconfig.json:
{ "compilerOptions": { "target": "ES2020", "module": "commonjs", "strict": true, "esModuleInterop": true, "skipLibCheck": true, "forceConsistentCasingInFileNames": true, "outDir": "./dist", "rootDir": "./src", "declaration": true, "sourceMap": true }, "include": ["src/**/*"], "exclude": ["node_modules", "dist", "**/*.test.ts"]}Quick start with a code example
Let's create a simple function and a test for it. Project structure:
my-project/├── src/│ ├── math.ts│ └── __tests__/│ └── math.test.ts├── jest.config.js├── tsconfig.json└── package.jsonFile: src/math.ts
export function add(a: number, b: number): number { return a + b;}
export function subtract(a: number, b: number): number { return a - b;}
export function multiply(a: number, b: number): number { return a * b;}
export function divide(a: number, b: number): number { if (b === 0) { throw new Error('Division by zero'); } return a / b;}File: src/__tests__/math.test.ts
import { add, subtract, multiply, divide } from '../math';
describe('Math functions', () => { test('adds 1 + 2 to equal 3', () => { expect(add(1, 2)).toBe(3)