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.
Solution 1:
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
Solution 2:
For Angular 6
ng config schematics.@schematics/angular.component.spec false
Solution 3:
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
Solution 4:
When using angular-cli there is a bunch of options to pass. To generate a new component without tests, you can simply add --spec=false
like so:
ng generate component --spec=false my-component
There is also an option in the angular-cli.json for which types you want to enable specs by default, where you can modify the behaviour:
"defaults": {
"spec": {
"class": false,
"component": true,
"directive": true,
"module": false,
"pipe": true,
"service": true
}
}
Solution 5:
For Angular 7 and higher this will work :
ng generate component componentname --skipTests
or
ng generate component componentname --skipTests=true