Config
Jest · Reference cheat sheet
Config
Jest · Reference cheat sheet
📋 Overview
Configure Jest via jest.config.js/ts or package.json#jest. Key knobs: environment, transforms, roots, coverage, and setup files.
🔧 Core concepts
| Option | Purpose |
|---|---|
testEnvironment | node | jsdom |
roots / testMatch | Discovery |
transform | TS/JS compile |
moduleNameMapper | Aliases / CSS mocks |
setupFilesAfterEnv | Matchers, RTL |
collectCoverageFrom | Coverage scope |
clearMocks | Auto clear between tests |
💡 Examples
jest.config.js:
/** @type {import('jest').Config} */
module.exports = {
testEnvironment: 'node',
roots: ['<rootDir>/src'],
testMatch: ['**/__tests__/**/*.test.ts'],
clearMocks: true,
collectCoverageFrom: ['src/**/*.{ts,tsx}', '!src/**/*.d.ts'],
moduleNameMapper: {
'^@/(.*)$': '<rootDir>/src/$1',
'\\.(css|less)$': 'identity-obj-proxy',
},
setupFilesAfterEnv: ['<rootDir>/jest.setup.ts'],
};React / jsdom:
testEnvironment: 'jsdom',package.json scripts:
{
"scripts": {
"test": "jest",
"test:ci": "jest --ci --coverage --maxWorkers=2"
}
}CLI overrides:
npx jest --config=jest.config.js
npx jest --env=jsdom
npx jest --runInBand⚠️ Pitfalls
- Path aliases in TS not mirrored in
moduleNameMapper. - Transforming
node_modulesaccidentally (slow) or not transforming ESM deps. - Multiple configs in monorepos without projects/
--selectProjects. jsdomfor pure Node libraries — unnecessary overhead.- Coverage thresholds failing CI without local awareness.