首页 > 解决方案 > Angular 11 CLI,“ng generate”选项:--skip-tests

问题描述

在使用 Angular 11 CLI 时,当我生成一个新组件时,我可以指定选项--skip-tests以避免.component.spec.ts在此阶段生成一个。

ng generate component --name asdf --module app --skip-tests --dry-run

当我生成一个包含新组件(--route选项)的新模块时,我无法指定该--skip-tests选项。

ng generate module --name asdf --module app --route asdf --routing true --skip-tests --dry-run

如果我这样做,我会收到错误:

Unknown option: '--skip-tests'

在这种情况下是否有另一种方法可以跳过生成测试,或者它只是不支持generate module

标签: angular-cli

解决方案


根据这篇文章,您可以--skip-tests在生成整个项目时添加标志,也可以生成单个组件。从逻辑的角度来看,您不能在模块上应用标志是有道理的,因为模块不是使用测试文件生成的。

要对所有未来生成的代码进行更改,请angular.json通过添加skipTests:true到原理图部分来修改您的文件。

 "schematics": {
        "@schematics/angular:component": {
          "style": "scss",
          "skipTests": true
        },
        "@schematics/angular:class": {
          "skipTests": true
        },
        "@schematics/angular:directive": {
          "skipTests": true
        },
        "@schematics/angular:guard": {
          "skipTests": true
        },
        "@schematics/angular:module": {
          "skipTests": true
        },
        "@schematics/angular:pipe": {
          "skipTests": true
        },
        "@schematics/angular:service": {
          "skipTests": true
        }
     }

推荐阅读