6

我尝试使用Cypressistanbul nyc在Angular 8项目中设置代码覆盖率。

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

在此处输入图像描述

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

在此处输入图像描述

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

$ 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"]
}
4

4 回答 4

6

nyc何时excludeAfterRemap为真似乎是一个问题。

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

于 2019-09-10T20:49:38.260 回答
3

这是一步一步的详细描述。 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'))}

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

于 2019-09-10T20:09:05.243 回答
1

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

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

{
  "plugins": ["istanbul"]
}

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

于 2021-01-10T20:21:39.097 回答
1

这是我的建议:

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

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

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

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

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

  1. 不需要通天塔;不是每个人都使用它。除了 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
于 2021-12-15T21:59:23.400 回答