Generating Component without spec.ts file in Angular 2+

前端 未结 25 967
轮回少年
轮回少年 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 01:11

    When using:

    • Angular CLI: 10.0.3
    • Node: 12.16.3
    • Angular: 10.0.4

    You have to use "--skip-tests true" when generating component. (Note: you can get the above version info by trying "ng v" or "ng -v" in the terminal).

    You can get the complete list of switches for "ng g c (short for ng generate component)" using "ng g c --help".

    Alternatively you can add a component manually. To do this, make a new folder by right clicking the /src/app folder of your project. After creating the folder, right click on it to add one or more files such as mycomp.component.html and mycomp.component.ts. Adding mycomp.component.ts is the minimum required list for a component, as you can add inline css and html to your .ts file.

    Example:

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

    @Component ({ selector: 'app-mycomp', template: '<p> Hello Angular </p>' })

    export class MyComp {}

    Import and Register MyComp in app.module.ts declarations list, and add it to app.component.html or some other template, using selector (<app-mycomp></app-mycomp>

    0 讨论(0)
  • 2021-02-01 01:12

    In recent Angular versions you can configure the cli generator to disable the creation of spec file for components and services in angular-cli.json as follows:

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

    You can add extra lines to disable specs also for guards and classes and so on.

    0 讨论(0)
  • 2021-02-01 01:14

    For Angular 7 and higher this will work :

    ng generate component componentname --skipTests
    

    or ng generate component componentname --skipTests=true

    0 讨论(0)
  • 2021-02-01 01:14

    You can use the following command when you create your component

    ng g c component-name --spec false
    
    0 讨论(0)
  • 2021-02-01 01:15

    For Angular 8: ng generate component mycomponent --skipTests=false

    0 讨论(0)
  • 2021-02-01 01:18

    --spec will no longer work. Use --skip-tests instead.

    You will see all the options with following command:

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