4

I have been trying to get Cypress code coverage working with my Angular production project to no avail.

To try and help diagnose it, I have created a minimal implementation project to make sure I wasn't introducing anything weird in the production version, which I don't think I am as the same issue is still happening. It's starting to drive me mad!

I have used a few references and as far as I can see I have the things in place I need to:

As far as I can tell the Angular and Cypress side is all hooked up and am getting output in the .nyc_output folder and a coverage report. However the report is not indicating typescript line coverage or including those stats.

enter image description here enter image description here

I have seen this but didn't seem to help.

Code Instrumentation (webpack extension + angular.json):

module.exports = {
  module: {
    rules: [
      {
        test: /\.(js|ts)$/,
        loader: "istanbul-instrumenter-loader",
        options: { esModules: true },
        enforce: "post",
        include: require("path").join(__dirname, "..", "src"),
        exclude: [
          /\.(e2e|spec)\.ts$/,
          /node_modules/,
          /(ngfactory|ngstyle)\.js/,
        ],
      },
    ],
  },
};
"serve": {
  "builder": "ngx-build-plus:dev-server",
  "options": {
    "browserTarget": "architecture-testing:build",
    "extraWebpackConfig": "./cypress/coverage.webpack.js",
    "sourceMap": true
  },
  "configurations": {
    "production": {
      "browserTarget": "architecture-testing:build:production"
    }
  }
}

Cypress appears to be recording and saving coverage:

const registerCodeCoverageTasks = require("@cypress/code-coverage/task");

module.exports = (on, config) => {
  registerCodeCoverageTasks(on, config);

  return config;
};

enter image description here enter image description here

out.json appears to have the correct file and code mapping:

enter image description here enter image description here enter image description here:

package.json (nyc config + deps):

{
  "name": "architecture-testing",
  "version": "0.0.0",
  "scripts": {
    "postinstall": "ngcc",
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e",
    "precypress": "rimraf .nyc_output coverage",
    "cypress": "ng run architecture-testing:cypress-run",
    "cypress:open": "cypress open",
    "cypress:run": "cypress run"
  },
  "nyc": {
    "extends": "@istanbuljs/nyc-config-typescript",
    "all": true,
    "exclude": [
      "coverage/**",
      "cypress/**",
      "**/*.spec.ts"
    ]
  },
  "private": true,
  "dependencies": {
    "@angular/animations": "~9.1.9",
    "@angular/common": "~9.1.9",
    "@angular/compiler": "~9.1.9",
    "@angular/core": "~9.1.9",
    "@angular/forms": "~9.1.9",
    "@angular/platform-browser": "~9.1.9",
    "@angular/platform-browser-dynamic": "~9.1.9",
    "@angular/router": "~9.1.9",
    "rxjs": "~6.5.4",
    "tslib": "^1.10.0",
    "zone.js": "~0.10.2"
  },
  "devDependencies": {
    "@angular-devkit/build-angular": "~0.901.7",
    "@angular/cli": "~9.1.7",
    "@angular/compiler-cli": "~9.1.9",
    "@briebug/cypress-schematic": "^3.3.0",
    "@cypress/code-coverage": "^3.8.1",
    "@cypress/webpack-preprocessor": "5.4.1",
    "@istanbuljs/nyc-config-typescript": "^1.0.1",
    "@types/node": "^12.11.1",
    "codelyzer": "^5.1.2",
    "cypress": "^4.8.0",
    "istanbul-instrumenter-loader": "^3.0.1",
    "istanbul-lib-coverage": "^3.0.0",
    "ngx-build-plus": "^9.0.6",
    "nyc": "^15.1.0",
    "rimraf": "^3.0.2",
    "source-map-support": "^0.5.19",
    "ts-loader": "7.0.5",
    "ts-node": "^8.3.0",
    "tslint": "~6.1.0",
    "typescript": "~3.8.3"
  }
}

Spec file:

it('does something', () => {
  cy.visit('http://localhost:4200');
  cy.get('[data-cy=button-one]').click();
  cy.get('[data-cy=button-output]').should('have.text', 'you clicked button 1');
});

Sorry it's so long but I am stuck as to where to go next. Many thanks if you can point me in any direction.

Update based on answer investigation:

Looking at the past versions of the @cypress/code-coverage it appears the issue for me was introduced in v3.3.0 of the plugin. All the versions for v3.2.* were working for me when downgrading my minimal project. After looking at the documentation changes for v3.3.0 the key bit of information in the readme was:

**Note:** if you have `all: true` NYC option set, this plugin will check the produced `.nyc_output/out.json` before generating the final report. If the `out.json` file does not have information for some files that should be there according to `include` list, then an empty placeholder will be included, see [PR 208](https://github.com/cypress-io/code-coverage/pull/208).

My original nyc config was:

"nyc": {
  "extends": "@istanbuljs/nyc-config-typescript",
  "all": true,
  "exclude": [
    "coverage/**",
    "cypress/**",
    "**/*.spec.ts"
  ]
}

So for some reason even though I have metrics for the files that were being tested in out.json a second "empty placeholder" node was being created and overriding the subsequent report generation. I am guessing that this is potentially a bug or a problem with my setup again so will ask the creators.

I can now see coverage if I change my nyc config to:

"nyc": {
  "extends": "@istanbuljs/nyc-config-typescript",
  "all": true,
  "include": [
    "src/**/*.ts"
  ],
  "exclude": [
    "coverage/**",
    "cypress/**",
    "**/*.spec.ts"
  ]
}

This does mean that if I don't hit a file with testing it won't be included as an empty placeholder as "all": true is no longer present.

Looking at @briebug/cypress-schematic@3.3.0 this doesn't appear to be causing any issues (same happens without using their builder) but has been raised as one here and here.

4

2 回答 2

2

ang-cy-cov-example与您的 package.json 进行比较,主要区别在于他使用 @cypress/code-coverage@1.14.0,而您拥有最新的 v3.8.1。

更改回此 v1.14.0 可以在您的设置中正常工作。由于您的信息表明数据出现在 中.nyc_output/out.json,因此我使用命令行进行了测试,该命令行./node_modules/.bin/nyc report可在控制台中快速查看。

比较.nyc_output/out.json两个版本,各个节点在结构上是相同的,即具有正确的部分(路径、statementMap、inputSourceMap 等)。

有两种类型的附加节点

  • 其他文件,例如 karma.conf.js、coverage.webpack.js、cy-ts-preprocessor.js、integration/spec.ts、support/commands.ts - 我们不感兴趣。

  • 我们感兴趣的文件在文件末尾重复,但重复项没有覆盖率指标。

例如

带有指标的 main.ts 的第一个副本

  "path-to\\src\\main.ts": {
    "path": "path-to\\src\\main.ts",
    "statementMap": {
         ...
      },
      "1": {
         ...
      },
      "2": {
         ...
      }
    },
    "fnMap": {},
    "branchMap": {
         ...
    },
    "s": {
      "0": 1,   // indicates one visit to this statement
      "1": 0,
      "2": 1
    },
    "f": {},
    "b": {
      "0": [
        0,
        1
      ]
    },
    "inputSourceMap": {
         ...
    },
    "_coverageSchema": "332fd63041d2c1bcb487cc26dd0d5f7d97098a6c",
    "hash": "5959c383a9744c99a600a28ff82b12f0a540c5e6"
  },

没有指标的 main.ts 的第二个副本

  "path-to/src/main.ts": {
    "path": "path-to/src/main.ts",
    "statementMap": {},
    "fnMap": {},
    "branchMap": {},
    "s": {},          // no metrics recorded here
    "f": {},
    "b": {}
  },

因此,结论是纽约报告正在用第二个节点的空指标替换第一个节点指标。

我跳过了这些版本,v3.2.0 是我发现的最新版本。

添加节点模块时还要注意这个警告,但不能说它是否是一个促成因素。

警告“ > @briebug/cypress-schematic@3.3.0”具有不正确的对等依赖关系“cypress@^3.6.1”


故障点

基本问题在 task-utils.js 中。

参考获取所有文件

const allFiles = globby.sync(patterns, { absolute: true })

whereglobby即使在 Windows 上也返回带有正斜杠的路径

和参考得到coveredPaths

const coveredPaths = coverageKeys.map(key => nycCoverage[key].path)

在 Windows中,密钥已out.json用反斜杠保存在其中。

一个快速的解决办法是在这一点上规范化路径

const coveredPaths = coverageKeys.map(key => nycCoverage[key].path)
  .map(path => path.replace(/\\/g, '/'))  // Compare "normalized" paths 

修补 '/node_modules/@cypress/code-coverage/task-utils.js' 解决了这个问题。

于 2020-06-13T03:33:16.317 回答
2

这个补丁现在已经在 pull request 中应用了,看起来像 v3.8.6

Ref处理覆盖文件路径中的反斜杠(2020 年 12 月)。

这(部分)是链接所说的:

此 PR 包含在 3.8.6 版本中

该版本适用于:

npm 包 (@latest dist-tag)
GitHub 发布

于 2021-09-11T10:02:37.743 回答