Set up Jest by installing it, creating jest.config.js with settings like testEnvironment, transform for TypeScript/Babel, setupFilesAfterEnv for test utilities, and coverage configuration. Integrate with React Testing Library for component tests.
Jest Setup:
Key Configuration:
With React:
// jest.config.js
module.exports = {
// Test environment
testEnvironment: 'jsdom',
// Setup files
setupFilesAfterEnv: ['<rootDir>/src/setupTests.js'],
// Transform TypeScript/JSX
transform: {
'^.+\\.(js|jsx|ts|tsx)$': 'babel-jest',
},
// Module aliases (match tsconfig paths)
moduleNameMapper: {
'^@/(.*)$': '<rootDir>/src/$1',
'\\.(css|less|scss)$': 'identity-obj-proxy',
},
// Coverage
collectCoverageFrom: [
'src/**/*.{js,jsx,ts,tsx}',
'!src/**/*.d.ts',
'!src/index.tsx',
],
coverageThreshold: {
global: {
branches: 80,
functions: 80,
lines: 80,
},
},
// Test patterns
testMatch: ['**/__tests__/**/*.[jt]s?(x)', '**/?(*.)+(spec|test).[jt]s?(x)'],
testPathIgnorePatterns: ['/node_modules/', '/dist/'],
};