Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generating Component without spec.ts file in Angular 2+

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 there is some setting to disable this specific testing file.

like image 675
saadeez Avatar asked Dec 06 '16 07:12

saadeez


People also ask

How do you make a component without spec ts?

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.

Do we need Spec TS file in angular?

if you generate new angular project using "ng new", you may skip a generating of spec. ts files. For this you should apply --skip-tests option.

How do I create a spec TS file?

Select an Angular *. ts file and use right click to generate the spec file. Generate spec (jest / jasmine / mockito / empty) files for Angular elements: component.


1 Answers

Updated for Angular >=8 CLI

For one component use the following command:

ng generate component --skip-tests=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 
like image 180
Poul Kruijt Avatar answered Sep 29 '22 21:09

Poul Kruijt