ts-jest
A Jest transformer with source map support that lets you use Jest to test projects written in TypeScript.
Top Related Projects
Node.js test runner that lets you develop with confidence 🚀
Delightful JavaScript Testing.
☕️ simple, flexible, fun javascript test framework for node.js & the browser
Simple JavaScript testing framework for browsers and node.js
Fast, easy and reliable testing for anything that runs in a browser.
Next generation testing framework powered by Vite.
Quick Overview
ts-jest is a TypeScript preprocessor with source map support for Jest, a popular JavaScript testing framework. It allows developers to write and run tests for their TypeScript-based projects using the familiar Jest syntax and ecosystem.
Pros
- Seamless TypeScript Integration: ts-jest seamlessly integrates TypeScript into the Jest testing environment, allowing developers to write tests in TypeScript and leverage the full power of the TypeScript language.
- Source Map Support: ts-jest provides source map support, which enables better error reporting and debugging experience when running tests.
- Jest Ecosystem Compatibility: By using ts-jest, developers can take advantage of the extensive Jest ecosystem, including a wide range of assertion libraries, coverage tools, and other plugins.
- Flexible Configuration: ts-jest offers a flexible configuration system, allowing developers to customize the TypeScript compilation process to fit their project's needs.
Cons
- Potential Performance Impact: Depending on the size and complexity of the project, the additional processing required by ts-jest may result in slower test execution times compared to running pure JavaScript tests.
- Dependency on Jest: ts-jest is tightly coupled with the Jest testing framework, which means that developers who prefer other testing frameworks may not be able to use it.
- Potential Compatibility Issues: As both Jest and TypeScript evolve, there may be occasional compatibility issues that require updates to ts-jest to maintain full functionality.
- Learning Curve: Developers new to TypeScript or Jest may need to invest time in learning the configuration and usage of ts-jest, which can add to the project's setup complexity.
Code Examples
Here are a few examples of how to use ts-jest in a TypeScript project:
- Basic Configuration:
// jest.config.js
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
};
- Custom TypeScript Compiler Options:
// jest.config.js
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
globals: {
'ts-jest': {
tsconfig: 'tsconfig.json',
},
},
};
- Mocking with ts-jest:
// __tests__/myModule.test.ts
import { myFunction } from '../myModule';
jest.mock('../myModule', () => ({
myFunction: jest.fn().mockReturnValue(42),
}));
test('myFunction returns 42', () => {
expect(myFunction()).toBe(42);
});
- Snapshot Testing with ts-jest:
// __tests__/myComponent.test.tsx
import React from 'react';
import { render } from '@testing-library/react';
import MyComponent from '../MyComponent';
test('MyComponent matches snapshot', () => {
const { container } = render(<MyComponent />);
expect(container).toMatchSnapshot();
});
Getting Started
To get started with ts-jest, follow these steps:
- Install the required dependencies:
npm install --save-dev ts-jest @types/jest
- Create a
jest.config.js
file in the root of your project and configure ts-jest:
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
};
- Create a TypeScript test file (e.g.,
__tests__/myModule.test.ts
) and write your tests:
import { myFunction } from '../myModule';
test('myFunction returns a number', () => {
expect(typeof myFunction()).toBe('number');
});
- Run your tests using the Jest CLI:
npx jest
That's it! You can now write and run TypeScript tests using ts-jest and the Jest testing framework.
Competitor Comparisons
Node.js test runner that lets you develop with confidence 🚀
Pros of AVA
- Simpler, more minimalist testing framework with a focus on speed and concurrency
- Built-in support for ES6 and async/await without additional configuration
- Isolated test file execution, preventing global state pollution
Cons of AVA
- Less extensive ecosystem and plugin support compared to Jest (which ts-jest extends)
- Steeper learning curve for developers familiar with more traditional testing frameworks
- Limited built-in mocking capabilities
Code Comparison
AVA:
import test from 'ava';
test('my test', t => {
t.is(1 + 1, 2);
});
ts-jest (with Jest):
describe('my test suite', () => {
it('my test', () => {
expect(1 + 1).toBe(2);
});
});
Key Differences
- AVA uses a more compact syntax with implicit assertions
- ts-jest (with Jest) follows a more traditional describe/it structure
- AVA tests run concurrently by default, while Jest tests run sequentially unless configured otherwise
- ts-jest provides seamless TypeScript integration for Jest, while AVA requires additional setup for TypeScript support
Both tools are powerful testing solutions, but they cater to different preferences and project requirements. AVA is ideal for developers who prefer a minimalist, fast testing approach, while ts-jest is better suited for projects already using Jest or requiring extensive TypeScript integration.
Delightful JavaScript Testing.
Pros of Jest
- Broader ecosystem and community support
- Built-in code coverage and mocking capabilities
- Supports various JavaScript frameworks out-of-the-box
Cons of Jest
- Requires additional configuration for TypeScript projects
- Slower compilation times for TypeScript files
- May encounter compatibility issues with certain TypeScript features
Code Comparison
Jest configuration for TypeScript:
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
};
ts-jest configuration:
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
};
Summary
Jest is a comprehensive JavaScript testing framework with broad support and built-in features. However, when working with TypeScript projects, it may require additional setup and face some performance issues.
ts-jest, on the other hand, is specifically designed for TypeScript projects and integrates seamlessly with Jest. It provides better TypeScript support out-of-the-box and can offer improved performance for TypeScript-heavy projects.
The choice between Jest and ts-jest depends on the project's specific needs. For pure JavaScript projects or those with minimal TypeScript usage, Jest might be sufficient. For projects heavily relying on TypeScript, ts-jest could provide a more streamlined experience.
☕️ simple, flexible, fun javascript test framework for node.js & the browser
Pros of Mocha
- More flexible and framework-agnostic, supporting various assertion libraries
- Extensive ecosystem with plugins and extensions
- Simpler setup for non-TypeScript projects
Cons of Mocha
- Requires additional configuration for TypeScript support
- Slower test execution compared to ts-jest
- Less integrated with TypeScript's type-checking capabilities
Code Comparison
Mocha test example:
describe('Array', function() {
describe('#indexOf()', function() {
it('should return -1 when the value is not present', function() {
assert.equal([1, 2, 3].indexOf(4), -1);
});
});
});
ts-jest test example:
describe('Array', () => {
describe('#indexOf()', () => {
it('should return -1 when the value is not present', () => {
expect([1, 2, 3].indexOf(4)).toBe(-1);
});
});
});
Both ts-jest and Mocha are popular testing frameworks, but they serve different purposes. ts-jest is specifically designed for TypeScript projects using Jest, offering seamless integration with TypeScript's type system and faster execution. Mocha, on the other hand, is more versatile and can be used with various assertion libraries and programming languages, making it a good choice for projects with diverse requirements or those not primarily using TypeScript.
Simple JavaScript testing framework for browsers and node.js
Pros of Jasmine
- Standalone testing framework with built-in assertion library
- Simpler setup process, no additional configuration required
- Supports both synchronous and asynchronous testing out of the box
Cons of Jasmine
- Limited TypeScript support compared to ts-jest
- Lacks advanced features like code coverage reporting and snapshot testing
- May require additional plugins or tools for more complex testing scenarios
Code Comparison
Jasmine:
describe('Calculator', () => {
it('should add two numbers', () => {
expect(add(2, 3)).toBe(5);
});
});
ts-jest:
import { add } from './calculator';
test('should add two numbers', () => {
expect(add(2, 3)).toBe(5);
});
Summary
Jasmine is a standalone testing framework that offers simplicity and ease of use, making it suitable for smaller projects or those new to testing. It provides a built-in assertion library and supports both synchronous and asynchronous testing without additional configuration.
ts-jest, on the other hand, is specifically designed for TypeScript projects using Jest as the testing framework. It offers better TypeScript integration, advanced features like code coverage reporting and snapshot testing, and seamless integration with the Jest ecosystem.
While Jasmine may be easier to set up initially, ts-jest provides more robust TypeScript support and additional features that can be beneficial for larger or more complex projects.
Fast, easy and reliable testing for anything that runs in a browser.
Pros of Cypress
- All-in-one testing framework with built-in assertion library and mocking capabilities
- Provides a real-time, interactive test runner with time-travel debugging
- Offers automatic waiting and retry-ability, reducing flaky tests
Cons of Cypress
- Limited to testing web applications, unlike ts-jest which can test any TypeScript code
- Steeper learning curve due to its unique API and testing approach
- Can be slower for large test suites compared to ts-jest
Code Comparison
ts-jest example:
import { sum } from './math';
test('adds 1 + 2 to equal 3', () => {
expect(sum(1, 2)).toBe(3);
});
Cypress example:
describe('Math operations', () => {
it('adds 1 + 2 to equal 3', () => {
cy.window().then((win) => {
expect(win.sum(1, 2)).to.equal(3);
});
});
});
Key Differences
- ts-jest focuses on unit testing TypeScript code, while Cypress is primarily for end-to-end testing of web applications
- Cypress provides a more visual and interactive testing experience
- ts-jest integrates seamlessly with Jest, offering a wide range of testing utilities and matchers
Use Cases
- Choose ts-jest for unit testing TypeScript projects and when integration with Jest is desired
- Opt for Cypress when conducting comprehensive end-to-end testing of web applications, especially for complex user interactions
Next generation testing framework powered by Vite.
Pros of Vitest
- Faster execution due to native ESM support and parallelization
- Built-in TypeScript support without additional configuration
- Simpler setup and configuration process
Cons of Vitest
- Less mature ecosystem compared to Jest and ts-jest
- May lack some advanced features or plugins available in the Jest ecosystem
- Potential compatibility issues with existing Jest-based test suites
Code Comparison
ts-jest configuration:
{
"jest": {
"preset": "ts-jest",
"testEnvironment": "node"
}
}
Vitest configuration:
// vite.config.js
import { defineConfig } from 'vite'
export default defineConfig({
test: {
// Vitest options
}
})
Vitest offers a more streamlined configuration process, often requiring less setup than ts-jest. While ts-jest requires additional configuration in the Jest setup, Vitest integrates TypeScript support out of the box.
Both tools provide excellent TypeScript support for testing, but Vitest's modern architecture and native ESM support give it an edge in terms of performance. However, ts-jest benefits from the mature Jest ecosystem and wider adoption, which may be crucial for some projects.
The choice between these tools depends on project requirements, existing infrastructure, and the need for specific Jest features or plugins.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
ts-jest
A Jest transformer with source map support that lets you use Jest to test projects written in TypeScript.
It supports all features of TypeScript including type-checking. Read more about Babel7 + preset-typescript
vs TypeScript (and ts-jest
).
We are not doing semantic versioning and 23.10 is a re-write, run npm i -D ts-jest@"<23.10.0" to go back to the previous version |
---|
View the online documentation (usage & technical)
Ask for some help in the Jest
Discord community or ts-jest
GitHub Discussion
Before reporting any issues, be sure to check the troubleshooting page
We're looking for collaborators! Want to help improve ts-jest
?
Getting Started
These instructions will get you setup to use ts-jest
in your project. For more detailed documentation, please check online documentation.
using npm | using yarn | |
---|---|---|
Prerequisites | npm i -D jest typescript | yarn add --dev jest typescript |
Installing | npm i -D ts-jest @types/jest | yarn add --dev ts-jest @types/jest |
Creating config | npx ts-jest config:init | yarn ts-jest config:init |
Running tests | npm test or npx jest | yarn test or yarn jest |
Built With
- TypeScript - JavaScript that scales
- Jest - Delightful JavaScript Testing
ts-jest
- Jest transformer for TypeScript (yes,ts-jest
uses itself for its tests)
Contributing
Please read CONTRIBUTING.md for details on our code of conduct, and the process for submitting pull requests to us.
Versioning
We DO NOT use SemVer for versioning. Though you can think about SemVer when reading our version, except our major number follows the one of Jest. For the versions available, see the tags on this repository.
Authors/maintainers
- Kulshekhar Kabra - kulshekhar
- Gustav Wengel - GeeWee
- Ahn - ahnpnl
- Huafu Gandon - huafu
See also the list of contributors who participated in this project.
Supporters
- JetBrains has been kind enough to support ts-jest with an open source license.
License
This project is licensed under the MIT License - see the LICENSE.md file for details
Top Related Projects
Node.js test runner that lets you develop with confidence 🚀
Delightful JavaScript Testing.
☕️ simple, flexible, fun javascript test framework for node.js & the browser
Simple JavaScript testing framework for browsers and node.js
Fast, easy and reliable testing for anything that runs in a browser.
Next generation testing framework powered by Vite.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot