使用 babel、nyc 和 istanbul 插件从 Cypress 获取代码覆盖率?

Cor*_*lie 8 testing istanbul reactjs babeljs cypress

对于 React/Redux 项目,我使用 Cypress 来测试 UI。我需要从这些测试中获取代码覆盖率,以便在声纳中获得详细信息(但那是另一个故事了)。

所以我开始阅读 Cypress 的文档:https://docs.cypress.io/guides/tooling/code-coverage.html

我按照说明找到了不同的插件,但没有任何作用。运行赛普拉斯测试后,我没有覆盖结果,当我尝试使用转译中的检测代码运行服务器并尝试获取window.__coverage__它的undefined.

这是我的配置。

我的 devDep 和 NYC conf 在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
}
Run Code Online (Sandbox Code Playgroud)

我的.babelrc文件:

{
"presets": ["@babel/preset-react"],
"plugins": [
    [ "react-intl", {
        "messagesDir": "./src/messages",
        "extractSourceLocation": true
    }],
    "transform-class-properties",
    "istanbul"
],
"env": {
    "test": {
      "plugins": [ "transform-class-properties", "istanbul" ]
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

我的\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
}
Run Code Online (Sandbox Code Playgroud)

所以我希望得到像您在赛普拉斯文档中看到的结果。网站已生成,但包含覆盖率报告的文件为空。

而且我不明白我的配置出了什么问题。

小智 0

我也面临这个问题。无法获得代码覆盖率。nyc将版本从降级14.x.x13.x.x解决了该问题,如此处所述