module.exports = { preset: 'ts-jest', testEnvironment: 'jsdom', roots: ['/src'], testMatch: ['**/__tests__/**/*.test.{ts,tsx}', '**/*.test.{ts,tsx}'], // 仅收集实际可单元测试的目录的覆盖率,排除 pages/API routes/admin 等需集成/E2E 测试的代码 collectCoverageFrom: [ 'src/components/ui/**/*.{ts,tsx}', 'src/components/layout/**/*.{ts,tsx}', 'src/components/seo/**/*.{ts,tsx}', 'src/components/detail/**/*.{ts,tsx}', 'src/components/sections/**/*.{ts,tsx}', 'src/components/content/**/*.{ts,tsx}', 'src/hooks/**/*.{ts,tsx}', 'src/lib/**/*.ts', '!src/**/*.d.ts', '!src/**/*.stories.{ts,tsx}', '!src/**/__tests__/**', ], // 覆盖率阈值采用渐进提升策略:初始值设低于当前实测值,每完成一个迭代任务后提升 // 当前实测值: global(~17% stmts/~12% branches), hooks(~18%/~5%), lib(~33%/~10%), ui(~16%/~21%) coverageThreshold: { global: { branches: 10, functions: 10, lines: 15, statements: 14, }, './src/hooks/': { branches: 3, functions: 15, lines: 15, statements: 14, }, './src/lib/': { branches: 8, functions: 18, lines: 28, statements: 28, }, './src/components/ui/': { branches: 16, functions: 9, lines: 14, statements: 13, }, }, coverageReporters: ['text', 'lcov', 'html', 'json'], coverageDirectory: '/coverage', moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'], moduleNameMapper: { '^@/(.*)$': '/src/$1', }, transform: { // ts-jest 需要明确指定 jsx: 'react-jsx',因为项目 tsconfig 使用 'preserve' // 由后续 SWC/Babel 处理,但 Jest 环境下没有这些工具链 '^.+\\.(ts|tsx)$': ['ts-jest', { tsconfig: { jsx: 'react-jsx' } }], }, transformIgnorePatterns: [ 'node_modules/(?!(nanoid|next-auth|@auth)/)', ], setupFilesAfterEnv: ['/jest.setup.js'], testTimeout: 10000, verbose: true, maxWorkers: '50%', };