【问题标题】:Jest Testing Coverage - Errors When Files Don't Have TestsJest 测试覆盖率 - 文件没有测试时的错误
【发布时间】:2021-05-14 16:18:31
【问题描述】:

我的 React 项目中有 Jest 测试,但我还没有覆盖所有文件。但是,我收到以下错误:

Failed to collect coverage from  /home/user/Development/VideoManager/VideoManagerClient/src/components/AppContent/VideoListLayout/VideoListLayout.js
ERROR: Cannot read property 'coverageData' of null
STACK: TypeError: Cannot read property 'coverageData' of null

这里提到的文件还没有为它创建单元测试。我希望它被简单地跟踪为缺乏覆盖率,从而降低我的整个覆盖率统计数据,而不是抛出错误。

此外,当我在我的 IDE 中运行单个测试时,我的日志中会出现更多此类错误,因为它没有运行其他测试。

我该如何解决这个问题?

附言。这是我的 Jest 配置:

"jest": {
    "collectCoverage": true,
    "coverageReporters": [
      "html"
    ],
    "coverageThreshold": {
      "global": {
        "branches": 80,
        "functions": 80,
        "lines": 80,
        "statements": 80
      }
    },
    "collectCoverageFrom": [
      "src/**/*.{js,jsx,ts,tsx}",
      "!src/**/*.d.ts",
      "!src/serviceWorker.js"
    ],
    "resolver": "jest-pnp-resolver",
    "setupFiles": [
      "react-app-polyfill/jsdom"
    ],
    "setupFilesAfterEnv": [
      "<rootDir>/test/setupTests.js"
    ],
    "testMatch": [
      "<rootDir>/src/**/__tests__/**/*.{js,jsx,ts,tsx}",
      "<rootDir>/src/**/?(*.)(spec|test).{js,jsx,ts,tsx}",
      "<rootDir>/test/**/*.{js,jsx,ts,tsx}"
    ],
    "testPathIgnorePatterns": [
      "<rootDir>/test/setupTests.js",
      "<rootDir>/test/mock/.*",
      "<rootDir>/test/exclude/.*"
    ],
    "testEnvironment": "jsdom",
    "testURL": "http://localhost",
    "transform": {
      "^.+\\.(js|jsx|ts|tsx)$": "<rootDir>/node_modules/babel-jest",
      "^.+\\.css$": "<rootDir>/config/jest/cssTransform.js",
      "^(?!.*\\.(js|jsx|ts|tsx|css|json)$)": "<rootDir>/config/jest/fileTransform.js"
    },
    "transformIgnorePatterns": [
      "[/\\\\]node_modules[/\\\\].+\\.(js|jsx|ts|tsx)$",
      "^.+\\.module\\.(css|sass|scss)$"
    ],
    "moduleNameMapper": {
      "^react-native$": "react-native-web",
      "^.+\\.module\\.(css|sass|scss)$": "identity-obj-proxy",
      "\\.(css|sass|scss)$": "identity-obj-proxy"
    },
    "moduleFileExtensions": [
      "web.js",
      "js",
      "web.ts",
      "ts",
      "web.tsx",
      "tsx",
      "json",
      "web.jsx",
      "jsx",
      "node"
    ]
  }

【问题讨论】:

    标签: jestjs


    【解决方案1】:

    我在使用 jest version 24.1.0babel-jest version 23.6.0 时遇到了完全相同的问题。

    通过将babel-jest 版本更新为24.0.0 解决了这个问题。

    希望对你有帮助。

    【讨论】:

      猜你喜欢
      • 2020-06-03
      • 2019-06-11
      • 1970-01-01
      • 1970-01-01
      • 2023-03-09
      • 2015-04-01
      • 2018-08-06
      • 2019-04-08
      • 1970-01-01
      相关资源
      最近更新 更多