赛普拉斯+伊斯坦堡无法获得代码覆盖率

时间:2019-09-10 19:10:52

标签: angular cypress istanbul nyc

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

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

enter image description here

以及运行.nyc_output

后在cypress:open中生成的覆盖率文件

enter image description here

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

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

3 个答案:

答案 0 :(得分:5)

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

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

答案 1 :(得分: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'))}

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

答案 2 :(得分:0)

使用@cypress/code-coverage,您必须知道该插件不会检测您的代码。您需要为代码工具使用 babel-plugin-istanbul 插件。

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

{
  "plugins": ["istanbul"]
}

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