Jest Testing Configuration Reference

Type: Software Reference Confidence: 0.93 Sources: 7 Verified: 2026-02-28 Freshness: 2026-02-28

TL;DR

Constraints

Quick Reference

OptionDefaultPurpose
presetnoneBase config (ts-jest)
testEnvironment"node"Test runtime
transform{}File transformers
moduleNameMapper{}Path alias mapping
setupFilesAfterFramework[]Post-init setup
collectCoveragefalseCoverage collection
coverageThresholdnoneMin coverage %
testTimeout5000Default timeout (ms)
verbosefalseShow individual results
maxWorkers50%Parallel workers

Decision Tree

START
├── TypeScript? → ts-jest preset
├── React/DOM? → testEnvironment: 'jsdom'
├── ESM (type: module)? → ESM preset + --experimental-vm-modules
├── Need speed? → @swc/jest (3-5x faster)
├── Path aliases? → moduleNameMapper
└── DEFAULT → ts-jest + node environment

Step-by-Step Guide

1. Install Jest and TypeScript support

[src4]

npm install --save-dev jest ts-jest @types/jest
# For React: npm install --save-dev jest-environment-jsdom @testing-library/react @testing-library/jest-dom

2. Create jest.config.ts

[src1]

import type { Config } from 'jest';

const config: Config = {
  preset: 'ts-jest',
  testEnvironment: 'node',
  roots: ['<rootDir>/src'],
  testMatch: ['**/__tests__/**/*.test.ts', '**/*.spec.ts'],
  verbose: true,
};

export default config;

3. Configure path aliases

[src1]

moduleNameMapper: {
  '^@/(.*)$': '<rootDir>/src/$1',
},

4. Configure coverage

collectCoverage: true,
coverageThreshold: {
  global: { branches: 80, functions: 80, lines: 80, statements: 80 },
},

Code Examples

Node.js + TypeScript backend

import type { Config } from 'jest';

const config: Config = {
  preset: 'ts-jest',
  testEnvironment: 'node',
  roots: ['<rootDir>/src'],
  moduleNameMapper: { '^@/(.*)$': '<rootDir>/src/$1' },
  collectCoverage: true,
  coverageThreshold: { global: { branches: 80, functions: 80, lines: 80, statements: 80 } },
  clearMocks: true,
  restoreMocks: true,
};
export default config;

@swc/jest (faster transforms)

const config: Config = {
  testEnvironment: 'node',
  transform: {
    '^.+\\.(t|j)sx?$': ['@swc/jest', {
      jsc: { parser: { syntax: 'typescript', tsx: true } },
    }],
  },
};

Monorepo (project-based)

const config: Config = {
  projects: [
    { displayName: 'api', preset: 'ts-jest', testEnvironment: 'node', roots: ['<rootDir>/packages/api/src'] },
    { displayName: 'web', preset: 'ts-jest', testEnvironment: 'jsdom', roots: ['<rootDir>/packages/web/src'] },
  ],
};

Anti-Patterns

Wrong: Not installing jest-environment-jsdom

// ❌ BAD — jsdom NOT bundled since Jest 28
{ testEnvironment: 'jsdom' }  // Error: Cannot find module

Correct: Install separately

# ✅ GOOD
npm install --save-dev jest-environment-jsdom

Wrong: Not handling CSS/image imports

// ❌ BAD — Jest crashes on CSS imports
// No moduleNameMapper for static assets

Correct: Map static assets to mocks

// ✅ GOOD
moduleNameMapper: {
  '\\.(css|less|scss)$': 'identity-obj-proxy',
  '\\.(jpg|png|svg)$': '<rootDir>/__mocks__/fileMock.ts',
}

Common Pitfalls

Diagnostic Commands

# Show resolved config
npx jest --showConfig

# Run matching tests
npx jest --testPathPattern="auth"

# Watch mode
npx jest --watch

# Coverage report
npx jest --coverage

# List tests
npx jest --listTests

Version History & Compatibility

VersionStatusBreaking ChangesMigration Notes
Jest 30CurrentNode <18 dropped, jsdom 26, stricter TS typesCheck toHaveBeenCalledWith matching
Jest 29MaintenanceLast version supporting Node 16
Jest 28EOLjsdom extractednpm i jest-environment-jsdom

When to Use / When Not to Use

Use WhenDon't Use WhenUse Instead
Established codebase on JestNew Vite projectVitest
Need snapshot testingDeno projectDeno.test
React Testing LibraryBrowser-native testingPlaywright

Important Caveats

Related Units