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
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.
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
>
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.
For Angular 7 and higher this will work :
ng generate component componentname --skipTests
or
ng generate component componentname --skipTests=true
You can use the following command when you create your component
ng g c component-name --spec false
For Angular 8: ng generate component mycomponent --skipTests=false
--spec
will no longer work. Use --skip-tests
instead.
You will see all the options with following command:
ng g c --help