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

為{幸葍}努か 提交于 2019-11-27 12:45:45

问题


I KNOW IT'S KIND OF A BAD PRACTICE, but pair with me:

I'm using Angular-CLI and particularly ng g to generate all of my classes, however, I'm not interested in any test file *.spec.ts and 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 default flag is set to true --spec

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

But How to disable the creation of test files globally? is there any default setting for it?

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

ng set spec=false --global

Then tried configuring ts settings file src/tsconfig.json by filling the exclude array.

"exclude": [
    "**/*.spec.ts"
]

回答1:


For Angular 6 >

Configuration (for e.g. generation of specs) can be done manually or with the Angular CLI.


Manual Angular CLI configuration

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




回答2:


If you're using v6 and need to edit your angular.json

You can edit the schematics for your 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
    }
  },



回答3:


You can run this command to disable spec file generation for a specific type of file:

ng set defaults.spec.FILETYPE false

For example:

ng set defaults.spec.component false // Won't generate spec files for .component files

Alternately you can just disable all spec file generation from the angular-cli.json file.

{
  ...
  "defaults": {
    "spec": {
      "class": false,
      "component": false,
      "directive": false,
      "module": false,
      "pipe": false,
      "service": false
    }
  }
}



回答4:


Just to update Sabbir Rahman's answer:

In version 1.0.2 of the CLI you will have to set the spec file to false for each individual type. An example is included below:

"defaults": {
    "styleExt": "scss",
    "component": {
      "spec": false
    },
    "service": {
      "spec": false
    },
    "directive": {
      "spec": false
    },
    "class": {
      "spec": false // Set to false by default
    },
    "module": {
      "spec": false // Set to false by default
    },
    "pipe": {
      "spec": false
    }
  }



回答5:


you can add --spec=false

example

ng g c home --spec=false

log will be

CREATE src/app/home/home.component.scss (0 bytes)
CREATE src/app/home/home.component.html (23 bytes)
CREATE src/app/home/home.component.ts (262 bytes)
UPDATE src/app/app.module.ts (467 bytes)



回答6:


you can add --skipTests=true|false if true it wont generate any spec.ts

example : ng g component componentName --skipTests=true

this line wont generate any spec.ts files

EDIT:

Note: As of angular 7 this command doesn't work, even though this command is mentioned on the official website of angular. Here: https://angular.io/cli/generate#component



来源:https://stackoverflow.com/questions/42049756/angular-6-cli-typescript-how-to-stop-generating-spec-ts-test-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!