【问题标题】:Angular 9 + CLI (TypeScript) - How to stop generating .spec.ts test filesAngular 9 + CLI (TypeScript) - 如何停止生成 .spec.ts 测试文件
【发布时间】:2017-06-22 07:56:03
【问题描述】:

我知道这是一种不好的做法,但请耐心等待:

我正在使用 Angular-CLI,尤其是 ng g 来生成我的所有类。但是,我对任何 *.spec.ts 测试文件都不感兴趣。我知道有两个标志(--inline-template--inline-style)来处理内联 CSS 和 HTML,而不是单独的文件,并且对于规范,--spec 标志默认设置为 true。

所以对于每次运行,是的,我可以做到ng g c foo --it --is --spec=false

但是如何全局禁用测试文件的创建呢?有没有默认设置?

仓促地,我做了一些事情(没用),比如:

ng set spec=false --global

我还尝试通过填充排除数组来配置我的src/tsconfig.json

"exclude": [
    "**/*.spec.ts"
]

【问题讨论】:

    标签: angular typescript angular-cli angular2-cli


    【解决方案1】:

    您可以运行此命令来禁用特定类型文件的规范文件生成:

    ng set defaults.spec.FILETYPE false

    例如:

    ng set defaults.spec.component false // Won't generate spec files for .component files

    或者,您可以禁用 angular-cli.json 文件中的所有规范文件生成。

    {
      ...
      "defaults": {
        "spec": {
          "class": false,
          "component": false,
          "directive": false,
          "module": false,
          "pipe": false,
          "service": false
        }
      }
    }
    

    【讨论】:

    • angular-cli.json 已弃用,取而代之的是 angular.json。而且 angular.json 中没有 defaults 键,所以恐怕这个答案已经过时了。
    【解决方案2】:

    只是为了更新Sabbir Rahman's answer

    在 CLI 版本 1.0.2 中,您必须为每个单独的类型将规范文件设置为 false。下面是一个例子:

    "defaults": {
        "styleExt": "scss",
        "component": {
          "spec": false
        },
        "service": {
          "spec": false
        },
        "directive": {
          "spec": false
        },
        "class": {
          "spec": false // Set to false by default
        },
        "module": {
          "spec": false // Set to false by default
        },
        "pipe": {
          "spec": false
        }
      }
    

    【讨论】:

      【解决方案3】:

      对于 Angular 9 >(版本 6 > 以下)

      1) 将 sn-p 复制到 angular.json 的根目录,(将设置配置到所有项目/全局)。
      2) 或者将 sn-p 复制到根目录angular.json 中的特定项目 (projects.your-project-name)(配置特定项目的设置)。

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

      每种文件类型的所有可配置选项Schematic Options

      "schematics": {
        "@schematics/angular:component": {
          "changeDetection": "Default",
          "entryComponent": false,
          "export": false,
          "flat": false,
          "inlineStyle": false,
          "inlineTemplate": false,
          "module": "",
          "prefix": "",
          "selector": "",
          "skipImport": false,
          "spec": true,
          "style": "css",
          "viewEncapsulation": "Emulated",
          "skipTests": "false"
        },
        "@schematics/angular:module": {
          "commonModule": true,
          "flat": false,
          "module": "",
          "routing": false,
          "routingScope": "Child"
        },
        "@schematics/angular:service": {
          "flat": true,
          "skipTests": true
        },
        "@schematics/angular:pipe": {
          "export": false,
          "flat": true,
          "module": "",
          "skipImport": false,
          "skipTests": true
        },
        "@schematics/angular:directive": {
          "export": false,
          "flat": true,
          "module": "",
          "prefix": "app",
          "selector": "",
          "skipImport": false,
          "skipTests": true
        },
        "@schematics/angular:class": {
          "skipTests": true
        }
      },
      

      对于 Angular 6 >

      1) 将 sn-p 复制到 angular.json 的根目录,(将设置配置到所有项目/全局)。
      2) 或者将 sn-p 复制到根目录angular.json 中的特定项目 (projects.your-project-name)(配置特定项目的设置)。

      "schematics": {
        "@schematics/angular:component": {
          "styleext": "scss",
          "spec": false
        },
        "@schematics/angular:class": {
          "spec": false
        },
        "@schematics/angular:directive": {
          "spec": false
        },
        "@schematics/angular:guard": {
          "spec": false
        },
        "@schematics/angular:module": {
          "spec": false
        },
        "@schematics/angular:pipe": {
          "spec": false
        },
        "@schematics/angular:service": {
          "spec": false
        }
      },
      

      每种文件类型的所有可配置选项 (Schematic Options):

      "schematics": {
        "@schematics/angular:component": {
          "changeDetection": "Default",
          "export": false,
          "flat": false,
          "inlineStyle": false,
          "inlineTemplate": false,
          "module": "",
          "prefix": "",
          "selector": "",
          "skipImport": false,
          "spec": true,
          "styleext": "css",
          "viewEncapsulation": "Emulated"
        },
        "@schematics/angular:module": {
          "commonModule": true,
          "flat": false,
          "module": "",
          "routing": false,
          "routingScope": "Child",
          "spec": true
        },
        "@schematics/angular:service": {
          "flat": true,
          "spec": true
        },
        "@schematics/angular:pipe": {
          "export": false,
          "flat": true,
          "module": "",
          "skipImport": false,
          "spec": true
        },
        "@schematics/angular:directive": {
          "export": false,
          "flat": true,
          "module": "",
          "prefix": "app",
          "selector": "",
          "skipImport": false,
          "spec": true
        },
        "@schematics/angular:class": {
          "spec": true
        }
      },
      

      使用 Angular CLI 配置 Angular CLI

      错误:

      ng set defaults.spec.component false 命令导致错误:get/set have been deprecated in favor of the config command.

      ng set 已更改为 ng config

      使用 Angular CLI(配置命令用法):

      angular.json 中用于生成规范、内​​联模板、内联样式等的设置现在保留在 schematics.@schematics/angular.<file-type>.<setting> 内。

      运行ng config schematics.@schematics/angular.component.spec false 为组件配置规范。此命令将设置添加到 angular.json 文件中的原理图属性内。


      Angular CLI workspace file (angular.json) on Angular Github

      Schematic options inside schema.json

      How to do X in Angular CLI v6

      【讨论】:

        【解决方案4】:

        如果您使用的是 v6 并且需要编辑您的 angular.json

        您可以为您的项目编辑原理图。

        "schematics": {
            "@schematics/angular:component": {
              "styleext": "scss",
              "spec": false
            },
            "@schematics/angular:class": {
              "spec": false
            },
            "@schematics/angular:directive": {
              "spec": false
            },
            "@schematics/angular:guard": {
              "spec": false
            },
            "@schematics/angular:module": {
              "spec": false
            },
            "@schematics/angular:pipe": {
              "spec": false
            },
            "@schematics/angular:service": {
              "spec": false
            }
          },
        

        【讨论】:

        • 这应该是 Angular 6 的正确答案。将此代码添加到项目的 angular.json 文件中。您应该使用带有“Angular Files”插件的 VScode 工具来创建服务、组件、模块、路由...
        • 谢谢.. 它也适用于 Angular 7。在 angular.json 文件末尾添加
        【解决方案5】:

        你可以添加--skipTests=true|false,如果为真它不会生成任何spec.ts

        示例:ng g component componentName --skipTests=true

        此行不会生成任何 spec.ts 文件

        编辑:

        注意:从 Angular 7 开始,该命令不起作用,即使在 Angular 官方网站上提到了该命令。 这里:https://angular.io/cli/generate#component

        【讨论】:

        • 不想 -1 你的答案,但它不起作用(我用 ng7 试过)...
        • 用 angular 9 试过了,这行得通。我更喜欢这种方式,而不是通过 json 禁用所有规范文件。
        【解决方案6】:

        你可以加--spec=false

        例子

        ng g c home --spec=false
        

        日志将是

        CREATE src/app/home/home.component.scss (0 bytes)
        CREATE src/app/home/home.component.html (23 bytes)
        CREATE src/app/home/home.component.ts (262 bytes)
        UPDATE src/app/app.module.ts (467 bytes)
        

        【讨论】:

          【解决方案7】:

          在 Angular.json 上添加以下代码

          "schematics": {
              "@schematics/angular": {
                "component": {
                  "spec": false
                }
              }
            }
          

          【讨论】:

            【解决方案8】:

            您可以在创建应用时尝试--skip-tests,如下所示。

            ng new yourProjectName --skip-tests

            【讨论】:

              【解决方案9】:

              对于角度 8+:

              不推荐使用“spec”选项:改用“skipTests”,所以如果你想创建一个新组件,请使用:

              ng g c my-new-component --skipTests=true
              

              【讨论】:

              • 已弃用!!
              【解决方案10】:

              对于 Angular 9+:

              Angular.json 配置略有更改,这导致以前的配置架构发生更改,根据此GitHub issue on Angular/CLI

              改编自@sacgrover's commentmy own:

              老办法

              "@schematics/angular:component": {
                  // true => DO generate spec files, false => DON'T generate them
                  "spec": true,
                  "styleext": "scss"
              }
              

              新方法:

              "@schematics/angular:component": {
                  // true => DON'T generate spec files, false => DO generate them
                  "skipTests": true,
                  "style": "scss"
              }
              

              其他参考 => Angular Docs for CLI Generate Command

              【讨论】:

              • 属性“@schematics/angular:component”是不允许的(angular10)。
              【解决方案11】:

              只需运行以下命令:ng config schematics.@schematics/angular.component.spec false

              【讨论】:

                【解决方案12】:

                只要这样做就可以了:

                ng generate component newFile --skip-tests
                

                【讨论】:

                  【解决方案13】:
                  "@schematics/angular:component": {"style": "scss","skipTests": true}
                  

                  只需将"skipTests":true 添加到您的 angular.json 文件中即可解决问题

                  【讨论】:

                    猜你喜欢
                    • 2016-09-26
                    • 1970-01-01
                    • 2017-04-06
                    • 1970-01-01
                    • 2018-11-05
                    • 2018-05-02
                    • 2017-03-01
                    • 2017-03-30
                    • 2018-06-16
                    相关资源
                    最近更新 更多