我的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中运行单独的测试时,由于日志没有运行其他测试,因此日志中充满了更多此类错误。
我该如何解决?谢谢。
PS。这是我的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"
]
}
答案 0 :(得分:0)
在使用jest version 24.1.0
和babel-jest version 23.6.0
时,我遇到了完全相同的问题。
通过将babel-jest
版本更新为24.0.0
来解决。
希望有帮助。