Angular 6 代码覆盖率...如何在应用程序的代码覆盖率中排除本地库?

Posted

技术标签:

【中文标题】Angular 6 代码覆盖率...如何在应用程序的代码覆盖率中排除本地库?【英文标题】:Angular 6 Code Coverage... how to exclude local library in code coverage of application? 【发布时间】:2019-01-23 19:10:35 【问题描述】:

我有一个具有以下基本结构的 Angular 工作区:

/projects
    -- /admin <-- angular application
    -- /core <-- library

这些是通过运行ng generate application adminng generate library core...相当直接生成的。

在运行ng test core 时,核心库的覆盖率约为 90%。

当运行ng test admin 时,管理应用程序应该有大约 90% 的覆盖率,但覆盖率报告包括核心文件(没有相应的测试)。示例:

------------------------------------------|----------|----------|----------|----------|-------------------|
File                                      |  % Stmts | % Branch |  % Funcs |  % Lines | Uncovered Line #s |
------------------------------------------|----------|----------|----------|----------|-------------------|
All files                                 |    68.08 |    58.27 |    40.72 |    68.74 |                   |
 admin/src                         |      100 |      100 |      100 |      100 |                   |
  polyfills.ts                            |      100 |      100 |      100 |      100 |                   |
  test.ts                                 |      100 |      100 |      100 |      100 |                   |
 admin/src/app                     |    94.55 |    70.69 |    83.33 |    97.78 |                   |
  app.component.ts                        |       95 |    62.07 |      100 |      100 |         3,4,5,6,9 |
  app.testing.module.ts                   |    94.29 |    79.31 |       75 |    96.67 |                17 |
 admin/src/app/configuration       |    59.57 |    54.55 |    30.77 |    62.62 |                   |
  configuration.actions.ts                |    69.66 |      100 |    51.61 |    69.66 |... 24,125,132,133 |
  configuration.effects.ts                |    47.54 |     69.7 |    11.54 |    55.56 |... 45,47,52,53,55 |
  configuration.reducer.ts                |    36.36 |    20.83 |     8.33 |    39.02 |... 71,74,76,87,90 |
  configuration.service.ts                |    83.33 |    62.07 |    44.44 |    83.87 |    26,29,32,35,38 |
 admin/src/app/edit/components     |    70.89 |    59.09 |    43.75 |    71.23 |                   |
  editor.component.ts                     |    70.89 |    59.09 |    43.75 |    71.23 |... 79,80,82,83,86 |
 admin/src/app/edit/containers     |    71.05 |    62.07 |       40 |     69.7 |                   |
  edit-container.component.ts             |    71.05 |    62.07 |       40 |     69.7 |... 32,37,38,41,44 |
 admin/src/app/landing             |    91.67 |    62.07 |    66.67 |    94.74 |                   |
  landing.component.ts                    |    91.67 |    62.07 |    66.67 |    94.74 |                21 |
 admin/src/app/models              |       90 |      100 |    66.67 |       90 |                   |
  application.ts                          |      100 |      100 |      100 |      100 |                   |
  config-item.ts                          |    84.62 |      100 |       50 |    84.62 |             12,15 |
 admin/src/app/view/components     |    77.27 |    71.88 |    57.14 |    76.79 |                   |
  view-app.component.ts                   |    95.24 |    79.31 |       80 |      100 |         3,4,5,6,9 |
  view-list.component.ts                  |    68.89 |    65.71 |    44.44 |     67.5 |... 36,40,41,42,45 |
 admin/src/app/view/containers     |    80.65 |    62.07 |    66.67 |    80.77 |                   |
  view-container.component.ts             |    80.65 |    62.07 |    66.67 |    80.77 |    23,24,25,26,29 |
 core/src                                 |      100 |       50 |      100 |      100 |                   |
  index.ts                                |      100 |       50 |      100 |      100 |                 3 |
  public_api.ts                           |      100 |       50 |      100 |      100 |                 6 |
 core/src/lib                             |    93.85 |    66.67 |    69.23 |    96.43 |                   |
  core.module.ts                          |    94.12 |       50 |       50 |    96.67 |                31 |
  core.testing.module.ts                  |    93.55 |    79.31 |    77.78 |    96.15 |                56 |
 core/src/lib/app-config                  |    66.67 |    54.55 |       40 |    66.67 |                   |
  app-config.service.ts                   |    66.67 |    54.55 |       40 |    66.67 |... 29,30,31,32,33 |
 core/src/lib/auth                        |       48 |    42.34 |       27 |    47.91 |                   |
  auth.actions.ts                         |    71.88 |      100 |    52.17 |    71.88 |... 74,81,88,95,96 |
  auth.effects.ts                         |    46.38 |    65.71 |    18.75 |    45.16 |... 71,72,75,76,80 |
  auth.guard.ts                           |    66.67 |    54.55 |     37.5 |    66.67 |... 28,29,30,32,35 |
  auth.reducer.ts                         |    33.33 |       20 |        0 |    40.91 |... 24,27,30,33,42 |
  auth.service.ts                         |       42 |    39.13 |       25 |    40.48 |... 51,53,55,58,60 |
  queryParamUtils.ts                      |       12 |        0 |        0 |    12.24 |... 89,90,91,92,93 |
  role.guard.ts                           |    57.14 |    46.15 |       30 |    57.14 |... 44,45,46,47,49 |
  secure.directive.ts                     |    48.39 |       46 |       20 |    47.27 |... 70,73,77,80,83 |
 core/src/lib/components/account-dropdown |    83.33 |    79.31 |     37.5 |       84 |                   |
  account-dropdown.component.ts           |    83.33 |    79.31 |     37.5 |       84 |       18,22,25,28 |
 core/src/lib/components/navbar           |    95.65 |    79.31 |       60 |      100 |                   |
  navbar.component.ts                     |    95.65 |    79.31 |       60 |      100 |         3,4,5,6,9 |
 core/src/lib/containers/page-not-found   |    94.74 |    62.07 |       60 |      100 |                   |
  page-not-found.component.ts             |    94.74 |    62.07 |       60 |      100 |         3,4,5,6,9 |
 core/src/lib/containers/parent           |       80 |    79.31 |       60 |       80 |                   |
  parent.component.ts                     |       80 |    79.31 |       60 |       80 |    19,20,21,22,23 |
 core/src/lib/containers/unauthorized     |    94.74 |    62.07 |       60 |      100 |                   |
  unauthorized.component.ts               |    94.74 |    62.07 |       60 |      100 |         3,4,5,6,9 |
 core/src/lib/models                      |      100 |      100 |       50 |      100 |                   |
  user.ts                            |      100 |      100 |       50 |      100 |                   |
  navbar-link.ts                          |      100 |      100 |       50 |      100 |                   |
  secure.ts                               |      100 |      100 |       50 |      100 |                   |
------------------------------------------|----------|----------|----------|----------|-------------------|

=============================== Coverage summary ===============================
Statements   : 68.08% ( 836/1228 )
Branches     : 58.27% ( 479/822 )
Functions    : 40.72% ( 125/307 )
Lines        : 68.74% ( 741/1078 )
================================================================================

admin/src/app 行显示 admin 项目的覆盖率为 94.55% (...),但这些数字并未反映在摘要中。

我明白为什么它试图包含文件(它们正在被导入)......但我该如何排除它们?我曾尝试修改 admin/ 中的 tsconfig.spec.json 文件以排除这些文件,但这不起作用。当我修改根 tsconfig.json 文件以删除库的“路径”(别名为 @martzcodes/core 并以这种方式导入)时,我收到了相应的导入警告。我不应该嘲笑我所有的库......

Typescript 版本 2.9.2,@angular/cli 6.1.3

【问题讨论】:

【参考方案1】:

angular.json 中使用codeCoverageExclude test.options 像这样:

"codeCoverageExclude": [
  "admin/**",
]

https://github.com/angular/angular-cli/issues/3356

angular cli exclude files/directory for `ng test --code-coverage`

【讨论】:

以上是关于Angular 6 代码覆盖率...如何在应用程序的代码覆盖率中排除本地库?的主要内容,如果未能解决你的问题,请参考以下文章

如何在订阅Angular7单元测试用例中对代码进行单元测试

Angular中伊斯坦布尔覆盖报告中的标记异常

如何通过 Angular、Karma 和 Webpack 的单独文件拆分代码覆盖率?

如何使用 angular9 和 Jasmine 为所有组件生成 .pdf 格式的单元测试用例代码覆盖率报告

防止 Angular 6 路由器覆盖 Express Server 中定义的路由

如何覆盖 Angular 2 材质样式?