Generating Component without spec.ts file in Angular 2+

前端 未结 25 970
轮回少年
轮回少年 2021-02-01 00:48

Is there a way to get rid of the spec.ts file in Angular 2+, whenever I create a new component. I know this is for testing purpose but what if I don\'t need it.

May be t

相关标签:
25条回答
  • 2021-02-01 00:56

    It is very easy to do. While creating the component add

    --skipTests=true

    ng generate component --skipTests=true componentName
    

    Or

    ng g c --skipTests=true componentName
    
    0 讨论(0)
  • 2021-02-01 00:57

    Latest Angular 9 Compatible

    CLI Command

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

    or using the alias s instead of --skipTests

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

    Modifying angular.json to avoid using above CLI command always

    Copy the below snippet to the root of a specific project (projects.your-project-name) in its angular.json.

    The below will ensure .spec files are not created for Components, Class, Directives, Pipe and Service with the ng generate command. You may choose the ones as per requirement.

    {
      ...
      "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: The --spec=false option is now deprecated and will not work

    0 讨论(0)
  • 2021-02-01 00:57

    Use also the following command if you want to skip your spec.ts file:

    ng g c try-it --skipTests=true

    where

    g c => generate component , try-it => component name , --skipTest=true => == -- spec false.

    0 讨论(0)
  • 2021-02-01 00:58

    For Angular 6

    ng config schematics.@schematics/angular.component.spec false
    
    0 讨论(0)
  • 2021-02-01 00:58

    For angular 6+ Simply run this command:

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

    OR

    just add this in your angular.json file and you're good to go

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

    NOTE: before pasting this check for conflicts, hope it helps

    0 讨论(0)
  • 2021-02-01 00:58

    simply try this one. this is working

    ng g c component_name --spec false
    
    0 讨论(0)
提交回复
热议问题