Files
novalon-website/config/test/jest.config.js
T
张翔 38be4a19ef test(core): 补充单元测试,修复 useCountUp 精度问题,新增项目文档
- 新增 hooks/components/lib 共 9 个测试文件,覆盖边界条件与异常路径
- 补充 animations.test.tsx 用例(RotatingBorder、CounterWithEffect 等)
- 修复 useCountUp 结束时 toFixed 精度问题
- 调整 jest 覆盖率配置为渐进式阈值,收缩收集范围
- 新增 docs/lessons-learned.md(经验教训汇总)与 docs/troubleshooting.md(问题排查索引)
- 更新 README.md 文档索引
2026-07-07 19:42:50 +08:00

67 lines
2.0 KiB
JavaScript

module.exports = {
preset: 'ts-jest',
testEnvironment: 'jsdom',
roots: ['<rootDir>/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: '<rootDir>/coverage',
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],
moduleNameMapper: {
'^@/(.*)$': '<rootDir>/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: ['<rootDir>/jest.setup.js'],
testTimeout: 10000,
verbose: true,
maxWorkers: '50%',
};