英文:
Code coverage for cypress component tests in an Nx Angular Monorepo?
问题
我已经设置了一个带有Angular应用程序和两个库(mylib-a
和mylib-b
)的NX monorepo。每个库都有一个单独的组件(mylib-a
中的TitleWidget
和mylib-b
中的TitleWidgetB
)。每个这些组件都有自己的一组Cypress组件测试。我可以分别运行每个库的测试,但我正在努力弄清楚如何对我的源代码进行仪器化,并进一步获得两个组件的组合测试覆盖报告。
您可以在此处找到示例应用程序以及运行它的说明:https://github.com/rtm-celum/angular-cypress-component-test-coverage
我遵循了Cypress端到端代码覆盖指南(https://docs.cypress.io/guides/tooling/code-coverage#E2E-and-unit-code-coverage)并成功安装了插件以及相关的一切。
我还尝试了遵循https://nx.dev/packages/angular/generators/cypress-component-configuration,并通过cypress.config.ts
或我的应用程序project.json
文件中的目标添加仪器化设置(babel-loader
,babel-plugin-istanbul
)作为额外的webpack配置。
不管我尝试了什么,当我执行Cypress组件测试时,.nyc_output/out.json
始终为空{}
。
英文:
I've set up an NX monorepo with an Angular application and two libraries: mylib-a
and mylib-b
. Each library has a single component (TitleWidget
in mylib-a
and TitleWidgetB
in mylib-b
). Each of these components has its own set of Cypress component tests. I can run the tests for each library separately, but I'm struggling to figure out how to instrument my source code and further get a combined test coverage report for both components.
You can find the sample app and instructions on how to run it here: https://github.com/rtm-celum/angular-cypress-component-test-coverage
I followed the cypress E2E code coverage guide (https://docs.cypress.io/guides/tooling/code-coverage#E2E-and-unit-code-coverage) and managed to install the plugin and everything around it.
I also tried to follow https://nx.dev/packages/angular/generators/cypress-component-configuration and to add instrumentation settings (babel-loader
, babel-plugin-istanbul
) as additional webpack configs via cypress.config.ts
or via targets in my applications project.json
file.
No matter what I tried, when I execute the cypress component tests, the .nyc_output/out.json
always was empty {}
.
答案1
得分: 3
这里是翻译好的部分:
-
安装一些额外的依赖项:
npm i @cypress/code-coverage @jsdevtools/coverage-istanbul-loader -D
-
添加自定义的 webpack 配置到你的项目中:
import * as path from "path"; export const CoverageWebpack = { module: { rules: [ { test: /\.(js|ts)$/, loader: '@jsdevtools/coverage-istanbul-loader', options: { esModules: true }, enforce: 'post', include: [ path.join(__dirname, '../..', 'apps'), path.join(__dirname, '../..', 'libs') ], exclude: [ /\.(cy|spec)\.ts$/, /node_modules/ ] } ] } };
-
修改 cypress.config.ts 如下:
import { nxComponentTestingPreset } from '@nx/angular/plugins/component-testing'; import { defineConfig } from 'cypress'; import { CoverageWebpack } from './coverage.webpack'; const nxPreset = nxComponentTestingPreset(__filename); export default defineConfig({ component: { ...nxPreset, devServer: { ...nxPreset.devServer, webpackConfig: CoverageWebpack, }, setupNodeEvents(on, config) { // eslint-disable-next-line @typescript-eslint/no-var-requires require('@cypress/code-coverage/task')(on, config); // 包括任何其他插件代码... // 返回带有更改的环境变量的配置对象非常重要 return config; }, }, });
-
在 component.ts 中添加以下行(感谢 @Miguel):
import '@cypress/code-coverage/support';
这是关于 mylib-a 的结果:
覆盖率输出 HTML
不幸的是,Cypress 命令也会被列出。也许可以通过在 coverage.webpack.ts 中更改 include 或 exclude 来解决此问题。
希望我没有漏掉任何东西。(我克隆了你的项目并进行了修改。如果你为我创建一个分支,我可以将我的更改推送到其中,如果你愿意的话。)
英文:
I also had problems with the setup but finally I found a solution.
Here are the required steps:
- Install some more dependencies:
npm i @cypress/code-coverage @jsdevtools/coverage-istanbul-loader -D
- Add a custom webpack config to cour project
import * as path from "path"; export const CoverageWebpack = { module: { rules: [ { test: /\.(js|ts)$/, loader: '@jsdevtools/coverage-istanbul-loader', options: {esModules: true}, enforce: 'post', include: [ path.join(__dirname, '../..', 'apps'), path.join(__dirname, '../..', 'libs') ], exclude: [ /\.(cy|spec)\.ts$/, /node_modules/ ] } ] } };
- Modify the cypress.config.ts like this:
import {nxComponentTestingPreset} from '@nx/angular/plugins/component-testing'; import {defineConfig} from 'cypress'; import {CoverageWebpack} from './coverage.webpack' const nxPreset = nxComponentTestingPreset(__filename); export default defineConfig({ component: { ...nxPreset, devServer: { ...nxPreset.devServer, webpackConfig: CoverageWebpack, }, setupNodeEvents(on, config) { // eslint-disable-next-line @typescript-eslint/no-var-requires require('@cypress/code-coverage/task')(on, config) // include any other plugin code... // It's IMPORTANT to return the config object // with any changed environment variables return config; }, } });
- Add following line to component.ts (thx @Miguel)
import '@cypress/code-coverage/support'
Here is my result for mylib-a:
coverage output html
Unfortunately also the cypress commands will be listed. Maybe this can be solved by changing the include or exclude in the coverage.webpack.ts.
I hope I did not forget anything. (I cloned your project and modified it. If you create a branch for me I might push my changes inside it if you want.)
答案2
得分: 1
I've also been dealing with this issue.
@Butterbluemchen提供的步骤与我遵循的步骤类似,您可以在这里找到它们。
我认为缺少添加
import '@cypress/code-coverage/support'
在 cypress/support/component.js 中
不管我在这里提到的任何选项,我仍然无法获得正确的覆盖报告。
它标记的行与真实源代码不对应,而是与由Cypress执行的缩小或转译代码对应...
编辑:我找到了关于正确覆盖报告的问题。原因是NX为Cypress启动的devServer未使用soureMap。我只需修改 project.json
中 component-test
标记的 devServerTarget
选项以使用 development
配置。类似于:
"component-test": {
"executor": "@nx/cypress:cypress",
"options": {
"cypressConfig": "libs/mylib-a/cypress.config.ts",
"testingType": "component",
"skipServe": true,
"devServerTarget": "mylib-a:build:development"
}
}
英文:
I've also been dealing with this issue.
The steps provided by @Butterbluemchen are similar to the ones I followed, you can find them here.
I think it is missing adding
import '@cypress/code-coverage/support'
in cypress/support/component.js
Anyway following any option mentioned here I'm still not getting the coverage report correct.
It is marking lines that don't correspond with the real source code but with minified or transpiled code executed by Cypress...
EDIT: I found my problem with the correct coverage report. The reason was the devServer started by NX for Cypress was not using soureMap. I just modified the devServerTarget
option for the component-test
tag in project.json
to use the development
configuration. Something like:
"component-test": {
"executor": "@nx/cypress:cypress",
"options": {
"cypressConfig": "libs/mylib-a/cypress.config.ts",
"testingType": "component",
"skipServe": true,
"devServerTarget": "mylib-a:build:development"
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论