Generating Component without spec.ts file in Angular 2+

前端 未结 25 966
轮回少年
轮回少年 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:52

    Updated for Angular >=8 CLI

    For one component use the following command:

    ng generate component --skipTests=true component-name
    

    For a single project, change or add the following in your angular.json:

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

    For a global setting for all your projects, change or add the following in your angular.json:

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

    Or by using the command line

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

    < Angular 8

    Inside your angular-cli.json set the spec.component parameter to false:

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

    or use the --spec=false option during creation

    ng generate component --spec=false component-name
    
    0 讨论(0)
  • 2021-02-01 00:52

    For angular 6 add this in angular.json inside "schematics":{} :

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

    OR

    as Franklin Pious sugested, run this command:

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

    here what it is for angular 9

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

    it will not generate spec.ts files.

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

    For Angular 9 you can use

    ng g c "componentName" --spec=false

    to generate component without spec file

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

    Simply create Component with this simple command in Angular 7

    ng g c component-name --spec false
    

    or if really dont want to include it in any component simply update angular.json

    "@schematics/angular": {
      "component": {
        "spec": false
      }
    }
    
    0 讨论(0)
  • 2021-02-01 00:56

    If you would like to skip spec file for specific component.

    New version:

    ng generate component "componentName" --skipTests
    

    Example

    ng generate component recipe-list --skipTests
    

    Old version :

    ng generate component "componentName" --spec false
    

    Example

    ng generate component recipe-list --spec false
    
    0 讨论(0)
提交回复
热议问题