乐闻世界logo
搜索文章和话题

How to configure Jest? What are the common configuration options?

2月21日 15:54

Jest provides multiple configuration methods to customize test behavior:

1. package.json Configuration:

json
{ "jest": { "testEnvironment": "node", "coverageThreshold": { "global": { "branches": 80, "functions": 80, "lines": 80, "statements": 80 } } } }

2. jest.config.js Configuration File:

javascript
module.exports = { testEnvironment: 'jsdom', setupFilesAfterEnv: ['<rootDir>/jest.setup.js'], moduleNameMapper: { '^@/(.*)$': '<rootDir>/src/$1', '\\.(css|less|scss)$': 'identity-obj-proxy' }, collectCoverageFrom: [ 'src/**/*.{js,jsx,ts,tsx}', '!src/**/*.d.ts', '!src/**/*.stories.{js,jsx,ts,tsx}' ], testMatch: [ '**/__tests__/**/*.{js,jsx,ts,tsx}', '**/*.{spec,test}.{js,jsx,ts,tsx}' ], transform: { '^.+\\.(js|jsx|ts|tsx)$': 'babel-jest' } };

Common Configuration Options:

  • testEnvironment: Test environment (node, jsdom)
  • setupFilesAfterEnv: Test environment setup file
  • moduleNameMapper: Module path mapping
  • collectCoverageFrom: Coverage collection scope
  • testMatch: Test file matching patterns
  • transform: File transformation configuration
  • coverageThreshold: Coverage thresholds
  • testTimeout: Test timeout duration

Command Line Options:

  • jest --watch: Watch mode
  • jest --coverage: Generate coverage report
  • jest --updateSnapshot: Update snapshots
  • jest --verbose: Verbose output
标签:Jest