TypeScript
Jest · Reference cheat sheet
TypeScript
Jest · Reference cheat sheet
📋 Overview
Run TypeScript tests with ts-jest, @swc/jest, or Babel. Keep tsconfig and Jest transforms aligned for path aliases and JSX.
🔧 Core concepts
| Approach | Notes |
|---|---|
ts-jest | Type-aware option; slower |
@swc/jest | Fast transpile |
babel-jest | Shared Babel pipeline |
| Types | @types/jest or @jest/globals |
Prefer transpile-only for speed; run tsc --noEmit separately in CI.
💡 Examples
ts-jest preset:
npm i -D ts-jest typescript @types/jest
npx ts-jest config:init// jest.config.js
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
roots: ['<rootDir>/src'],
};SWC:
transform: {
'^.+\\.(t|j)sx?$': ['@swc/jest', {
jsc: { parser: { syntax: 'typescript', tsx: true } },
}],
},Globals import (ESM-friendly):
import { describe, expect, test } from '@jest/globals';
test('typed', () => {
const n: number = 1;
expect(n).toBe(1);
});Path aliases: mirror paths from tsconfig in moduleNameMapper.
⚠️ Pitfalls
- Type errors only caught if
ts-jestdiagnostics enabled — don't assume. - JSX runtime mismatches (
reactvsreact-jsx). - Mixing ESM
ts-jestexperimental mode without Node flags. - Duplicate
@types/jestvs Vitest types in the same project. - Compiling tests into
distaccidentally via broadinclude.