Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 9 + CLI (TypeScript) - How to stop generating .spec.ts test files

I know it's kind of a bad practice, but bear with me:

I'm using Angular-CLI, particularly ng g to generate all of my classes. However, I'm not interested in any *.spec.ts test files. I know that there are two flags (--inline-template, --inline-style) to handle inline CSS and HTML instead of separated files, and for spec the --spec flag is set to true by default.

So for each run, yes, I could do ng g c foo --it --is --spec=false.

But how do I disable the creation of test files globally? Is there any default setting for it?

Rashly, I did some stuff (that didn't work), like:

ng set spec=false --global 

I also tried configuring my src/tsconfig.json by filling the exclude array:

"exclude": [     "**/*.spec.ts" ] 
like image 462
Mo Kawsara Avatar asked Feb 05 '17 07:02

Mo Kawsara


People also ask

How can you avoid Spec TS file in angular while creating component?

Run ng config schematics. @schematics/angular. component. spec false to configure spec for components.

Can I delete Spec TS files?

As said around: if you don't plan on testing you can delete spec. ts files manually.

Which angular CLI flag will generate unit tests for a component?

Every Angular CLI command that scaffolds out new code for you will also, by default, generate an accompanying unit test. This stands true for commands like ng generate service or ng generate component . Below, you can see an example of a generated unit test for a component scaffolded out via running ng g c my .

Why we use Spec TS file in angular?

spec. ts file. This file contains unit tests for the main AppComponent. When running tests using the Angular CLI, all unit tests in files with the *.


1 Answers

For Angular 9 > (version 6 > below)

1) Copy snippet to the root of angular.json, (configures settings to all projects/globally).
2) Or copy snippet to the root of a specific project (projects.your-project-name) in angular.json (configures settings for a specific project).

"schematics": {   "@schematics/angular:component": {     "style": "scss",     "skipTests": true   },   "@schematics/angular:class": {     "skipTests": true   },   "@schematics/angular:directive": {     "skipTests": true   },   "@schematics/angular:pipe": {     "skipTests": true   },   "@schematics/angular:service": {     "skipTests": true   } }, 

All configurable options per type of file Schematic Options:

"schematics": {   "@schematics/angular:component": {     "changeDetection": "Default",     "entryComponent": false,     "export": false,     "flat": false,     "inlineStyle": false,     "inlineTemplate": false,     "module": "",     "prefix": "",     "selector": "",     "skipImport": false,     "spec": true,     "style": "css",     "viewEncapsulation": "Emulated",     "skipTests": "false"   },   "@schematics/angular:module": {     "commonModule": true,     "flat": false,     "module": "",     "routing": false,     "routingScope": "Child"   },   "@schematics/angular:service": {     "flat": true,     "skipTests": true   },   "@schematics/angular:pipe": {     "export": false,     "flat": true,     "module": "",     "skipImport": false,     "skipTests": true   },   "@schematics/angular:directive": {     "export": false,     "flat": true,     "module": "",     "prefix": "app",     "selector": "",     "skipImport": false,     "skipTests": true   },   "@schematics/angular:class": {     "skipTests": true   } }, 

For Angular 6 >

1) Copy snippet to the root of angular.json, (configures settings to all projects/globally).
2) Or copy snippet to the root of a specific project (projects.your-project-name) in angular.json (configures settings for a specific project).

"schematics": {   "@schematics/angular:component": {     "styleext": "scss",     "spec": false   },   "@schematics/angular:class": {     "spec": false   },   "@schematics/angular:directive": {     "spec": false   },   "@schematics/angular:guard": {     "spec": false   },   "@schematics/angular:module": {     "spec": false   },   "@schematics/angular:pipe": {     "spec": false   },   "@schematics/angular:service": {     "spec": false   } }, 

All configurable options per type of file (Schematic Options):

"schematics": {   "@schematics/angular:component": {     "changeDetection": "Default",     "export": false,     "flat": false,     "inlineStyle": false,     "inlineTemplate": false,     "module": "",     "prefix": "",     "selector": "",     "skipImport": false,     "spec": true,     "styleext": "css",     "viewEncapsulation": "Emulated"   },   "@schematics/angular:module": {     "commonModule": true,     "flat": false,     "module": "",     "routing": false,     "routingScope": "Child",     "spec": true   },   "@schematics/angular:service": {     "flat": true,     "spec": true   },   "@schematics/angular:pipe": {     "export": false,     "flat": true,     "module": "",     "skipImport": false,     "spec": true   },   "@schematics/angular:directive": {     "export": false,     "flat": true,     "module": "",     "prefix": "app",     "selector": "",     "skipImport": false,     "spec": true   },   "@schematics/angular:class": {     "spec": true   } }, 

Angular CLI configuration with Angular CLI

ERROR:

The ng set defaults.spec.component false command results in the error: get/set have been deprecated in favor of the config command.

ng set got changed to ng config.

Using the Angular CLI (config command usage):

The settings for generating specs, inline templates, inline styling etc. within angular.json are now persisted inside the schematics.@schematics/angular.<file-type>.<setting>.

Run ng config schematics.@schematics/angular.component.spec false to configure spec for components. This command adds the setting inside the schematics property within the angular.json file.


Angular CLI workspace file (angular.json) on Angular Github

Schematic options inside schema.json

How to do X in Angular CLI v6

like image 197
Brampage Avatar answered Oct 11 '22 14:10

Brampage