You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Jest单文件与全量测试覆盖率结果不一致问题求助

Jest 覆盖率随机不一致问题求助

单个测试文件单独运行时,能稳定拿到100%覆盖率;但执行全量测试命令 yarn test --coverage 时,覆盖率结果变得不可靠且无规律:已被测试覆盖的代码行,会在不同测试运行中随机显示为未覆盖。

问题表现出随机性,不同文件会“轮流”出现覆盖率缺失:某次运行显示文件A有未覆盖行,下一次可能变成文件B,完全没有固定规律。

相关配置文件

jest.config.ts

import type { Config } from 'jest';
import nextJest from 'next/jest.js';

const createJestConfig = nextJest({
  dir: './',
});

const config: Config = {
  coverageProvider: 'v8',
  testEnvironment: 'jsdom',
  clearMocks: true,
  restoreMocks: true,
  resetMocks: true,
  preset: 'ts-jest',
  testEnvironmentOptions: { url: 'http://localhost' },
  setupFilesAfterEnv: ['./jest.setup.tsx'],
  collectCoverageFrom: ['src/**/*.{ts,tsx}'],
  coveragePathIgnorePatterns: [
    '/node_modules/',
    'src/app/layout.tsx',
    'src/i18n/',
    'typings.d.ts',
    'stories.tsx',
    'sanityClient.ts',
    'src/app/stores/index',
  ],
  moduleNameMapper: {
    'lucide-react': require.resolve('lucide-react'),
    '^components/(.*)$': '<rootDir>/src/app/components/$1',
    '^stores$': '<rootDir>/src/app/stores',
    '^stores/(.*)$': '<rootDir>/src/app/stores/$1',
    '^designSystem/(.*)$': '<rootDir>/src/app/designSystem/$1',
    '^lib/(.*)$': '<rootDir>/src/app/lib/$1',
    '^pdp/(.*)$': '<rootDir>/src/app/pdp/$1',
    '^akills/(.*)$': '<rootDir>/src/app/skills/$1',
    '^home/(.*)$': '<rootDir>/src/app/home/$1',
    '^@/(.*)$': '<rootDir>/src/app/$1',
  },
};

export default createJestConfig(config);

jest.setup.tsx

import 'react';
import '@testing-library/jest-dom';

export const mockEmblaApi = {
  scrollPrev: jest.fn(),
  scrollNext: jest.fn(),
  scrollTo: jest.fn(),
  selectedScrollSnap: jest.fn().mockReturnValue(0),
  canScrollPrev: jest.fn().mockReturnValue(false),
  canScrollNext: jest.fn().mockReturnValue(false),
  on: jest.fn(),
  off: jest.fn(),
  reInit: jest.fn(),
};

export const updateMockEmblaApi = (updates: Partial<typeof mockEmblaApi>) => {
  Object.assign(mockEmblaApi, updates);
};
jest.mock('embla-carousel-react', () => {
  return {
    __esModule: true,
    default: () => [jest.requireActual('react').createRef(), mockEmblaApi],
  };
});

module.exports = { updateMockEmblaApi, mockEmblaApi };

const mockSanity = {
  fetch: jest.fn().mockResolvedValue([]),
};

jest.mock('./src/app/lib/sanityClient', () => ({ sanityClient: mockSanity }));

jest.mock('./src/app/stores', () => ({
  useCategoriesStore: jest.fn(),
  useSkillsStore: jest.fn(),
  useFiltersStore: jest.fn(),
  useSortStore: jest.fn(),
  useRefinementStore: jest.fn(),
  useSkillProductsStore: jest.fn(),
  usePortfolioSettingsStore: jest.fn(),
}));

jest.mock('next/navigation', () => ({
  ...jest.requireActual('next-router-mock/navigation'),
  notFound: jest.fn(),
}));

已尝试的解决方法

  • 在Jest配置中添加resetMocks: true,但引发React Hook错误
  • 尝试clearMocks及多种重置组合配置
  • 尝试隔离测试模块
  • 修复大量单个测试用例的潜在问题

希望能得到帮助诊断并修复该问题。

内容的提问来源于stack exchange,提问作者Ndx

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.12 09:34:51