【问题标题】:Cannot get code coverage with Cypress + Istanbul无法使用 Cypress + Istanbul 获得代码覆盖率
【发布时间】:2020-01-12 13:45:10
【问题描述】:

我尝试使用 Cypressistanbul nycAngular 8 项目中设置代码覆盖率。 p>

我设法检测了代码(全局变量 __coverage__ 已正确设置):

以及运行cypress:open后在.nyc_output中生成的覆盖文件

但是生成的覆盖率报告是空的:

$ cat coverage/coverage-final.json
{}

执行命令时结果相同:

$ npx nyc report --report-dir ./coverage --temp-dir .nyc_output --reporter=text
----------|----------|----------|----------|----------|-------------------|
File      |  % Stmts | % Branch |  % Funcs |  % Lines | Uncovered Line #s |
----------|----------|----------|----------|----------|-------------------|
All files |        0 |        0 |        0 |        0 |                   |
----------|----------|----------|----------|----------|-------------------|

这是我的 package.json devDependencies:

"devDependencies": {
  "@angular-devkit/build-angular": "^0.803.3",
  "@angular-devkit/build-optimizer": "^0.803.3",
  "@angular/cli": "^8.3.3",
  "@angular/compiler-cli": "8.2.5",
  "@angular/language-service": "8.2.5",
  "@briebug/cypress-schematic": "^2.0.0",
  "@cypress/code-coverage": "^1.10.1",
  "@cypress/webpack-preprocessor": "^4.1.0",
  "@istanbuljs/nyc-config-typescript": "^0.1.3",
  "@types/jasmine": "^3.4.0",
  "@types/jasminewd2": "^2.0.6",
  "@types/node": "^12.7.4",
  "babel-plugin-istanbul": "^5.2.0",
  "codelyzer": "^5.1.0",
  "cypress": "^3.4.1",
  "istanbul-instrumenter-loader": "^3.0.1",
  "istanbul-lib-coverage": "^2.0.5",
  "jasmine-core": "^3.4.0",
  "jasmine-spec-reporter": "4.2.1",
  "karma": "^4.3.0",
  "karma-chrome-launcher": "^3.1.0",
  "karma-cli": "^2.0.0",
  "karma-coverage-istanbul-reporter": "^2.1.0",
  "karma-jasmine": "^2.0.1",
  "karma-jasmine-html-reporter": "^1.4.2",
  "mochawesome": "^4.1.0",
  "ngx-build-plus": "^8.1.4",
  "nyc": "^14.1.1",
  "protractor": "^5.4.2",
  "protractor-html-reporter-2": "^1.0.4",
  "protractor-http-client": "^1.0.4",
  "source-map-support": "^0.5.13",
  "ts-node": "^8.3.0",
  "tslib": "^1.10.0",
  "tslint": "^5.19.0",
  "typescript": "3.5.3"
}

还有我的.nycrc.json

{
    "cache": false,
    "extension": [
      ".ts",
      ".tsx"
    ],
    "exclude": [
      "**/*.d.ts",
      "coverage/**",
      "packages/*/test/**",
      "test/**",
      "test{,-*}.ts",
      "**/*{.,-}{test,spec}.ts",
      "**/__tests__/**",
      "**/node_modules/**"
    ],
    "all": true,
    "check-coverage": true,
    "require": [
      "ts-node/register"
    ],
    "temp-directory": ".nyc_output",
    "sourceMap": false,
    "instrument": false,
    "include": ["src/**/*.ts", "src/**/*.tsx"]
}

【问题讨论】:

  • 您在支持文件中有此代码吗? // cypress/support/index.js 导入'@cypress/code-coverage/support'
  • @N.. 不抱歉
  • 对不起,我误解了这个问题(在编辑之前看到了它)。是的,我确实修改了支持和插件文件,您可以看到任务已执行(resetCoverage,combineCoverage,coverageReport)
  • 问题是 NYC 没有生成正确的输出

标签: angular cypress istanbul nyc


【解决方案1】:

excludeAfterRemap 为真时,这似乎是nyc 中的一个问题。

将其设置为 false 可以解决问题。

【讨论】:

    【解决方案2】:

    这里是一步一步的详细描述。 https://docs.cypress.io/guides/tooling/code-coverage.html#E2E-code-coverage

    我认为您在 index.js 中缺少代码覆盖/支持文件请遵循更改并运行。我觉得你应该很好。

        // cypress/support/index.js
           import '@cypress/code-coverage/support'
    
       // cypress/plugins/index.js   
          module.exports = (on, config) => {
          on('task', require('@cypress/code-coverage/task'))}
    

    当您现在运行赛普拉斯测试时,您应该会在测试完成后看到一些命令。我们在下方使用绿色矩形突出显示了这些命令。

    【讨论】:

    • 感谢您的回复,但是我确实有您在问题中看到的命令(第一个屏幕截图)
    • 我错过的另一件事是 - 返回配置。 docs.cypress.io/guides/tooling/code-coverage#Install-the-plugin module.exports = (on, config) => {require('@cypress/code-coverage/task')(on, config); /*其他东西;*/返回配置;};
    • 还有一个隐藏的问题。我正在使用打字稿。我在我的一个示例中意识到我的第一页 - src/index.js 是扩展名 .js。有一天我不明白为什么我的报道没有被渲染。当我将 index.js 更改为 index.tsx 时,覆盖范围随即被渲染。
    【解决方案3】:

    这是我的建议:

    1. 首先,确保您的测试针对您的本地服务实例运行。

      示例:ng serve --configuration=demo & cypress run --headed ...

    2. 根据我的经验,.nycrc.json 并不是真正需要的。

    3. 你的 package.json 结尾需要这个:

    “纽约”:{ “报告目录”:“覆盖 e2e” }

    1. Babel 不是必需的;不是每个人都使用它。除了 Babel,Webpack 构建配置也可以工作:./cypress/coverage.webpack.js

    最后,你应该知道:

    您可以检测您的服务器,然后使用以下命令生成报告:

    ## first, instrument your server locally
    nyc --silent ng serve
    
    { then manually test your app or run automation against it }
    
    ## then generate your report from the json files that were generated above
    nyc report --reporter html --reporter text  --report-dir ./cov-e2e/summary
    

    【讨论】:

      【解决方案4】:

      使用@cypress/code-coverage,您必须知道该插件不会检测您的代码。您可以为此目的使用 instanbul。 比如这个插件babel-plugin-istanbul可以帮你实现。

      如果您已经在使用 babel,则需要将插件添加到您的 .babelrc 中,如下所示:

      {
        "plugins": ["istanbul"]
      }
      

      更多信息请查看https://github.com/cypress-io/code-coverage#instrument-your-application

      【讨论】:

      • 不正确,您不必使用 babel 来执行此操作。答案具有误导性。
      • 嘿@djangofan,我更新了我的答案。感谢您指出这一点。
      猜你喜欢
      • 2019-12-14
      • 2019-03-15
      • 2016-10-19
      • 2018-09-13
      • 2016-02-29
      • 2015-09-10
      • 2016-05-23
      • 1970-01-01
      相关资源
      最近更新 更多