【问题标题】:Generating Component without spec.ts file in Angular 2+在Angular 2+中生成没有spec.ts文件的组件
【发布时间】:2017-04-20 19:00:36
【问题描述】:

每当我创建一个新组件时,有没有办法摆脱 Angular 2+ 中的 spec.ts 文件。我知道这是为了测试目的,但如果我不需要它怎么办。

可能有一些设置可以禁用这个特定的测试文件。

【问题讨论】:

  • 当你使用 Angular CLI 创建新组件时?
  • 是的,当我们从 Angular cli 生成组件时。
  • Angular 7/8 CLI 其 --skipTests=true|false

标签: angular angular-cli angular-components angular-schematics


【解决方案1】:

对于 Angular 10+ - 用于所有项目的全局设置。

  1. 转到 angular.json 文件

  2. 在“原理图”字段中添加:

     "@schematics/angular:component": {
       "skipTests": true
     },
    

** 也可以用于服务、警卫等...

    "@schematics/angular:service": {
      "skipTests": true
    },
    "@schematics/angular:guard": {
      "skipTests": true
    }

【讨论】:

    【解决方案2】:

    在您的项目中进入 angular.json 文件并在原理图字段中添加 spec - false

    保存并重新启动您的项目 现在您可以在没有规范文件的情况下创建所有组件

     "schematics": {
                    "@schematics/angular:component": {
                        "style": "scss",
                        "spec": false
                    }
                },
    

    【讨论】:

      【解决方案3】:

      回复:在 Angular 2+ 中生成没有 spec.ts 文件的组件

      ng g c ComponentName --skip-tests true

      【讨论】:

      • 欢迎来到 StackOverflow!这似乎是正确的,但太像已经给出的答案,所以将来您可能希望检查重复。例如,如果 ComponentName 的大小写需要准确,这是非常好的信息。
      【解决方案4】:

      针对 Angular >=8 CLI 更新

      对于一个组件,使用以下命令:

      ng generate component --skip-tests=true component-name
      

      对于单个项目,在您的 angular.json 中更改或添加以下内容:

      { 
        "projects": {
          "{PROJECT_NAME}": {
            "schematics": {
              "@schematics/angular:component": {
                "skipTests": true
              }
            }
          }
        }
      }
      

      对于所有项目的全局设置,请在 angular.json 中更改或添加以下内容:

      { 
        "schematics": {
          "@schematics/angular:component": {
            "skipTests": true
          }
        }
      }
      

      或者使用命令行

      ng config schematics.@schematics/angular:component.skipTests true
      

      在您的angular-cli.json 中,将 spec.component 参数设置为false

      {
         ...
         "defaults" : {
             ...
             "spec": {
                 ...
                 "component": false
             }
         }
      }
      

      或在创建过程中使用--spec=false 选项

      ng generate component --spec=false component-name
      

      【讨论】:

      • @Pierre - 如果我也不想要文件夹怎么办
      • 它可以工作,但默认对象内没有“规范”,所以它可能在以后的版本中由 Angular 更新。新的解决方案应该是这样的"defaults": { "class": { "spec": false }, "component": { "spec": false } }
      • 选项spec 已弃用:改用skipTests
      • 有没有办法停止为整个项目生成这个 spec.ts 文件?
      • @Damika 不是在我的回答开头提到的吗?
      【解决方案5】:

      它对我有用 angular 11

      ng g c posts/new-component --module app --skip-tests
      

      注意:

      组件名称应如下所示:new-componentnewComponent

      为特定文件夹生成:dir/new-component

      new-component 添加到app.module.ts--module app

      在命令中只使用--skip-tests 来生成没有spec.ts 文件的组件

      【讨论】:

        【解决方案6】:

        对于 Angular 8+,您只需要添加

        --skipTests=true

        最后

        ng g c component-name --skipTests=true
        

        g 缩写为生成。 c 缩写为组件

        你也可以使用

        ng generate component component-name --skipTests=true
        

        【讨论】:

          【解决方案7】:

          角度 $ ng g component component-name --spec=false

          角度 > 8 $ ng g component component-name --skipTests=false --flat

             $ ng g component employee/listEmployees --skipTests=false --flat
          

          【讨论】:

            【解决方案8】:

            这很容易做到。在创建组件时添加

            --skipTests=true

            ng generate component --skipTests=true componentName
            

            或者

            ng g c --skipTests=true componentName
            

            【讨论】:

              【解决方案9】:

              使用时:

              • Angular CLI:10.0.3
              • 节点:12.16.3
              • 角度:10.0.4

              生成组件时必须使用“--skip-tests true”。 (注意:您可以通过在终端中尝试“ng v”或“ng -v”来获取上述版本信息)。

              您可以使用“ng g c --help”获取“ng g c(ng generate component 的缩写)”的完整开关列表。

              您也可以手动添加组件。为此,请通过右键单击项目的 /src/app 文件夹来创建一个新文件夹。创建文件夹后,右键添加一个或多个文件,如mycomp.component.html和mycomp.component.ts。 添加 mycomp.component.ts 是组件的最低要求列表,因为您可以将内联 css 和 html 添加到 .ts 文件中。

              示例:

              import { Component } from '@angular/core';
              

              @组件 ({ 选择器:'app-mycomp', 模板:'<p> Hello Angular </p>' })

              导出类 MyComp {}

              在 app.module.ts 声明列表中导入并注册 MyComp,并使用选择器将其添加到 app.component.html 或其他模板中 (<app-mycomp></app-mycomp>

              【讨论】:

                【解决方案10】:

                最新的 Angular 9 兼容

                CLI 命令

                ng generate component your-component-name --skipTests=true
                

                使用别名s而不是--skipTests

                ng generate component your-component-name -s=true
                

                修改 angular.json 以避免总是使用上述 CLI 命令

                将下面的 sn-p 复制到特定项目的根目录 (projects.your-project-name) 中的 angular.json

                以下内容将确保不会为 ComponentsClassDirectivesPipe.spec 文件/strong> 和 Service 使用ng generate 命令。您可以根据需要选择。

                {
                  ...
                  "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
                        }
                      },
                    ...
                }
                

                PS:--spec=false 选项现已弃用,将不起作用

                【讨论】:

                • s 应该在这里大写。 Small s 创建内联样式(没有外部 scss 文件)。 ng generate component your-component-name -S=true
                • 我尝试了 -S 选项并得到了一个未知的评论。 -s 并没有阻止规范文件,而是阻止了 css 文件。我使用的是 12 版。
                • 其实我的意思是“未知选项”而不是“未知评论”
                【解决方案11】:

                如果您想跳过特定组件的规范文件。

                新版本:

                ng generate component "componentName" --skipTests
                

                例子

                ng generate component recipe-list --skipTests
                

                旧版本:

                ng generate component "componentName" --spec false
                

                例子

                ng generate component recipe-list --spec false
                

                【讨论】:

                  【解决方案12】:

                  --spec 将不再工作。请改用--skip-tests

                  您将通过以下命令看到所有选项:

                  ng g c --help
                  

                  【讨论】:

                    【解决方案13】:

                    这里是 angular 9

                    ng g class models\user --skip-tests true
                    

                    它不会生成 spec.ts 文件。

                    【讨论】:

                      【解决方案14】:

                      只是添加了这一点信息,因为我发现自己在同样的问题上苦苦挣扎,并且无法使用当前的答案来解决它,而且我不想对 angular-cli.json 文件进行更改。 Angular CLI --spec=false 的最新版本似乎已被贬值,因此不再有效,请改用 --skipTests

                      测试:

                      Angular CLI: 9.1.1
                      Node: 12.16.1
                      OS: win32 x64
                      

                      您的命令 then 是: ng g c mycomponent --skipTests 代替 ng g c mycomponent --spec=false

                      PS:始终使用--dry-run 选项测试您的命令以测试您的输出。

                      【讨论】:

                        【解决方案15】:

                        对于 Angular 9,您可以使用

                        ng g c "componentName" --spec=false

                        生成没有spec文件的组件

                        【讨论】:

                          【解决方案16】:

                          对于 Angular 8:ng generate component mycomponent --skipTests=false

                          【讨论】:

                            【解决方案17】:

                            如果你想跳过你的 spec.ts 文件,也可以使用以下命令:

                            ng g c try-it --skipTests=true

                            在哪里

                            g c => 生成组件, try-it => 组件名称, --skipTest=true => == -- spec false

                            【讨论】:

                              【解决方案18】:

                              创建组件时可以使用以下命令

                              ng g c component-name --spec false
                              

                              【讨论】:

                                【解决方案19】:

                                对于 Angular 7 及更高版本,这将起作用:

                                ng generate component componentname --skipTests
                                

                                ng generate component componentname --skipTests=true

                                【讨论】:

                                  【解决方案20】:

                                  对于 angular 6+ 只需运行以下命令:

                                  ng config schematics.@schematics/angular.component.spec false
                                  

                                  只需将其添加到您的 angular.json 文件中,您就可以开始了

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

                                  注意:在粘贴此冲突检查之前,希望对您有所帮助

                                  【讨论】:

                                  • 在我的 Angular 7.4 项目中按照上述更新了我的 angular.json,一切都很好。
                                  【解决方案21】:

                                  在 Angular 7 中使用这个简单的命令简单地创建组件

                                  ng g c component-name --spec false
                                  

                                  或者如果真的不想将它包含在任何组件中,只需更新 angular.json

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

                                  【讨论】:

                                    【解决方案22】:

                                    对于 Angular 6,在 "schematics":{} 内的 angular.json 中添加:

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

                                    按照 Franklin Pious 的建议,运行以下命令:

                                    ng config schematics.@schematics/angular.component.spec false
                                    

                                    【讨论】:

                                      【解决方案23】:

                                      默认情况下,有一个命令可以禁用 Angular 生成“规范”文件

                                      ng set defaults.class.spec=false
                                      ng set defaults.component.spec=false
                                      

                                      更新:对于 Angular 6

                                      ng config schematics.@schematics/angular.component.spec false

                                      【讨论】:

                                      • 在 Angular 6 中已弃用
                                      【解决方案24】:

                                      对于 Angular 6

                                      ng config schematics.@schematics/angular.component.spec false
                                      

                                      【讨论】:

                                      • 迄今为止 ng6+ 的最佳答案
                                      • @danday74 谢谢
                                      【解决方案25】:

                                      只需试试这个。 这是有效的

                                      ng g c component_name --spec false
                                      

                                      【讨论】:

                                        【解决方案26】:

                                        在最近的 Angular 版本中,您可以配置 cli 生成器以禁用为 angular-cli.json 中的组件和服务创建规范文件,如下所示:

                                        "defaults": {
                                            "component": { "spec": false },
                                            "service": { "spec": false },
                                            ...
                                        }
                                        

                                        您可以添加额外的行来禁用守卫和类等的规范。

                                        【讨论】:

                                        • 虽然此代码可能会回答问题,但提供有关它如何和/或为什么解决问题的额外上下文将提高​​答案的长期价值。
                                        • 我在答案中添加了额外的解释。谢谢。
                                        【解决方案27】:
                                        ng g c component-name --spec false
                                        

                                        【讨论】:

                                          【解决方案28】:

                                          根据fix #156,您可以使用创建没有规范文件的组件

                                          ng generate component my-comp --nospec
                                          

                                          【讨论】:

                                            【解决方案29】:

                                            当使用 angular-cli 时,有很多选项可以通过。要生成无需测试的新组件,您可以像这样简单地添加--spec=false

                                            ng generate component --spec=false my-component
                                            

                                            在 angular-cli.json 中还有一个选项,您希望默认启用哪些类型的规范,您可以在其中修改行为:

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

                                            【讨论】:

                                              猜你喜欢
                                              • 1970-01-01
                                              • 2016-09-26
                                              • 2017-04-06
                                              • 2017-06-22
                                              • 2016-09-26
                                              • 1970-01-01
                                              • 1970-01-01
                                              • 1970-01-01
                                              • 1970-01-01
                                              相关资源
                                              最近更新 更多