Last modified: Jul 21, 2025 By Alexander Williams
How to Install ts-jest in Node.js
ts-jest is a TypeScript preprocessor for Jest. It allows you to test TypeScript code with Jest. This guide will show you how to install and configure it.
Prerequisites
Before installing ts-jest, ensure you have Node.js and npm installed. You also need Jest and TypeScript.
If you don't have Jest, follow our guide on How to Install Jest in Node.js.
For TypeScript, check our Install TypeScript in Node.js guide.
Install ts-jest
Run this command in your project directory:
npm install --save-dev ts-jest @types/jest
This installs ts-jest and Jest types as dev dependencies.
Configure Jest for TypeScript
Create or modify your jest.config.js
file:
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
};
This tells Jest to use ts-jest for TypeScript files.
Update tsconfig.json
Ensure your tsconfig.json
has these compiler options:
{
"compilerOptions": {
"types": ["jest"],
"esModuleInterop": true
}
}
This adds Jest types and enables ES module interop.
Write a Test
Create a simple test file example.test.ts
:
// Example test
describe('sum module', () => {
test('adds 1 + 2 to equal 3', () => {
expect(1 + 2).toBe(3);
});
});
Run Your Tests
Execute your tests with:
npx jest
You should see output like:
PASS ./example.test.ts
sum module
✓ adds 1 + 2 to equal 3 (2 ms)
Troubleshooting
If you get type errors, ensure you installed @types/jest
. For other issues, check your Jest and TypeScript versions are compatible.
For more complex setups, you might need Webpack configuration.
Conclusion
ts-jest makes testing TypeScript code with Jest simple. Follow these steps to set it up in your Node.js project. Happy testing!