对于React/Redux项目,我使用Cypress测试UI。我需要从这些测试中获得代码覆盖率,才能在Sonar中获得详细信息(但这是另一个故事)。
所以我开始阅读Cypress的文档:https://docs.cypress.io/guides/tooling/code-coverage.html
我按照指示,发现不同的插件,但没有任何工作。在运行Cypress测试之后,我没有覆盖结果,当我尝试使用跨溢出的工具代码运行服务器并尝试获得window.__coverage__时,它是undefined。
这是我的配置。
我的devDep和纽约的package.json公司
"devDependencies": {
"@cypress/code-coverage": "^1.8.0",
"babel-cli": "^6.26.0",
"babel-plugin-istanbul": "^5.2.0",
"babel-plugin-react-intl": "^2.4.0",
"babel-plugin-transform-class-properties": "^6.24.1",
"babel-preset-react-app": "^3.1.2",
"cross-env": "^5.2.0",
"cypress": "3.1.0",
"istanbul-lib-coverage": "^2.0.5",
"jest-junit-reporter": "^1.1.0",
"junit": "^1.4.9",
"nyc": "^14.1.1",
"react-test-renderer": "^16.3.0"
[...]
},
"scripts": {
"start": "react-scripts start",
[...]
"test:unit:local": "react-scripts test --env=jsdom .*_test.js",
"test:unit": "npm run test:unit:local -- --ci --coverage --testResultsProcessor ./node_modules/jest-junit-reporter",
"test:integ:local": "cypress run --reporter junit",
"test:integ": "start-server-and-test start http://localhost:3000 test:integ:local",
"test": "cross-env NODE_ENV=test && npm run test:unit && npm run test:integ"
},
"nyc": {
"sourceMap": false,
"instrument": false
}我的.babelrc文件:
{
"presets": ["@babel/preset-react"],
"plugins": [
[ "react-intl", {
"messagesDir": "./src/messages",
"extractSourceLocation": true
}],
"transform-class-properties",
"istanbul"
],
"env": {
"test": {
"plugins": [ "transform-class-properties", "istanbul" ]
}
}
}我的\cypress\support\index.js包含以下一行:
import '@cypress/code-coverage/support'
这是我的\cypress\plugins\index.js
module.exports = (on, config) => {
on('task', require('@cypress/code-coverage/task'))
on('file:preprocessor', require('@cypress/code-coverage/use-babelrc'))
return config
}所以我希望有一个结果,就像你在Cypress文档上看到的那样。生成该网站,但包含覆盖报告的文件为空。
我不明白我的配置有什么问题。
发布于 2021-07-23 08:01:19
试试下面的一行:
"start": "react-scripts -r @cypress/instrument-cra start",发布于 2019-12-07 12:42:07
我也面临着这个问题。无法获得代码覆盖。将nyc版本从14.x.x降级为13.x.x解决了问题,就像提到的这里一样。
https://stackoverflow.com/questions/57391598
复制相似问题